http request请求处理类
阿祖
2019-04-18 19:22:40
访问量: 303
TAG:
php
curl
laravel
<?php
/**
* http request 处理类库
* User: zucheng
* Date: 2019/4/12
*/
namespace App\Traits;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
trait HasHttpRequest
{
/**
* get请求
* @param string $endpoint 补全url
* @param array $params 请求参数
* @param array $headers 请求头
* @return Object
* @throws CannotParseResponseException
* @throws HttpException
*/
protected function get($endpoint, $params, $headers = [])
{
return $this->request('get', $endpoint, [
'header' => $headers,
'query' => $params
]);
}
/**
* post请求
* @param string $endpoint 补全url
* @param array|string $params
* @param array ...$options
* @return Object
* @throws CannotParseResponseException
* @throws HttpException
*/
protected function post($endpoint, $params = [], ...$options)
{
if (is_array($params))
$options['form_params'] = $params;
else
$options['body'] = $params;
return $this->request('post', $endpoint, !empty($options) ? $options : []);
}
/**
* delete方式请求
* @param string $endpoint 补全url
* @param array $params 请求参数
* @param array $headers 请求头
* @return Object
* @throws CannotParseResponseException
* @throws HttpException
*/
protected function delete($endpoint, $params, $headers = [])
{
return $this->request('delete', $endpoint, [
'header' => $headers,
'query' => $params
]);
}
/**
* patch方式请求
* @param $endpoint
* @param array $params
* @param array ...$options
* @return Object
* @throws CannotParseResponseException
* @throws HttpException
*/
protected function patch($endpoint, $params = [], ...$options)
{
if (is_array($params))
$options['form_params'] = $params;
else
$options['body'] = $params;
return $this->request('patch', $endpoint, !empty($options) ? $options : []);
}
/**
* 获取http对象
* @param array $config
* @return Client
*/
protected function getHttpClient(array $config)
{
return new Client($config);
}
/**
* api请求操作
* @param string $method
* @param string $endpoint
* @param array $options
* @return Object
* @throws CannotParseResponseException
* @throws HttpException
*/
protected function request($method, $endpoint, $options)
{
$uri = method_exists($this, "setUri") ? $this->setUri($endpoint) : $endpoint;
return $this->unwrapResponse($this->getHttpClient($this->getBaseOptions())->{$method}($uri, $options));
}
/**
* 基础配置
* @return array
*/
protected function getBaseOptions()
{
$options = [
'base_uri' => method_exists($this, 'getBaseUri') ? $this->getBaseUri() : '',
'timeout' => property_exists($this, 'timeout') ? $this->timeout : 5.0,
];
return $options;
}
/**
* 转换返回数据为Result对象
* @param ResponseInterface $response
* @return mixed
* @throws \Exception
*/
protected function unwrapResponse(ResponseInterface $response)
{
$contentType = $response->getHeaderLine('Content-Type');
$contents = $response->getBody()->getContents();
if ($response->getStatusCode() != 200) {
throw new \Exception($response->getStatusCode());
}
if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
return json_decode($contents);
} elseif (false !== stripos($contentType, 'xml')) {
return json_decode(json_encode(simplexml_load_string($contents)));
}
throw new \Exception('解析结果失败');
}
}