iamahless / crs-request
轻松发送HTTP请求到CRS路由器。
v2
2020-12-25 10:49 UTC
Requires
- php: >=7.2
- guzzlehttp/guzzle: ^7.0.1
This package is not auto-updated.
Last update: 2024-09-29 05:49:02 UTC
README
轻松地向HNG CRS api发送请求。
基本用法
从composer拉取包 "iamahless/crs-request":"1.*"
。就这么简单。以下是一个示例实现。
<?php
// Helper functions.... you can implement this however you want...
function saveCrsApiSession(array $session) {
$path = 'path/to/storage/accesstoken.txt';
file_put_contents($path, json_encode($session));
}
function getCrsApiSession() {
$path = 'path/to/storage/accesstoken.txt';
if (file_exists($path)) {
return json_decode(file_get_contents($path), true);
}
return [];
}
// Using Lumen Router...
try {
// Configuration details...
$config = [
'base_url' => 'http://crsapi.dev',
'client_id' => '12345',
'client_secret' => 'SUPER_SECRET_CLIENT_SECRET',
'scopes' => ['scope1', 'scope2'],
];
// Use Guzzle...
$guzzle = new HNG\Http\GuzzleRequest($config);
// Get the stored session details and set it to the request class,
// This is to avoid multiple calls to the service for authentication.
$previouslyStoredSession = getCrsApiSession();
// Create a request instance...
$request = new HNG\Http\Request($guzzle, $config, $previouslyStoredSession);
$bookings = $request->get('/bookings');
saveCrsApiSession((array)$request->getSession());
} catch (\Exception $e) {
// This should not really happen except your credentials are wrong...
throw $e;
}
调试
每次发生错误时,错误详细信息都会记录在上述存储路径的log
目录中。以下是一个记录信息的示例
[2016-12-05 03:16:52]
Endpoint: http://api.somesite.com/posts
Error: cURL error 56: Problem (2) in the Chunked-Encoded data (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)