alesanchezr / slim-api-wrapper
此包最新版本(0.0.4)没有提供许可证信息。
快速构建Slim API
0.0.4
2019-05-06 16:08 UTC
Requires
- php: >=5.3.3
- firebase/php-jwt: ^5.0
- monolog/monolog: ^1.24
- phpunit/phpunit: ^6.5
- respect/validation: ^1.1
- slim/slim: ^3.0
This package is auto-updated.
Last update: 2024-09-09 12:47:07 UTC
README
一个简单的slim包装器,用于避免每次启动新API时重复做同样的事情。
此包非常适合用于微框架架构,其中API通过多个独立的服务器/开发进行分发。
安装
$ composer require alesanchezr/slim-api-wrapper
如果您打算使用授权头,您必须允许Apache在您的.htaccess
中使用HTTP头。
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
1分钟内创建API 🧐
以下是一个如何使用单个GET /hello
端点创建简单API的示例
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; require("./vendor/autoload.php"); $api = new \SlimAPI\SlimAPI([ 'name' => 'My Super Duper API', 'debug' => true ]); $api->get('/hello', function (Request $request, Response $response, array $args) use ($api) { return $response->withJson(["Hello World"]); });
📝 为API添加README
添加一个markdown编写的README.md文件是一种好习惯,只需调用$api->addReadme()
方法,指定用户希望访问的README.md
的URI即可。
// here users will GET to path /readme in order to read the readme
$api->addReadme('/readme');
//here users will GET to the root to read the readme file, but you can also specify the name of your readme file.
$api->addReadme('/','./INSTRUCTIONS.md');
💻 添加更多端点
API在后台使用Slim PHP 3.0,您可以根据Slim文档添加任意数量的端点。
💡 这里有一些您可以使用示例列表。
🔑 JWT身份验证
- 要创建私有/认证端点,只需在端点末尾添加
->add($inst->auth());
,如下所示
$inst->app->get('/hello/private', function (Request $request, Response $response, array $args){ return $response->withJson([ "private" => "object" ]); })->add($inst->auth()); //here I say I want this endpoint to be private
- 将一个密钥种子添加到API中,这将被用作令牌生成的盐,您只需执行此步骤一次。
// adding an internal seed for random private key generation // this only has to be done once in the entire API $api->setJWTKey("adSAD43gtterT%rtwre32@");
- 至少添加一个客户端到API中,您可以挑选一个用户名,但密钥必须使用
generatePrivateKey
方法生成。
// pick any username you like for the JWT credentials $clientId = "alesanchezr"; // generate a key based on that username $clientKey = $api->generatePrivateKey($clientId);
- 现在您可以对任何请求进行调用,但您必须在请求的
Authorization
头或作为查询字符串上的access_token
添加密钥。
使用查询字符串进行身份验证
//here is an example in Javascript using QueryString autentication fetch('https://my_api.com/path/to/endpoint?access_token=ddsfs#@$fsd3425Ds') .then(resp => { //if the token is wrong you will recive status == 403 if(resp.status == 403) console.error("You have a wrong access_token token"); else if(resp.ok) return resp.json() else console.error("Uknown problem on the API"); }) .then(data => console.log(data)) .catch(err => console.error("There is a problem on the front-end or the API is down"))
使用Authorization
头进行身份验证
//here is an example in Javascript using QueryString autentication fetch('https://my_api.com/path/to/endpoint', { 'method': 'POST', 'headers': { 'Content-Type': '' 'Authorization': 'JWT asdA@SDad!sdASASDsd453453SDF43' }, 'body': JSON.stringify(data) }) .then(resp => { //if the token is wrong you will recive status == 403 if(resp.status == 403) console.error("You have a wrong access_token token"); else if(resp.ok) return resp.json() else console.error("Uknown problem on the API"); }) .then(data => console.log(data)) .catch(err => console.error("There is a problem on the front-end or the API is down"))
附加信息
运行测试
./vendor/bin/phpunit example/with_tests/tests.php --colors