jolicht / apigility-client
0.1.4
2016-05-31 10:21 UTC
Requires
- php: >=5.5
- ext-json: *
- zendframework/zend-http: ~2.3
- zendframework/zend-servicemanager: ~2.3
Requires (Dev)
- phpunit/phpunit: ~5.2
- zendframework/zend-loader: ~2.3
This package is not auto-updated.
Last update: 2019-02-20 18:52:18 UTC
README
安装
composer require jolicht/apigility-client
为 Zend Framework 2 配置
注册抽象服务工厂
将 Jolicht\\ApigilityClient\\ClientAbstractServiceFactory
注册为服务管理器中的抽象服务
'service_manager' => [
'abstract_factories' => [
'Jolicht\\ApigilityClient\\ClientAbstractServiceFactory'
]
],
配置 Apigility Http 客户端
将配置键 apigility_http_client
添加到您喜欢的配置文件中。(应用程序级别例如:'config/autoload/global.php'; 模块级别例如:config/module.config.php
'apigility_http_client' => [
'NameOfFirstClient' => [
'base_uri' => 'http://your-apigility-service-uri.dev',
'default_headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'http_client_options' => [
'timeout' => 42
],
'throw_exceptions' => false,
]
],
'NameOfSecondClient' => [
'base_uri' => 'http://your-second-apigility-service-uri.dev',
'default_headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'http_client_options' => [
'timeout' => 10
],
]
],
],
始终设置 default_headers
的 Accept
和 Content-Type
以防止内容类型错误。如果 throw_exceptions
为 true
,则会在无效的响应状态码上抛出异常。
用法
获取 Apigility http 客户端实例
使用配置的服务名称作为参数调用 ServiceManager::get
或 ServiceLocator::get
$firstClient = $serviceManager->get('NameOfFirstClient');
$secondClient = $serviceManager->get('NameOfSecondClient');
操作
创建
$response = $client->create('/api-route', $data);
检索
$response = $client->fetch('/api-route', 17);
检索所有
$response = $client->fetchAll('/api-route'); // without query data
$response = $client->fetchAll('/api-route', $queryData); // with query data
修补
$response = $client->patch('/api-route', 17, $data);
更新
$response = $client->update('/api-route', 17, $data);
调用任何 Http 方法
$response = $client->call('/api-route'); // HTTP GET
$response = $client->call('/api-route/3'); // HTTP GET
$response = $client->call('/api-route', 'GET', $data); // HTTP GET with query parameters
$response = $client->call('/api-route', 'POST', $data); // HTTP POST with content data
尚未实现
- 删除
- 删除列表
- 替换列表