これは、基本的な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ミドルウェアでハードコード化されています。適切なロジックを実装するのはあなた次第です。
$ 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 );サービスは、デフォルトで抽象的なWebServiceクラスからget 、 post 、 put 、 delete 4つの関数を継承します。これらは、データベースでCRUDアクションを実行するのに最適です。しかし、いくつかの特別なタスクを行うべきサービスが必要な場合はどうでしょうか?このような場合、HTTPリクエストメソッドPATCHを使用して、URIのアクションの名前を提供できます。
以下は、2つの数値を掛けるcal固菌サービスの例です。
使用法: 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は、次のステータスコードで応答します。これを好みに変更できます。
成功について:
さもないと:
以下は、独自のミドルウェアを構築するために使用できるパターンの例です。 Imiddlewareインターフェイスを実装するlibフォルダーにクラスを作成することにより、独自のミドルウェアを作成できます。
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 );マーティンウルフ
mit