Simple development of simple examples based on thinkphp6.x API interface

Below, the Thinkphp framework tutorial column will introduce you to a simple example of API interface development based on thinkphp6.x. I hope it will be helpful to friends in need!


A simple example of API interface development-based on thinkphp6.x is

mainly helpful for PHP kids who have never been in contact with interface development, that is: submit a product ID on the front end and return product details; there is nothing for authentication, just for understanding the process, Getting started from proficient

API interface development of small simple example - based thinkphp6.x code is as follows:

View / index / index.html front-end code (requester):: step 1

. 1

2

. 3

. 4

. 5

. 6

. 7

. 8

. 9

10

. 11

12 is

13 is

















    

    







the first Step 2: Controller code (request side) controller/index.php:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23


namespace app\controller;

use app\BaseController;

class Index extends BaseController {         //Front view public function index() { return view(); } //Submit query entry public function api_chaxun() { // http protocol request $url ='http://localhost/index.php/index/goods/api/'; // input('goods_id') is the name value passed from from on the front end $ch = curl_init($url.'?goods_id ='.input('goods_id')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Execute and assign the executed data to $data $data = curl_exec($ch); // Close curl_close($ch ); // return data return $data; } }







































Step 3: API interface side, code controller/goods.php:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15


namespace app\controller;

use app\BaseController;

use think\facade\Db;

class Goods extends BaseController { /** The client submits the product ID (goods_id) to the API * API returns the product information **/ public function api($goods_id=1) { // Query and assign the data to $data $data = Db:: name('goods')->where('id',$goods_id)->find(); // return data return json($data);//print_r($data); } }



















Guess you like

Origin blog.csdn.net/an17822307871/article/details/113020150