PHP restful api

参考

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.")
    );
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值