這是使用基本HTTP身份驗證的PHP REST-API。由於基本的HTTP身份驗證已編碼但未加密,因此強烈建議使用安全連接(HTTPS)和強密碼。
如果您使用此API來處理敏感數據,則會自行冒險。
設置MySQL Server並執行example_database.sql 。在settings.php中輸入相關的mySQL參數。
$ php -S localhost:2400 -t public
默認情況下啟用身份驗證。您可以在settings.php中禁用它。
以下憑據目前在lib/Authentication Muiddrware中進行了硬編碼。由您決定適當的邏輯。
$ username = " rest " ;
$ password = " test " ;您可以在services目錄和特定版本文件夾中創建服務。服務總是從抽像類Web服務繼承。這是一個基本模板,您可以用來快速啟動:
use RESTapi Sources Request ;
use RESTapi Sources Response ;
use RESTapi Sources WebService ;
use RESTapi Library Database ;
class YourService extends WebService {
private Database | null $ db ;
public function __construct ()
{
$ this -> db = Database:: getInstance ();
}
public function get ( Request $ request , Response $ response ): void
{
// Your logic here ...
$ affectedRows = 12 ;
$ response -> write ( " JSON " ); // Add a JSON object
$ response -> addHeader ( " X-Data-Count " , $ affectedRows );
$ response -> setStatus ( 200 );
}
public function post ( Request $ request , Response $ response ): void
{
// Your logic here ...
$ affectedRows = 12 ;
$ response -> write ( " JSON " ); // Add a JSON object
$ response -> addHeader ( " X-Insert-Count " , $ affectedRows );
$ response -> setStatus ( 201 );
}
public function put ( Request $ request , Response $ response ): void
{
// Your logic here ...
$ affectedRows = 1 ;
$ response -> write ( " JSON " ); // Add a JSON object
$ response -> addHeader ( " X-Update-Count " , $ affectedRows );
$ response -> setStatus ( 204 );
}
public function delete ( Request $ request , Response $ response ): void
{
// Your logic here ...
$ affectedRows = 1 ;
$ response -> write ( " JSON " ); // Add a JSON object
$ response -> addHeader ( " X-Delete-Count " , $ affectedRows );
$ response -> setStatus ( 205 );
}
}使用數據庫:
請查看Users服務,以學習和了解如何創建查詢以執行CRUD操作。
用法: GET domain.tld/[version]/[service]
$ ch = curl_init ( " http://localhost:2400/v1/Users/ " );
curl_setopt ( $ ch , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
curl_setopt ( $ ch , CURLOPT_USERPWD , " $ username : $ password " );
curl_setopt ( $ ch , CURLOPT_HEADER , true );
if (! $ output = curl_exec ( $ ch )) {
trigger_error ( curl_error ( $ ch ));
}
curl_close ( $ ch );用法: GET domain.tld/[version]/[service]/[id]
$ ch = curl_init ( " http://localhost:2400/v1/Users/3 " );
curl_setopt ( $ ch , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
curl_setopt ( $ ch , CURLOPT_USERPWD , " $ username : $ password " );
curl_setopt ( $ ch , CURLOPT_HEADER , true );
if (! $ output = curl_exec ( $ ch )) {
trigger_error ( curl_error ( $ ch ));
}
curl_close ( $ ch );用法: POST domain.tld/[version]/[service]
$ body = [
" name " => " Greta Garbo " ,
" age " => " 93 " ,
" city " => " Hollywood " ,
" country " => " California "
];
$ ch = curl_init ( " http://localhost:2400/v1/Users " );
curl_setopt ( $ ch , CURLOPT_POST , true );
curl_setopt ( $ ch , CURLOPT_POSTFIELDS , http_build_query ( $ body ));
curl_setopt ( $ ch , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
curl_setopt ( $ ch , CURLOPT_USERPWD , " $ username : $ password " );
curl_setopt ( $ ch , CURLOPT_HEADER , true );
if (! $ output = curl_exec ( $ ch )) {
trigger_error ( curl_error ( $ ch ));
}
curl_close ( $ ch );用法: PUT domain.tld/[version]/[service]/[id]
$ body = [
" name " => " John Rambo " ,
" age " => " 42 " ,
" city " => " Seattle " ,
" country " => " Washington "
];
$ ch = curl_init ( " http://localhost:2400/v1/Users/4 " );
curl_setopt ( $ ch , CURLOPT_CUSTOMREQUEST , " PUT " );
curl_setopt ( $ ch , CURLOPT_POSTFIELDS , http_build_query ( $ body ));
curl_setopt ( $ ch , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
curl_setopt ( $ ch , CURLOPT_USERPWD , " $ username : $ password " );
curl_setopt ( $ ch , CURLOPT_HEADER , true );
if (! $ output = curl_exec ( $ ch )) {
trigger_error ( curl_error ( $ ch ));
}
curl_close ( $ ch );用法: DELETE domain.tld/[version]/[service]/[id]
$ ch = curl_init ( " http://localhost:2400/v1/Users/1 " );
curl_setopt ( $ ch , CURLOPT_CUSTOMREQUEST , " DELETE " );
curl_setopt ( $ ch , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
curl_setopt ( $ ch , CURLOPT_USERPWD , " $ username : $ password " );
curl_setopt ( $ ch , CURLOPT_HEADER , true );
if (! $ output = curl_exec ( $ ch )) {
trigger_error ( curl_error ( $ ch ));
}
curl_close ( $ ch );默認get delete ,服務從抽象網絡服務類中繼承put四個函數post這些非常適合在數據庫上執行CRUD操作。但是,如果您需要一項應該執行一些特殊任務的服務怎麼辦?在這種情況下,您可以使用HTTP請求方法PATCH ,並在URI中提供操作的名稱。
以下是二元服務的一個示例,該服務乘以兩個數字:
用法: PATCH domain.tld/[version]/[service]/[action]
$ ch = curl_init ( " http://localhost:2400/v1/Calculator/multiply " );
$ body = [
" a " => 12 ,
" b " => 2.5
];
$ ch = curl_init ( $ url );
curl_setopt ( $ ch , CURLOPT_CUSTOMREQUEST , " PATCH " );
curl_setopt ( $ ch , CURLOPT_POSTFIELDS , http_build_query ( $ body ));
curl_setopt ( $ ch , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
curl_setopt ( $ ch , CURLOPT_USERPWD , " $ username : $ password " );
curl_setopt ( $ ch , CURLOPT_HEADER , true );
if (! $ output = curl_exec ( $ ch )) {
trigger_error ( curl_error ( $ ch ));
}
curl_close ( $ ch );API響應以下狀態代碼。您可以將其更改為您的喜歡:
成功:
否則:
以下是一個示例模式,您可以用來構建自己的中間件。您可以通過在lib文件夾中創建一個實現Imiddleware接口的類中的類來創建自己的中間件:
class YourMiddleware implements IMiddleware {
public function handle ( Request $ request , Response $ response ): void {}
}要向另一個中間件注入中間件,請使用該中間件的構造函數:
class YourMiddleware implements IMiddleware {
public function __construct ( private IMiddleware $ anotherMiddleware ) {}
public function handle ( Request $ request , Response $ response ): void
{
// 1. Add your logic ...
// 2. Handle Middleware ...
$ this -> anotherMiddleware -> handle ( $ request , $ response );
// 3. Do something afterwards ...
}
}
$ anotherMiddleware = new AnotherMiddleware ();
$ yourMiddleware = new YourMiddleware ( $ anotherMiddleware );
$ yourMiddleware -> handle ( $ request , $ response );馬丁·沃爾夫
麻省理工學院