sj_royd/http_service
Composer 安装命令:
composer require sj_royd/http_service
包简介
HTTP service for REST and SOAP services
README 文档
README
Simple library for HTTP requests via REST or SOAP service.
Usage
SOAP service
Required in PHP
- ext-simplexml
- ext-soap
<?php
// optional Client
class Client extends \SoapClient
{
/**
* @var string
*/
private $headerNs = 'http://www.w3.org/2005/08/addressing';
/**
* @var resource
*/
private $context;
public function __construct($wsdl, array $options = [])
{
$this->context = stream_context_create();
$options['stream_context'] = $this->context;
parent::__construct($wsdl, $options);
}
/**
* @param string $action
* @param array $args
* @param null $options
* @param null $in_heads
* @param null $out_heads
*
* @return mixed
*/
public function __soapCall($action, $args, $options = null, $in_heads = null, &$out_heads = null)
{
$in_heads = $this->getRequestHeaders($action);
return parent::__soapCall($action, [$args], $options, $in_heads, $out_heads);
}
/**
* @param string $request
* @param string $location
* @param string $action
* @param int $version
* @param int $one_way
*
* @return string
*/
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
return stristr(stristr($response, '<s:'), '</s:Envelope>', true) . '</s:Envelope>';;
}
/**
* @param $myData
*/
public function setMyHeader($myData)
{
stream_context_set_option($this->context, [
'http' => ['header' => "my-data: {$myData}"]
]);
}
/**
* @param $data
*
* @return array
*/
private function getRequestHeaders($data)
{
return [
new \SoapHeader($this->headerNs, 'Name', $data)
];
}
}
class MyService extends SJRoyd\HTTPService\SoapRequest
{
protected $ws_path = 'https://sample.service.com';
protected $ws_name = 'service1'; // https://sample.service.com/service1
protected $soapVersion = SOAP_1_1; // default SOAP_1_2
protected $wsdl = true; // default
protected $wsdl_config = []; // SOAP WSDL options
// optional Client injection
public function __construct($test = false, $debug = false)
{
parent::__construct($test, $debug, Client::class);
$this->client->__setLocation('My_Location_Data');
}
public function callAction()
{
$this->client->setMyHeader('some-data');
$body = []; // action body params as array or an array convertible object
$cast = [
200 => Some\Response\Class::class,
'4..' => Some\Response\Error::class // any 400+ status code response
];
$response = $this->call('actionName', $body, $cast);
}
}
REST service
Required in PHP
- ext-simplexml (optionally if XML response)
<?php
class MyService extends SJRoyd\HTTPService\RestRequest
{
protected $ws_path = 'https://sample.service.com/api';
}
class MyServiceAction extends MyService
{
protected $ws_name = 'service1'; // https://sample.service.com/api/service1
public function getAction()
{
$response = $this->callGet('getAction', [], [
200 => Some\Response\Unit::class,
401 => Some\Response\Error\Unauthorized::class,
'4..' => Some\Response\Error::class // any 400+ status code response
]);
if($response instanceof \Exception){
throw new \Exception($response->getMessage(), $this->responseStatusCode);
}
return $response;
}
public function putAction(array $data){
$params = [ // Guzzle request params
'json' => $data
];
$cast = [
401 => Some\Response\Error\Unauthorized::class,
'5..' => Some\Response\Error::class // any 500+ status code response
];
$response = $this->callPut('putAction', $params, $cast);
if($response instanceof \Exception){
throw new \Exception($response->getMessage(), $this->responseStatusCode);
}
return true;
}
public function postAction(array $data){
$params = [ // Guzzle request params
'method' => 'POST',
'json' => $data
];
$cast = [
200 => Some\Response\Unit::class,
401 => Some\Response\Error\Unauthorized::class,
'5..' => Some\Response\Error::class // any 500+ status code response
];
$response = $this->call('postAction', $params, $cast);
if($response instanceof \Exception){
throw new \Exception($response->getMessage(), $this->responseStatusCode);
}
return $response;
}
}
RestRequest have methods for GET, POST, PUT, PATCH, and DELETE request and they calls callGet(), callPost(), callPut(), callPatch() and callDelete(). You can also use the call() method and send HTTP method in params array like $params = ['method' => 'PUT'] but default method is GET.
PHP 8.x named arguments
<?php
class MyService extends SJRoyd\HTTPService\RestRequest
{
protected $ws_path = 'https://sample.service.com/api';
}
class MyServiceAction extends MyService
{
protected $ws_name = 'service1'; // https://sample.service.com/api/service1
public function getAction()
{
$response = $this->callGet( // call GET https://sample.service.com/api/service1/getAction
action: 'getAction',
cast: [
200 => Some\Response\Unit::class,
401 => Some\Response\Error\Unauthorized::class,
'4..' => Some\Response\Error::class // any 400+ status code response
]
);
if ($this->responseStatusCode == 200) {
return $response;
} else {
throw $response;
}
return $response;
}
public function putAction(array $data)
{
$response = $this->callPut( // call PUT https://sample.service.com/api/service1/putAction
action: 'putAction',
params: [ // Guzzle request params
'json' => $data
],
cast: [
401 => Some\Response\Error\Unauthorized::class,
'5..' => Some\Response\Error::class // any 500+ status code response
]
);
if ($this->responseStatusCode == 200) {
return true;
} else {
throw $response;
}
}
public function postAction(array $data)
{
$params = [ // Guzzle request params
'method' => 'POST',
'json' => $data
];
$cast = [
200 => Some\Response\Unit::class,
401 => Some\Response\Error\Unauthorized::class,
'5..' => Some\Response\Error::class // any 500+ status code response
];
$response = $this->call(action: 'postAction', params: $params, cast: $cast); // call POST https://sample.service.com/api/service1/postAction
if ($this->responseStatusCode == 200) {
return $response;
} else {
throw $response;
}
}
public function defaultAction(array data)
{
$response = $this->callPost( // call POST https://sample.service.com/api/service1
cast: [
401 => Some\Response\Error\Unauthorized::class,
'400|404' => Some\Response\Error::class // 400 and 404 status code response
]
);
if ($this->responseStatusCode == 200) {
return $response; // raw data
} else {
throw $response;
}
return $response;
}
}
Get response raw data
After call any of service actions the raw response is always be available by calling the getRawData() method.
<?php
$service->getAction(); // Some\Response\Unit instance
$service->getRawData(); // The raw data before casting to the object
sj_royd/http_service 适用场景与选型建议
sj_royd/http_service 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 89 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「service」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sj_royd/http_service 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sj_royd/http_service 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sj_royd/http_service 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
A fast and intuitive dependency injection container.
Registry service.
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Service auto install for Nette Framework
Structure for the Laravel Service-Repository Pattern
统计信息
- 总下载量: 89
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-08