emporium / emporium-api-client
该软件包最新版本(v0.2.14)没有提供许可信息。
Emporium RESTful API 客户端
v0.2.14
2017-08-22 02:10 UTC
Requires
- guzzlehttp/guzzle: ^6.2
- krak/array: ^0.4.1
Requires (Dev)
- illuminate/container: ^5.4
- illuminate/support: ^5.4
- peridot-php/peridot: ^1.18
- phake/phake: ^2.3
- pimple/pimple: ^3.0
README
PHP 客户端和Emporium API的接口。
安装
使用composer安装
{
"repositories": [
{
"type": "vcs",
"url": "git@gitlab.venue.com:emporium/php-api-client.git"
}
],
"require": {
"emporium/php-api-client": "^0.2"
}
}
使用方法
创建API客户端
<?php
$payment = Emporium\Api\Payment\PaymentApiClient::create('http://svc-payment-stg.emporium.com/', getenv('API_TOKEN'));
调用API方法
<?php
$resp = $payment->getSource(1);
更多文档请查看每个API的文档和代码库中的API接口。
一次性请求
<?php
$resp = $payment->request('GET', 'sources', [
'query' => [
'user_id' => '1',
]
]);
// this will perform a request to the API with proper authentication.
选项数组与GuzzleHttp Client支持的请求选项相同。
注意:没有前导斜杠。添加前导斜杠可能会搞乱API资源的路径。
使用响应
<?php
if ($resp->isOk()) {
} else if ($resp->isError()) {
} else if ($resp->getStatus() == 302) {
}
$body = $resp->getBody(); // returns parsed body for json or a PSR Stream
$id = $resp['id']; // accesses the parsed body
$card_number = $resp['card.number']; // access $body['card']['number']
分页结果迭代
某些API端点返回分页结果。`PagedResultIterator`提供了一种方便的方式,使用PHP迭代器迭代集合。
<?php
use Emporium\Api;
$order_api = Api\Order\OrderApiClient::create($base_uri, $token);
$orders = new Api\PagedResultIterator(function($page) use ($order_api) {
return $order_api->search('order.id < 50', 'order.created_at DESC', $with = 0, $page, $per_page = 10);
});
// this will iterate over all the results and automatically page the result set.
foreach ($orders as $order) {
}
提供商
我们支持Pimple和Laravel
Pimple
<?php
use Emporium\Api;
$c->register(new Api\Provider\Pimple\EmporiumApiServiceProvider(), [
'emporium.api.env' => 'stg', // or prd
'emporium.api.payment.token' => '', // specific token for payment api
'emporium.api.token' => '', // default token for all services
]);
$payment = $c[Api\Payment\PaymentApi::class];
除了定义参数外,还可以定义环境变量来定义每个服务的token和基础URI。
Laravel
<?php
use Emporium\Api\Provider\Laravel\EmporiumApiServiceProvider;
$app->register(EmporiumApiServiceProvider::class);
$app['emporium.api.env'] = 'stg'; // or prd
$app['emporium.api.payment.token'] = ''; // specific token for payment api
$app['emporium.api.token'] = ''; // default token for all services
$app['emporium.api.order.base_uri'] = 'https://stage.emporium.com/api/'; // specific uri for order service, (MAKE SURE IT ENDS WITH A SLASH)
除了定义参数外,还可以定义环境变量来定义每个服务的特定token和基础URI。