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
|
<?php
namespace Twilio\Http;
use Twilio\Exceptions\EnvironmentException;
class CurlClient implements Client {
const DEFAULT_TIMEOUT = 60;
protected $curlOptions = array();
public function __construct(array $options = array()) {
$this->curlOptions = $options;
}
public function request($method, $url, $params = array(), $data = array(),
$headers = array(), $user = null, $password = null,
$timeout = null) {
$options = $this->options($method, $url, $params, $data, $headers,
$user, $password, $timeout);
try {
if (!$curl = curl_init()) {
throw new EnvironmentException('Unable to initialize cURL');
}
if (!curl_setopt_array($curl, $options)) {
throw new EnvironmentException(curl_error($curl));
}
if (!$response = curl_exec($curl)) {
throw new EnvironmentException(curl_error($curl));
}
$parts = explode("\r\n\r\n", $response, 3);
list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')
? array($parts[1], $parts[2])
: array($parts[0], $parts[1]);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$responseHeaders = array();
$headerLines = explode("\r\n", $head);
array_shift($headerLines);
foreach ($headerLines as $line) {
list($key, $value) = explode(':', $line, 2);
$responseHeaders[$key] = $value;
}
curl_close($curl);
if (isset($buffer) && is_resource($buffer)) {
fclose($buffer);
}
return new Response($statusCode, $body, $responseHeaders);
} catch (\ErrorException $e) {
if (isset($curl) && is_resource($curl)) {
curl_close($curl);
}
if (isset($buffer) && is_resource($buffer)) {
fclose($buffer);
}
throw $e;
}
}
public function options($method, $url, $params = array(), $data = array(),
$headers = array(), $user = null, $password = null,
$timeout = null) {
$timeout = is_null($timeout)
? self::DEFAULT_TIMEOUT
: $timeout;
$options = $this->curlOptions + array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_INFILESIZE => -1,
CURLOPT_HTTPHEADER => array(),
CURLOPT_TIMEOUT => $timeout,
);
foreach ($headers as $key => $value) {
$options[CURLOPT_HTTPHEADER][] = "$key: $value";
}
if ($user && $password) {
$options[CURLOPT_HTTPHEADER][] = 'Authorization: Basic ' . base64_encode("$user:$password");
}
$body = $this->buildQuery($params);
if ($body) {
$options[CURLOPT_URL] .= '?' . $body;
}
switch (strtolower(trim($method))) {
case 'get':
$options[CURLOPT_HTTPGET] = true;
break;
case 'post':
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $this->buildQuery($data);
break;
case 'put':
$options[CURLOPT_PUT] = true;
if ($data) {
if ($buffer = fopen('php://memory', 'w+')) {
$dataString = $this->buildQuery($data);
fwrite($buffer, $dataString);
fseek($buffer, 0);
$options[CURLOPT_INFILE] = $buffer;
$options[CURLOPT_INFILESIZE] = strlen($dataString);
} else {
throw new EnvironmentException('Unable to open a temporary file');
}
}
break;
case 'head':
$options[CURLOPT_NOBODY] = true;
break;
default:
$options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
}
return $options;
}
public function buildQuery($params) {
$parts = array();
$params = $params ?: array();
foreach ($params as $key => $value) {
if (is_array($value)) {
foreach ($value as $item) {
$parts[] = urlencode((string)$key) . '=' . urlencode((string)$item);
}
} else {
$parts[] = urlencode((string)$key) . '=' . urlencode((string)$value);
}
}
return implode('&', $parts);
}
}
|