373 lines
6.7 KiB
PHP
Raw Normal View History

2020-05-21 23:08:16 +08:00
<?php
namespace catcher\library\client;
use catcher\exceptions\FailedException;
use GuzzleHttp\Client;
2020-05-22 14:08:53 +08:00
use GuzzleHttp\Promise\Promise;
2020-05-21 23:08:16 +08:00
use GuzzleHttp\TransferStats;
use Psr\Http\Message\ResponseInterface;
use function GuzzleHttp\Psr7\stream_for;
class Http
{
/**
* @var Client $client
*/
protected $client = null;
/**
* auth
*
* @var array
*/
protected $auth = [];
/**
* 代理
*
* @var array
*/
protected $proxy = [];
protected $body = [];
protected $header = [];
protected $formParams = [];
protected $query = [];
protected $json = [];
2020-05-22 15:45:14 +08:00
/**
* 可选参数
* @var array
*/
protected $options = [];
2020-05-21 23:08:16 +08:00
/**
* 异步请求
*
* @var bool
*/
protected $async = false;
2020-05-22 14:08:53 +08:00
/**
* @var array
*/
2020-05-21 23:08:16 +08:00
protected $timeout = [];
/**
2020-05-22 14:08:53 +08:00
* @var string
2020-05-21 23:08:16 +08:00
*/
2020-05-22 14:08:53 +08:00
protected $token = '';
2020-05-21 23:08:16 +08:00
2020-05-22 14:08:53 +08:00
protected $multipart = [];
2020-05-21 23:08:16 +08:00
/**
* 获取 Guzzle 客户端
*
* @time 2020年05月21日
* @return Client
*/
2020-05-22 14:08:53 +08:00
public function getClient()
2020-05-21 23:08:16 +08:00
{
if (!$this->client) {
2020-05-22 14:08:53 +08:00
$this->client = new Client;
2020-05-21 23:08:16 +08:00
}
return $this->client;
}
/**
* headers
*
* @time 2020年05月21日
* @param array $headers
* @return $this
*/
public function headers(array $headers)
{
2020-05-22 14:08:53 +08:00
$this->header = isset($this->header['headers']) ?
array_merge($this->header['headers'], $headers) :
[ 'headers' => $headers ];
return $this;
}
/**
* set bearer token
*
* @time 2020年05月22日
* @param string $token
* @return $this
*/
public function token(string $token)
{
$this->header['headers']['authorization'] = 'Bearer '. $token;
2020-05-21 23:08:16 +08:00
return $this;
}
/**
* body
*
* @time 2020年05月21日
* @param $body
* @return $this
*/
public function body($body)
{
$this->body = [
'body' => $body
];
return $this;
}
/**
* json
*
* @time 2020年05月21日
* @param array $data
* @return $this
*/
public function json(array $data)
{
$this->json = [
'json' => $data
];
return $this;
}
/**
* query
*
* @time 2020年05月21日
* @param array $query
* @return $this
*/
public function query(array $query)
{
$this->query = [
'query' => $query,
];
return $this;
}
/**
* form params
*
* @time 2020年05月21日
* @param $params
* @return $this
*/
2020-05-22 14:08:53 +08:00
public function form(array $params)
2020-05-21 23:08:16 +08:00
{
$this->formParams = [
2020-05-22 14:08:53 +08:00
'form_params' => array_merge($this->multipart, $params)
2020-05-21 23:08:16 +08:00
];
return $this;
}
/**
* timeout
*
* @time 2020年05月21日
* @param $timeout
* @return $this
*/
public function timeout($timeout)
{
$this->timeout = [
'connect_timeout' => $timeout
];
return $this;
}
2020-05-22 15:45:14 +08:00
/**
* 可选参数
*
* @time 2020年05月22日
* @param array $options
* @return $this
*/
public function options(array $options)
{
$this->options = $options;
return $this;
}
2020-05-21 23:08:16 +08:00
/**
* Request get
*
* @time 2020年05月21日
2020-05-22 15:45:14 +08:00
* @param string $url
2020-05-21 23:08:16 +08:00
* @return Response
*/
2020-05-22 14:08:53 +08:00
public function get(string $url)
2020-05-21 23:08:16 +08:00
{
2020-05-22 15:45:14 +08:00
return new Response($this->getClient()->{$this->asyncMethod(__FUNCTION__)}($url, array_merge($this->header, $this->query, $this->timeout, $this->options)));
2020-05-21 23:08:16 +08:00
}
/**
* Request post
*
* @time 2020年05月21日
* @param $url
* @return mixed
*/
2020-05-22 14:08:53 +08:00
public function post(string $url)
2020-05-21 23:08:16 +08:00
{
2020-05-22 14:08:53 +08:00
return new Response($this->getClient()->{$this->asyncMethod(__FUNCTION__)}($url, array_merge(
2020-05-22 15:45:14 +08:00
$this->header, $this->body, $this->formParams, $this->json, $this->timeout, $this->multipart,$this->options
2020-05-22 14:08:53 +08:00
)));
2020-05-21 23:08:16 +08:00
}
/**
* Request put
*
* @time 2020年05月21日
* @param $url
* @return mixed
*/
2020-05-22 14:08:53 +08:00
public function put(string $url)
2020-05-21 23:08:16 +08:00
{
2020-05-22 14:08:53 +08:00
return new Response($this->getClient()->{$this->asyncMethod(__FUNCTION__)}($url, array_merge(
2020-05-22 15:45:14 +08:00
$this->header, $this->body, $this->formParams, $this->json, $this->timeout, $this->options
2020-05-22 14:08:53 +08:00
)));
2020-05-21 23:08:16 +08:00
}
/**
* Request delete
*
* @time 2020年05月21日
* @param $url
* @return mixed
*/
2020-05-22 14:08:53 +08:00
public function delete(string $url)
2020-05-21 23:08:16 +08:00
{
2020-05-22 14:08:53 +08:00
return new Response($this->getClient()->{$this->asyncMethod(__FUNCTION__)}($url, array_merge(
2020-05-22 15:45:14 +08:00
$this->header, $this->query, $this->timeout, $this->options
2020-05-22 14:08:53 +08:00
)));
2020-05-21 23:08:16 +08:00
}
/**
* 异步请求
*
* @time 2020年05月21日
* @return $this
*/
public function async()
{
$this->async = true;
return $this;
}
2020-05-22 14:08:53 +08:00
/**
* 附件
*
* @time 2020年05月22日
* @param $name
* @param $resource
* @param $filename
* @return $this
*/
public function attach(string $name, $resource, string $filename)
{
$this->multipart = [
'multipart' => [
[
'name' => $name,
'contents' => $resource,
'filename' => $filename,
]
]
];
return $this;
}
2020-05-21 23:08:16 +08:00
/**
* 异步方法
*
* @time 2020年05月21日
* @param $method
* @return string
*/
protected function asyncMethod($method)
{
return $this->async ? $method . 'Async' : $method;
}
2020-05-22 15:45:14 +08:00
/**
* onHeaders
*
* @time 2020年05月22日
* @param callable $callable
* @return mixed
*/
public function onHeaders(callable $callable)
2020-05-21 23:08:16 +08:00
{
2020-05-22 15:45:14 +08:00
$this->options['on_headers'] = $callable;
2020-05-21 23:08:16 +08:00
2020-05-22 15:45:14 +08:00
return $this;
2020-05-21 23:08:16 +08:00
}
/**
2020-05-22 15:45:14 +08:00
* onStats
2020-05-21 23:08:16 +08:00
*
2020-05-22 15:45:14 +08:00
* @time 2020年05月22日
* @param callable $callable
2020-05-21 23:08:16 +08:00
* @return mixed
*/
2020-05-22 15:45:14 +08:00
public function onStats(callable $callable)
2020-05-21 23:08:16 +08:00
{
2020-05-22 15:45:14 +08:00
$this->options['on_stats'] = $callable;
2020-05-21 23:08:16 +08:00
2020-05-22 15:45:14 +08:00
return $this;
2020-05-21 23:08:16 +08:00
}
/**
* 认证
*
* @time 2020年04月30日
* @param $username
* @param $password
* @return $this
*/
public function auth($username, $password)
{
2020-05-22 15:56:28 +08:00
$this->options = [
'auth' => $username, $password
];
2020-05-21 23:08:16 +08:00
return $this;
}
/**
* proxy
*
* @time 2020年05月21日
* @param array $proxy
* @return $this
*/
public function proxy(array $proxy)
{
$this->proxy = $proxy;
return $this;
}
}