forked from wenbin1989/yii2-curl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Curl.php
206 lines (185 loc) · 6.23 KB
/
Curl.php
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace wenbin1989\yii2\curl;
use Yii;
use yii\base\Component;
use yii\base\Exception;
use yii\helpers\Json;
use yii\web\HttpException;
/**
* cURL wrapper for yii2 framework.
*
* @author Wenbin Wang <[email protected]>
*/
class Curl extends Component
{
/**
* @var float timeout to use for http request.
* This value will be used to configure the curl `CURLOPT_CONNECTTIMEOUT` option.
* If not set, no explicit timeout will be set for curl.
*/
public $connectionTimeout = null;
/**
* @var float timeout to use when reading the http response.
* This value will be used to configure the curl `CURLOPT_TIMEOUT` option.
* If not set, no explicit timeout will be set for curl.
*/
public $dataTimeout = null;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
}
/**
* Performs GET HTTP request
*
* @param string $url URL
* @param array $options URL options
* @param string $body request body
* @param boolean $raw if response body contains JSON and should be decoded
* @return mixed response
*/
public function get($url, $options = [], $body = null, $raw = true)
{
return $this->httpRequest('GET', $this->createUrl($url, $options), $body, $raw);
}
/**
* Performs HEAD HTTP request
*
* @param string $url URL
* @param array $options URL options
* @param string $body request body
* @return mixed response
*/
public function head($url, $options = [], $body = null)
{
return $this->httpRequest('HEAD', $this->createUrl($url, $options), $body);
}
/**
* Performs POST HTTP request
*
* @param string $url URL
* @param array $options URL options
* @param string $body request body
* @param boolean $raw if response body contains JSON and should be decoded
* @return mixed response
*/
public function post($url, $options = [], $body = null, $raw = true)
{
return $this->httpRequest('POST', $this->createUrl($url, $options), $body, $raw);
}
/**
* Performs PUT HTTP request
*
* @param string $url URL
* @param array $options URL options
* @param string $body request body
* @param boolean $raw if response body contains JSON and should be decoded
* @return mixed response
*/
public function put($url, $options = [], $body = null, $raw = true)
{
return $this->httpRequest('PUT', $this->createUrl($url, $options), $body, $raw);
}
/**
* Performs DELETE HTTP request
*
* @param string $url URL
* @param array $options URL options
* @param string $body request body
* @param boolean $raw if response body contains JSON and should be decoded
* @return mixed response
*/
public function delete($url, $options = [], $body = null, $raw = true)
{
return $this->httpRequest('DELETE', $this->createUrl($url, $options), $body, $raw);
}
/**
* Creates URL
*
* @param mixed $path path
* @param array $options URL options
* @return string
*/
private function createUrl($path, $options = [])
{
if (!is_string($path)) {
$url = implode('/', array_map(function ($a) {
return urlencode(is_array($a) ? implode(',', $a) : $a);
}, $path));
if (!empty($options)) {
$url .= '?' . http_build_query($options);
}
} else {
$url = $path;
if (!empty($options)) {
$url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($options);
}
}
return $url;
}
/**
* Performs HTTP request
*
* @param string $method method name
* @param string $url URL
* @param string $requestBody request body
* @param boolean $raw if response body contains JSON and should be decoded
* @throws Exception if request failed
* @throws HttpException
* @return mixed if response http code is 404, return false; if http code >= 200 and < 300, return response body;
* throws HttpException for other http code.
*/
protected function httpRequest($method, $url, $requestBody = null, $raw = false)
{
$method = strtoupper($method);
// response body
$body = '';
$options = [
CURLOPT_USERAGENT => 'Yii Framework 2 ' . __CLASS__,
CURLOPT_RETURNTRANSFER => false,
CURLOPT_HEADER => false,
// http://www.php.net/manual/en/function.curl-setopt.php#82418
CURLOPT_HTTPHEADER => ['Expect:'],
CURLOPT_WRITEFUNCTION => function ($curl, $data) use (&$body) {
$body .= $data;
return mb_strlen($data, '8bit');
},
CURLOPT_CUSTOMREQUEST => $method,
];
if ($this->connectionTimeout !== null) {
$options[CURLOPT_CONNECTTIMEOUT] = $this->connectionTimeout;
}
if ($this->dataTimeout !== null) {
$options[CURLOPT_TIMEOUT] = $this->dataTimeout;
}
if ($requestBody !== null) {
$options[CURLOPT_POSTFIELDS] = $requestBody;
}
if ($method == 'HEAD') {
$options[CURLOPT_NOBODY] = true;
unset($options[CURLOPT_WRITEFUNCTION]);
}
$profile = $method . ' ' . $url . '#' . md5(serialize($requestBody));
Yii::trace("Sending request: $url\n" . Json::encode($requestBody), __METHOD__);
Yii::beginProfile($profile, __METHOD__);
$curl = curl_init($url);
curl_setopt_array($curl, $options);
if (curl_exec($curl) === false) {
throw new Exception('curl request failed: ' . curl_error($curl) , curl_errno($curl));
}
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
Yii::endProfile($profile, __METHOD__);
if ($responseCode >= 200 && $responseCode < 300) {
if ($method == 'HEAD') {
return true;
}
return $raw ? $body : Json::decode($body);
} elseif ($responseCode == 404) {
return false;
}
throw new HttpException($responseCode, $body);
}
}