|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaylyu\Alipay\Kernel; |
|
4
|
|
|
|
|
5
|
|
|
use Kaylyu\Alipay\Kernel\Http\Response; |
|
6
|
|
|
use Kaylyu\Alipay\Kernel\Traits\HasHttpRequests; |
|
7
|
|
|
use GuzzleHttp\MessageFormatter; |
|
8
|
|
|
use GuzzleHttp\Middleware; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class BaseClient. |
|
12
|
|
|
*/ |
|
13
|
|
|
class BaseClient |
|
14
|
|
|
{ |
|
15
|
|
|
use HasHttpRequests { |
|
16
|
|
|
request as performRequest; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Kaylyu\Alipay\Kernel\ServiceContainer |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $app; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $baseUri; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* BaseClient constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Kaylyu\Alipay\Kernel\ServiceContainer $app |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(ServiceContainer $app) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->app = $app; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* GET request. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $url |
|
43
|
|
|
* @param array $query |
|
44
|
|
|
* @param array $forceArrayKeys |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Kaylyu\Alipay\Kernel\Support\Collection|array|object|string |
|
47
|
|
|
* |
|
48
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidConfigException |
|
49
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
public function httpGet(string $url, array $query = [], array $forceArrayKeys = []) |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->request($url, 'GET', ['query' => $query],false, $forceArrayKeys); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* POST request. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $url |
|
60
|
|
|
* @param array $data |
|
61
|
|
|
* @param array $forceArrayKeys |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Kaylyu\Alipay\Kernel\Support\Collection|array|object|string |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidConfigException |
|
66
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\Exception |
|
67
|
|
|
*/ |
|
68
|
|
|
public function httpPost(string $url, array $data = [], array $forceArrayKeys = []) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->request($url, 'POST', ['form_params' => $data],false, $forceArrayKeys); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* JSON request. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $url |
|
77
|
|
|
* @param array $data |
|
78
|
|
|
* @param array $forceArrayKeys |
|
79
|
|
|
* |
|
80
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Kaylyu\Alipay\Kernel\Support\Collection|array|object|string |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidConfigException |
|
83
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function httpPostJson(string $url, array $data = [], array $forceArrayKeys = []) |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->request($url, 'POST', ['json' => $data], false, $forceArrayKeys ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Upload file. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $url |
|
94
|
|
|
* @param array $files |
|
95
|
|
|
* @param array $form |
|
96
|
|
|
* @param array $query |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Kaylyu\Alipay\Kernel\Support\Collection|array|object|string |
|
99
|
|
|
* |
|
100
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidConfigException |
|
101
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\Exception |
|
102
|
|
|
*/ |
|
103
|
|
|
public function httpUpload(string $url, array $files = [], array $form = [], array $query = []) |
|
104
|
|
|
{ |
|
105
|
|
|
$multipart = []; |
|
106
|
|
|
|
|
107
|
|
|
foreach ($files as $name => $path) { |
|
108
|
|
|
$multipart[] = [ |
|
109
|
|
|
'name' => $name, |
|
110
|
|
|
'contents' => fopen($path, 'r'), |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
foreach ($form as $name => $contents) { |
|
115
|
|
|
$multipart[] = compact('name', 'contents'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->request($url, 'POST', [ |
|
119
|
|
|
'query' => $query, |
|
120
|
|
|
'multipart' => $multipart, |
|
121
|
|
|
'connect_timeout' => 30, |
|
122
|
|
|
'timeout' => 30, |
|
123
|
|
|
'read_timeout' => 30 |
|
124
|
|
|
]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $url |
|
129
|
|
|
* @param string $method |
|
130
|
|
|
* @param array $options |
|
131
|
|
|
* @param bool $returnRaw |
|
132
|
|
|
* @param array $forceArrayKeys |
|
133
|
|
|
* |
|
134
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Kaylyu\Alipay\Kernel\Support\Collection|array|object|string |
|
135
|
|
|
* |
|
136
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidConfigException |
|
137
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\Exception |
|
138
|
|
|
*/ |
|
139
|
|
|
public function request(string $url, string $method = 'GET', array $options = [], $returnRaw = false, array $forceArrayKeys = []) |
|
140
|
|
|
{ |
|
141
|
|
|
if (empty($this->middlewares)) { |
|
142
|
|
|
$this->registerHttpMiddlewares(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$response = $this->performRequest($url, $method, $options); |
|
146
|
|
|
|
|
147
|
|
|
return $returnRaw ? $response |
|
148
|
|
|
: $this->castResponseToType($response, $this->app->config->get('response_type'), $forceArrayKeys); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $url |
|
153
|
|
|
* @param string $method |
|
154
|
|
|
* @param array $options |
|
155
|
|
|
* |
|
156
|
|
|
* @return \Kaylyu\Alipay\Kernel\Http\Response |
|
157
|
|
|
* |
|
158
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidConfigException |
|
159
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\Exception |
|
160
|
|
|
*/ |
|
161
|
|
|
public function requestRaw(string $url, string $method = 'GET', array $options = []) |
|
162
|
|
|
{ |
|
163
|
|
|
return Response::buildFromPsrResponse($this->request($url, $method, $options, true)); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Register Guzzle middlewares. |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function registerHttpMiddlewares() |
|
170
|
|
|
{ |
|
171
|
|
|
// log |
|
172
|
|
|
$this->pushMiddleware($this->logMiddleware(), 'log'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Log the request. |
|
177
|
|
|
* |
|
178
|
|
|
* @return \Closure |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function logMiddleware() |
|
181
|
|
|
{ |
|
182
|
|
|
$formatter = new MessageFormatter($this->app['config']['http.log_template'] ?? MessageFormatter::DEBUG); |
|
183
|
|
|
|
|
184
|
|
|
return Middleware::log($this->app['logger'], $formatter); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: