기본 HTTP 인증을 사용하는 PHP REST-API입니다. 기본 HTTP 인증은 인코딩되었지만 암호화되지 않으므로 보안 연결 (HTTP)과 강력한 암호를 사용하는 것이 좋습니다.
민감한 데이터를 처리하기 위해이 API를 사용하는 경우 자신의 위험에 따라 수행합니다.
MySQL 서버를 설정하고 example_database.sql 을 실행하십시오. settings.php 에 관련 mySQL 매개 변수를 입력하십시오.
$ php -S localhost:2400 -t public
인증은 기본적으로 활성화됩니다. settings.php 에서 비활성화 할 수 있습니다.
아래 자격 증명은 현재 lib/Authentication Middleware에서 하드 코딩됩니다. 적절한 논리를 구현하는 것은 당신에게 달려 있습니다.
$ username = " rest " ;
$ password = " test " ; services 디렉토리 및 특정 버전 폴더에서 서비스를 만듭니다. 서비스는 항상 추상 클래스 웹 서비스 에서 상속됩니다. 다음은 신속하게 시작하는 데 사용할 수있는 기본 템플릿입니다.
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 );
}
}데이터베이스 작업 :
CRUD 작업을 수행하기 위해 쿼리를 작성하는 방법을 배우고 이해하려면Users서비스를 확인하십시오.
사용법 : 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 ); 서비스는 기본적으로 초록 웹 서비스 클래스에서 4 가지 기능 put delete get 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는 다음 상태 코드로 응답합니다. 당신은 이것을 당신의 취향으로 변경할 수 있습니다 :
성공 :
그렇지 않으면:
아래는 자신의 미들웨어를 구축하는 데 사용할 수있는 예제입니다. 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