-
Notifications
You must be signed in to change notification settings - Fork 18
/
Curl.php
executable file
·217 lines (182 loc) · 6.37 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
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* ObjectStorage_Http_Client that uses CURL
*
* @package ObjectStorage-Client
* @copyright Copyright (c) 2012 SoftLayer Technologies Inc. (http://www.softlayer.com)
*/
class ObjectStorage_Http_Adapter_Curl implements ObjectStorage_Http_Adapter_Interface
{
protected $uri;
protected $headers;
protected $body;
protected $method;
protected $timeout = 30;
protected $connectTimeout = null;
protected $requestHeaders = array();
protected $fileHandler = null;
public function __construct($options = array())
{
if (extension_loaded('curl') && function_exists('curl_version')) {
$curlVersion = curl_version();
if (! isset($curlVersion['protocols']) || ! in_array('https', $curlVersion['protocols'])) {
throw new ObjectStorage_Exception('CURL does not supported HTTPS.');
}
} else {
throw new ObjectStorage_Exception('CURL is not supported on this server.');
}
if (isset($options['timeout']) && is_numeric($options['timeout'])) {
$this->timeout = $options['timeout'];
}
if (isset($options['connect_timeout']) && is_numeric($options['connect_timeout'])) {
$this->connectTimeout = $options['connect_timeout'];
}
}
/**
* (non-PHPdoc)
* @see ObjectStorage_Http_Adapter_Interface::setUri()
*/
public function setUri($uri)
{
$this->uri = $uri;
}
/**
* (non-PHPdoc)
* @see ObjectStorage_Http_Adapter_Interface::setHeaders()
*/
public function setHeaders($name, $value)
{
$this->headers[$name] = $name . ': ' . $value;
}
/**
* (non-PHPdoc)
* @see ObjectStorage_Http_Adapter_Interface::setBody()
*/
public function setBody($body)
{
$this->body = $body;
}
/**
* (non-PHPdoc)
* @see ObjectStorage_Http_Adapter_Interface::setFileHandler()
*/
public function setFileHandler($handler)
{
$this->fileHandler = $handler;
}
/**
* (non-PHPdoc)
* @see ObjectStorage_Http_Adapter_Interface::setMethod()
*/
public function setMethod($method)
{
$this->method = $method;
}
/**
* (non-PHPdoc)
* @see ObjectStorage_Http_Adapter_Interface::reset()
*/
public function reset()
{
$this->headers = array();
$this->body = '';
$this->method = '';
$this->fileHandler = null;
return true;
}
/**
* (non-PHPdoc)
* @see ObjectStorage_Http_Adapter_Interface::request()
*/
public function request()
{
$curl = curl_init();
$urlInfo = parse_url($this->uri);
$requiredHeaders[] = 'Host: ' . $urlInfo['host'];
$requiredHeaders[] = 'Connection: close';
$requiredHeaders[] = 'Expect:';
$this->requestHeaders = array_merge($requiredHeaders, $this->headers);
// To get around CURL issue. http://curl.haxx.se/mail/lib-2010-08/0171.html
$requestHeaders = implode("\r\n", $this->requestHeaders);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $this->uri);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($requestHeaders));
if ( $this->connectTimeout !== null ) {
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
}
$method = strtoupper($this->method);
switch($method) {
case 'HEAD':
curl_setopt($curl, CURLOPT_NOBODY, true);
break;
case 'PUT':
curl_setopt($curl, CURLOPT_PUT, true);
break;
case 'POST':
curl_setopt($curl, CURLOPT_POST, true);
break;
case 'DELETE':
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
if (in_array($method, array('PUT', 'POST'))) {
if ($this->fileHandler != null) {
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_INFILE, $this->fileHandler);
curl_setopt($curl, CURLOPT_READFUNCTION, array(&$this, 'readFileCallback'));
} else if ($this->body != '') {
$filePointer = fopen('php://temp/maxmemory:256000', 'w');
if (! $filePointer) {
throw new ObjectStorage_Exception('could not open temp memory data');
}
fwrite($filePointer, $this->body);
fseek($filePointer, 0);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_INFILE, $filePointer);
curl_setopt($curl, CURLOPT_INFILESIZE, strlen($this->body));
}
}
$rawResponse = curl_exec($curl);
if ($rawResponse === false) {
throw new ObjectStorage_Exception(curl_error($curl));
}
$curlInfo = curl_getinfo($curl);
$rawHeaders = substr($rawResponse, 0, $curlInfo['header_size']);
$headers = array();
if ($rawHeaders != '') {
$headerLines = explode("\n", $rawHeaders);
foreach ($headerLines as $line) {
$headerChunk = explode(': ', $line);
if (count($headerChunk) == 2) {
$headers[ucfirst(strtolower($headerChunk[0]))] = trim($headerChunk[1]);
}
}
}
$body = substr($rawResponse, $curlInfo['header_size']);
$statusCode = $curlInfo['http_code'];
curl_close($curl);
$response = new ObjectStorage_Http_Response();
$response->setStatusCode($statusCode);
$response->setHeaders($headers);
$response->setBody($body);
return $response;
}
/**
* (non-PHPdoc)
* @see ObjectStorage_Http_Adapter_Interface::getLastRequestHeaders()
*/
public function getLastRequestHeaders()
{
return $this->requestHeaders;
}
protected function readFileCallback($curl, $fileHandler, $length = 1000)
{
$data = fread($fileHandler, $length);
$len = strlen($data);
return $data;
}
}