Simple small example of API interface development based on thinkphp6.x
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!
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>
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 = 'http://localhost/index.php/index/goods/api/'; // input('goods_id') 是前端的from传过来的name值 $ch = curl_init($url.'?goods_id='.input('goods_id')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 执行 并把执行后的数据赋值给 $data $data = curl_exec($ch); // 关闭 curl_close($ch); // 返回数据 return $data; } }
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('goods')->where('id',$goods_id)->find(); // 返回数据 return json($data);//print_r($data); } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
