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
|
|
|
* Headers returned in the response. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $headers = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Assign dependencies. |
23
|
|
|
* |
24
|
|
|
* @param array $config |
25
|
|
|
*/ |
26
|
16 |
|
public function __construct(array $config = []) |
27
|
|
|
{ |
28
|
16 |
|
$this->config = $config; |
29
|
16 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Make a POST request. |
33
|
|
|
* |
34
|
|
|
* @param string $path |
35
|
|
|
* @param mixed $body |
36
|
|
|
* @param array $headers |
37
|
|
|
* |
38
|
|
|
* @return Request |
39
|
|
|
*/ |
40
|
8 |
|
public function post($path, $body = null, array $headers = []) |
41
|
|
|
{ |
42
|
8 |
|
return $this->request($path, $body, 'POST', $headers); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Send request with HTTP client. |
47
|
|
|
* |
48
|
|
|
* @param string $path |
49
|
|
|
* @param mixed $body |
50
|
|
|
* @param string $method |
51
|
|
|
* @param array $headers |
52
|
|
|
* |
53
|
|
|
* @return Response |
54
|
|
|
*/ |
55
|
8 |
|
protected function request($path, $body = null, $method = 'GET', array $headers = []) |
56
|
|
|
{ |
57
|
8 |
|
$request = $this->createRequest($method, $path, $body, $headers); |
58
|
8 |
|
$response = $this->send($request); |
59
|
|
|
|
60
|
8 |
|
return $response; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create request with HTTP client. |
65
|
|
|
* |
66
|
|
|
* @param string $method |
67
|
|
|
* @param string $path |
68
|
|
|
* @param mixed $body |
69
|
|
|
* @param array $headers |
70
|
|
|
* |
71
|
|
|
* @return Request |
72
|
|
|
*/ |
73
|
8 |
|
protected function createRequest($method, $path, $body = null, array $headers = []) |
74
|
|
|
{ |
75
|
8 |
|
$defaultHeaders = isset($this->config['headers']) ? $this->config['headers'] : []; |
76
|
8 |
|
return new Request( |
77
|
8 |
|
$method, |
78
|
8 |
|
$path, |
79
|
8 |
|
array_merge($defaultHeaders, $headers), |
80
|
|
|
$body |
81
|
8 |
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Send the request. |
86
|
|
|
* |
87
|
|
|
* @param Request $request |
88
|
|
|
* |
89
|
|
|
* @return Response |
90
|
|
|
*/ |
91
|
4 |
|
public function send(Request $request) |
92
|
|
|
{ |
93
|
4 |
|
$client = new CurlClient; |
94
|
4 |
|
$client->setOption(CURLOPT_URL, $this->buildUri($request->getPath())); |
95
|
4 |
|
$client->setOption(CURLOPT_RETURNTRANSFER, true); |
96
|
|
|
|
97
|
4 |
|
$headers = []; |
98
|
4 |
|
foreach ($request->getHeaders() as $key => $value) { |
99
|
2 |
|
array_push($headers, sprintf('%s: %s', $key, $value)); |
100
|
4 |
|
} |
101
|
4 |
|
$client->setOption(CURLOPT_HTTPHEADER, $headers); |
102
|
|
|
|
103
|
4 |
|
$client->setOption(CURLOPT_POST, true); |
104
|
4 |
|
$client->setOption(CURLOPT_POSTFIELDS, $request->getBody()); |
105
|
4 |
|
$client->setOption(CURLOPT_HEADERFUNCTION, [&$this, 'headerCallback']); |
106
|
|
|
|
107
|
4 |
|
if (!$result = $client->execute()) { |
108
|
2 |
|
$result = 'cURL Error: '.$client->error(); |
109
|
2 |
|
} |
110
|
|
|
|
111
|
4 |
|
$client->close(); |
112
|
|
|
|
113
|
4 |
|
return new Response(200, $this->headers, $result); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Callback to store response headers. |
118
|
|
|
* |
119
|
|
|
* @param resource $curl |
120
|
|
|
* @param string $header |
121
|
|
|
* |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
2 |
|
public function headerCallback($curl, $header) |
125
|
|
|
{ |
126
|
2 |
|
return strlen($header); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Build uri with possible base uri. |
131
|
|
|
* |
132
|
|
|
* @param string $uri |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
4 |
|
private function buildUri($uri) |
137
|
|
|
{ |
138
|
4 |
|
if (isset($this->config['base_uri'])) { |
139
|
2 |
|
return $this->config['base_uri'].$uri; |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
return $uri; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|