Simple API Development Example Using ThinkPHP 6.x

This article presents a step‑by‑step tutorial for PHP beginners on building a simple product‑detail API with ThinkPHP 6.x, covering front‑end HTML form, request‑side controller using cURL, and API controller that queries a database and returns JSON data.

php Courses
php Courses
php Courses
Simple API Development Example Using ThinkPHP 6.x

This tutorial provides a straightforward example for PHP beginners to create an API that returns product details based on a product ID submitted from the front end, without any authentication, using the ThinkPHP 6.x framework.

Step 1: Front‑end code (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: Request‑side controller (controller/index.php)

<?php
namespace app\controller;
use app\BaseController;
class Index extends BaseController {
    // front‑end view
    public function index() {
        return view();
    }
    // submission entry point
    public function api_chaxun() {
        // http request
        $url = 'http://localhost/index.php/index/goods/api/';
        // input('goods_id') is the name value from the front‑end form
        $ch = curl_init($url . '?goods_id=' . input('goods_id'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // execute and store result in $data
        $data = curl_exec($ch);
        // close connection
        curl_close($ch);
        // return data
        return $data;
    }
}
?>

Step 3: API controller (controller/goods.php)

<?php
namespace app\controller;
use app\BaseController;
use think\facade\Db;
class Goods extends BaseController {
    /**
     * Client submits product ID (goods_id) to the API
     * API returns the product information
     */
    public function api($goods_id = 1) {
        // query database and assign to $data
        $data = Db::name('goods')->where('id', $goods_id)->find();
        // return data as JSON
        return json($data); // print_r($data);
    }
}
?>

The article also includes a link to the original source and several illustrative images.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPAPIThinkPHP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.