HasHttpRequest.php
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace Yansongda\Supports\Traits;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
/**
* Trait HasHttpRequest.
*
* @property string baseUri base_uri
* @property string timeout timeout
* @property string connectTimeout connect_timeout
*/
trait HasHttpRequest
{
/**
* Http client.
*
* @var null|Client
*/
protected $httpClient = null;
/**
* Http client options.
*
* @var array
*/
protected $httpOptions = [];
/**
* Send a GET request.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $endpoint
* @param array $query
* @param array $headers
*
* @return array|string
*/
public function get($endpoint, $query = [], $headers = [])
{
return $this->request('get', $endpoint, [
'headers' => $headers,
'query' => $query,
]);
}
/**
* Send a POST request.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $endpoint
* @param string|array $data
* @param array $options
*
* @return array|string
*/
public function post($endpoint, $data, $options = [])
{
if (!is_array($data)) {
$options['body'] = $data;
} else {
$options['form_params'] = $data;
}
return $this->request('post', $endpoint, $options);
}
/**
* Send request.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $method
* @param string $endpoint
* @param array $options
*
* @return array|string
*/
public function request($method, $endpoint, $options = [])
{
return $this->unwrapResponse($this->getHttpClient()->{$method}($endpoint, $options));
}
/**
* Set http client.
*
* @author yansongda <me@yansongda.cn>
*
* @param Client $client
*
* @return $this
*/
public function setHttpClient(Client $client)
{
$this->httpClient = $client;
return $this;
}
/**
* Get default options.
*
* @author yansongda <me@yansongda.cn>
*
* @return array
*/
protected function getOptions()
{
return array_merge([
'base_uri' => property_exists($this, 'baseUri') ? $this->baseUri : '',
'timeout' => property_exists($this, 'timeout') ? $this->timeout : 5.0,
'connect_timeout' => property_exists($this, 'connectTimeout') ? $this->connectTimeout : 5.0,
], $this->httpOptions);
}
/**
* Return http client.
*
* @return \GuzzleHttp\Client
*/
protected function getHttpClient()
{
if (is_null($this->httpClient)) {
$this->httpClient = $this->getDefaultHttpClient();
}
return $this->httpClient;
}
/**
* Get default http client.
*
* @author yansongda <me@yansongda.cn>
*
* @return Client
*/
protected function getDefaultHttpClient()
{
return new Client($this->getOptions());
}
/**
* Convert response.
*
* @author yansongda <me@yansongda.cn>
*
* @param ResponseInterface $response
*
* @return array|string
*/
protected function unwrapResponse(ResponseInterface $response)
{
$contentType = $response->getHeaderLine('Content-Type');
$contents = $response->getBody()->getContents();
if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
return json_decode($contents, true);
} elseif (false !== stripos($contentType, 'xml')) {
return json_decode(json_encode(simplexml_load_string($contents, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
}
return $contents;
}
}