当前位置: 首页 > 工具软件 > PHP_MVC_REST > 使用案例 >

PHP restful api

胡夕
2023-12-01

参考

php写restful api的实例教程

框架

├─ api/
├─── config/
├────── core.php - file used for core configuration
├────── database.php - file used for connecting to the database.
├─── objects/
├────── product.php - contains properties and methods for “product” database queries.
├────── category.php - contains properties and methods for “category” database queries.
├─── product/
├────── create.php - a file that will accept posted product data to be saved to the database.
├────── delete.php - a file that will accept a product ID to delete a database record.
├────── read.php - a file that will output JSON data based on “products” database records.
├────── read_paging.php - a file that will output “products” JSON data with pagination.
├────── read_one.php - a file that will accept product ID to read a record from the database.
├────── update.php - a file that will accept a product ID to update a database record.
├────── search.php - a file that will accept keywords parameter to search “products” database.
├─── category/
├────── read.php - a file that will output JSON data based on “categories” database records.
├─── shared/
├────── utilities.php - a file that will return pagination array.
标粗是能正常运行的最小单位,集齐这三个就能做一个restful api

database.php

<?php
class Database{
  
    // specify your own database credentials
    private $host = "localhost";
    private $db_name = "db331";
    private $username = "root";
    private $password = "123456";
    // 修改上面的
    public $conn;
  
    // get the database connection
    public function getConnection(){
  
        $this->conn = null;
  
        try{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->exec("set names utf8");
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }
  
        return $this->conn;
    }
}
?>

product.php

<?php
class getPoStatus
{

    private $conn;
    private $table_name = "POs331";
	// 修改table_name
    public $poNo331;
    public $clientCompID331;
    public $datePo331;
    public $poStatus331;

    public function __construct($db)
    {
        $this->conn = $db;
    }

    function read()
    {

        $query = "SELECT
                p.poNo331, p.poStatus331
            FROM
                " . $this->table_name . " p
                
            ORDER BY
                p.poNo331 ASC";
		// 要运行的MySQL语句
        $stmt = $this->conn->prepare($query);

        $stmt->execute();

        return $stmt;
    }

}

update.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

include_once (__DIR__ . '/../config/database.php');
include_once (__DIR__ . '/../objects/getPoStatus.php');
// 链接database.php、product.php的路径
$database = new Database();
$db = $database->getConnection();

$poStatus = new getPoStatus($db);

$stmt = $poStatus->read();
$num = $stmt->rowCount();

if ($num > 0) {

    $poStatus_arr = array();
    $poStatus_arr["data"] = array();

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);

        $poStatus_item = array(
            "poNo331" => $poNo331,
            "poStatus331" => $poStatus331,
        );
		// 变量获取后台数据
        array_push($poStatus_arr["data"], $poStatus_item);
    }

    http_response_code(200);

    echo json_encode($poStatus_arr);
	// 返回json结果
} else {

    http_response_code(404);

    echo json_encode(
        array("message" => "Not found.")
    );
}

 类似资料:

相关阅读

相关文章

相关问答