|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Artstorm\MonkeyLearn\HttpClient; |
|
4
|
|
|
|
|
5
|
|
|
class HttpClient implements HttpClientInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Client config. |
|
9
|
|
|
* |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $config; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Assign dependencies. |
|
16
|
|
|
* |
|
17
|
|
|
* @param array $config |
|
18
|
|
|
*/ |
|
19
|
16 |
|
public function __construct(array $config = []) |
|
20
|
|
|
{ |
|
21
|
16 |
|
$this->config = $config; |
|
22
|
16 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Make a POST request. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $path |
|
28
|
|
|
* @param mixed $body |
|
29
|
|
|
* @param array $headers |
|
30
|
|
|
* |
|
31
|
|
|
* @return Request |
|
32
|
|
|
*/ |
|
33
|
8 |
|
public function post($path, $body = null, array $headers = []) |
|
34
|
|
|
{ |
|
35
|
8 |
|
return $this->request($path, $body, 'POST', $headers); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Send request with HTTP client. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $path |
|
42
|
|
|
* @param mixed $body |
|
43
|
|
|
* @param string $method |
|
44
|
|
|
* @param array $headers |
|
45
|
|
|
* |
|
46
|
|
|
* @return Response |
|
47
|
|
|
*/ |
|
48
|
8 |
|
protected function request($path, $body = null, $method = 'GET', array $headers = []) |
|
49
|
|
|
{ |
|
50
|
8 |
|
$request = $this->createRequest($method, $path, $body, $headers); |
|
51
|
8 |
|
$response = $this->send($request); |
|
52
|
|
|
|
|
53
|
8 |
|
return $response; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Create request with HTTP client. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $method |
|
60
|
|
|
* @param string $path |
|
61
|
|
|
* @param mixed $body |
|
62
|
|
|
* @param array $headers |
|
63
|
|
|
* |
|
64
|
|
|
* @return Request |
|
65
|
|
|
*/ |
|
66
|
8 |
|
protected function createRequest($method, $path, $body = null, array $headers = []) |
|
67
|
|
|
{ |
|
68
|
8 |
|
$defaultHeaders = isset($this->config['headers']) ? $this->config['headers'] : []; |
|
69
|
8 |
|
return new Request( |
|
70
|
8 |
|
$method, |
|
71
|
8 |
|
$path, |
|
72
|
8 |
|
array_merge($defaultHeaders, $headers), |
|
73
|
|
|
$body |
|
74
|
8 |
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Send the request. |
|
79
|
|
|
* |
|
80
|
|
|
* @param Request $request |
|
81
|
|
|
* |
|
82
|
|
|
* @return Response |
|
83
|
|
|
*/ |
|
84
|
4 |
|
public function send(Request $request) |
|
85
|
|
|
{ |
|
86
|
4 |
|
$client = new CurlClient; |
|
87
|
4 |
|
$client->setOption(CURLOPT_URL, $this->buildUri($request->getPath())); |
|
88
|
4 |
|
$client->setOption(CURLOPT_RETURNTRANSFER, true); |
|
89
|
|
|
|
|
90
|
4 |
|
$headers = []; |
|
91
|
4 |
|
foreach ($request->getHeaders() as $key => $value) { |
|
92
|
2 |
|
array_push($headers, sprintf('%s: %s', $key, $value)); |
|
93
|
4 |
|
} |
|
94
|
4 |
|
$client->setOption(CURLOPT_HTTPHEADER, $headers); |
|
95
|
|
|
|
|
96
|
4 |
|
$client->setOption(CURLOPT_POST, true); |
|
97
|
4 |
|
$client->setOption(CURLOPT_POSTFIELDS, $request->getBody()); |
|
98
|
|
|
|
|
99
|
4 |
|
if (!$result = $client->execute()) { |
|
100
|
2 |
|
$result = 'cURL Error: '.$client->error(); |
|
101
|
2 |
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
$client->close(); |
|
104
|
|
|
|
|
105
|
4 |
|
return new Response(200, [], $result); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Build uri with possible base uri. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $uri |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
4 |
|
private function buildUri($uri) |
|
116
|
|
|
{ |
|
117
|
4 |
|
if (isset($this->config['base_uri'])) { |
|
118
|
2 |
|
return $this->config['base_uri'].$uri; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
return $uri; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|