
一步步教你進(jìn)行 Python REST API 身份驗(yàn)證
為了存儲(chǔ)博客文章和分類信息,我們需要先配置數(shù)據(jù)庫(kù)。以下是 SQL 結(jié)構(gòu):
-- posts表結(jié)構(gòu)
CREATE TABLE posts (
id INT PRIMARY KEY AUTO_INCREMENT,
category_id INT,
title VARCHAR(255),
body TEXT,
author VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- categories表結(jié)構(gòu)
CREATE TABLE categories (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
按照以下步驟進(jìn)行數(shù)據(jù)庫(kù)設(shè)置:
my_blog
的數(shù)據(jù)庫(kù)。接下來(lái),創(chuàng)建以下文件夾和文件結(jié)構(gòu):
php_rest_myblog/
├── config/
│ └── Database.php
├── models/
│ └── Post.php
├── api/
│ └── posts/
│ └── read.php
Database
類在 config/Database.php
中,創(chuàng)建用于連接數(shù)據(jù)庫(kù)的 Database 類:
< ?php
class Database {
private $host = "localhost";
private $dbname = "my_blog";
private $username = "root";
private $password = "";
private $conn;
public function connect() {
$this- > conn = null;
try {
$this- > conn = new PDO(
"mysql:host=" . $this- > host . ";dbname=" . $this- > dbname,
$this- > username,
$this- > password
);
$this- > conn- > setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection Error: " . $e- > getMessage();
}
return $this- > conn;
}
}
? >
Post
模型類
在 models/Post.php
中,創(chuàng)建處理文章數(shù)據(jù)的 Post 模型類:
< ?php
class Post {
private $conn;
private $table = "posts";
public $id;
public $category_id;
public $title;
public $body;
public $author;
public $created_at;
public function __construct($db) {
$this- > conn = $db;
}
public function read() {
$query = "SELECT c.name AS category_name, p.id, p.category_id, p.title, p.body, p.author, p.created_at
FROM " . $this- > table . " p
LEFT JOIN categories c ON p.category_id = c.id
ORDER BY p.created_at DESC";
$stmt = $this- > conn- > prepare($query);
$stmt- > execute();
return $stmt;
}
}
? >
在 api/posts/read.php
中,創(chuàng)建 REST API 端點(diǎn)來(lái)讀取文章數(shù)據(jù):
< ?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once '../../config/Database.php';
include_once '../../models/Post.php';
$database = new Database();
$db = $database- > connect();
$post = new Post($db);
$result = $post- > read();
$num = $result- > rowCount();
if ($num > 0) {
$posts_arr = array();
$posts_arr['data'] = array();
while ($row = $result- > fetch(PDO::FETCH_ASSOC)) {
extract($row);
$post_item = array(
'id' = > $id,
'title' = > $title,
'body' = > html_entity_decode($body),
'author' = > $author,
'category_id' = > $category_id,
'category_name' = > $category_name
);
array_push($posts_arr['data'], $post_item);
}
echo json_encode($posts_arr);
} else {
echo json_encode(array('message' = > 'No Posts Found'));
}
? >
通過(guò) Postman 測(cè)試 API:
http://localhost/php_rest_myblog/api/posts/read.php
。如果配置無(wú)誤,你將看到返回的 JSON 數(shù)據(jù),包含所有文章及其分類信息。
通過(guò)本文,你已經(jīng)學(xué)會(huì)如何使用 純PHP 創(chuàng)建 RESTful API。這種從零開(kāi)始的方式雖然更為繁瑣,但能幫助你深入理解 Web 開(kāi)發(fā)的核心概念。希望你能從中獲得啟發(fā),打造更復(fù)雜的項(xiàng)目!
原文引自YouTube視頻:https://www.youtube.com/watch?v=OEWXbpUMODk
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)