Home > PHP Framework > ThinkPHP > Simple small example of API interface development based on thinkphp6.x

Simple small example of API interface development based on thinkphp6.x

藏色散人
Release: 2020-12-11 16:02:54
forward
4979 people have browsed it

The following tutorial column of thinkphp framework will introduce to you a simple small example of API interface development based on thinkphp6.x. I hope it will be helpful to friends in need!

Simple small example of API interface development based on thinkphp6.x

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

Mainly helpful for PHP children who have never been exposed to interface development. That is: the front end submits a product ID and returns product details; there is no authentication or anything else, it is just used to understand the process and become proficient from getting started

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

Step 1: Front-end code (requester) view/index/index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>API接口开发简单小实例-基于thinkphp6.x</title>
</head>
<body>
<form action="http://localhost/index.php/index/index/api_chaxun/" method="post">
    <input type="text" name="goods_id">
    <input type="submit" value="提交查询">
</form>
</body>
</html>
Copy after login

Step 2: Controller code (requester) controller/index.php:

<?php 
namespace app\controller;
use app\BaseController;
class Index extends BaseController {
        //前端视图
public function index() {
return view();
}
//提交查询入口
public function api_chaxun() {
// http协议请求
$url = &#39;http://localhost/index.php/index/goods/api/&#39;;
// input(&#39;goods_id&#39;) 是前端的from传过来的name值
$ch = curl_init($url.&#39;?goods_id=&#39;.input(&#39;goods_id&#39;));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行 并把执行后的数据赋值给 $data
$data = curl_exec($ch);
// 关闭
curl_close($ch);
// 返回数据
return $data;
}
}
Copy after login

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

<?php 
namespace app\controller;
use app\BaseController;
use think\facade\Db;
class Goods extends BaseController {
/** 客户端提交商品ID(goods_id)给API
* API返回此商品信息
**/
public function api($goods_id=1) {
// 查询 并把数据赋值给 $data
$data = Db::name(&#39;goods&#39;)->where(&#39;id&#39;,$goods_id)->find();
// 返回数据
return json($data);//print_r($data);
}
}
Copy after login

The above is the detailed content of Simple small example of API interface development based on thinkphp6.x. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Laravel adds Api namespace
From 1970-01-01 08:00:00
0
0
0
What does the Laravel api manual do?
From 1970-01-01 08:00:00
0
0
0
javascript - About Baidu Map API calling issues
From 1970-01-01 08:00:00
0
0
0
How to get the Baidu map api
From 1970-01-01 08:00:00
0
0
0
laravel dingo/api installation and configuration
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template