这是使用基本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 );马丁·沃尔夫
麻省理工学院