forked from sesser/Instaphp
/
request.php
290 lines (257 loc) · 10.1 KB
/
request.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Instaphp
*
* Copyright (c) 2011 randy sesser <randy@instaphp.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author randy sesser <randy@instaphp.com>
* @copyright 2011, randy sesser
* @license http://www.opensource.org/licenses/mit-license The MIT License
* @package Instaphp
* @filesource
*/
namespace Instaphp {
use Instaphp\Config;
/**
* Request
* The Request class performs simple curl requests to a URL optionally passing
* parameters on the querystring. Currently, it supports GET,POST and DELETE requests.
* @package Instaphp
* @version 1.0
* @author randy sesser <randy@instaphp.com>
*/
class Request
{
/**
* Associative array of key/value pairs to pass to the Instagram API
* @var Array
* @access public
*/
public $parameters = array();
/**
* The URL in which to make the request
* @var String
* @access public
*/
public $url = null;
/**
* A var to store whether or not to use curl
* @var boolean
* @access private
*/
private $useCurl = false;
/**
* A curl handle
* @var Handle
* @access private
*/
private $ch = null;
/**
* Default options to pass to cURL
* @var Array
* @access private
*/
private $curl_opts = array(
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 10,
CURLOPT_ENCODING => ''
);
/**
* Max number of redirects to follow a request before giving up
* @var int
* @access private
* @static
*/
private static $max_redirects = 3;
/**
* The constructor contructs
* @param string $url A URL in which to create a new request (optional)
* @param Array $params An associated array of key/value pairs to pass to said URL (optional)
*/
public function __construct($url = null, $params = array())
{
if (isset(Config::Instance()->Endpoint['timeout']))
$this->curl_opts[CURLOPT_TIMEOUT] = (int)Config::Instance()->Endpoint['timeout'];
$this->curl_opts[CURLOPT_USERAGENT] = 'Instaphp/v' . INSTAPHP_VERSION;
//-- this is an interesting hack to make curl+ssl+windows follow redirects
//-- without skipping verification. For some reason, the version of libcurl/curl
//-- included with ZendServer CE doesn't use the systems CA bundle, so, we specify
//-- the path to the cert here (via config setting)
if (isset(Config::Instance()->Instaphp->CACertBundlePath) && !empty(Config::Instance()->Instaphp->CACertBundlePath)) {
$this->curl_opts[CURLOPT_SSL_VERIFYPEER] = true;
$this->curl_opts[CURLOPT_SSL_VERIFYHOST] = 2;
$this->curl_opts[CURLOPT_SSLVERSION] = 3;
$this->curl_opts[CURLOPT_CAINFO] = Config::Instance()->Instaphp->CACertBundlePath;
}
$this->useCurl = self::HasCurl();
$this->parameters = $params;
$this->url = $url;
}
/**
* Used to close the current curl handle
*/
public function __destruct()
{
//-- close the curl handle. we're done with it
if (null != $this->ch)
curl_close($this->ch);
}
/**
* Makes a GET request
* @param string $url A URL in which to make a GET request
* @param Array $params An associative array of key/value pairs to pass to said URL
* @return Request
*/
public function Get($url = null, $params = array())
{
if (null !== $url)
$this->url = $url;
if (!empty($params))
$this->parameters = $params;
$this->response = $this->GetResponse();
return $this;
}
/**
* Makes a POST request
* @param string $url A URL in which to make a POST request
* @param Array $params An associative array of key/value pairs to pass to said URL
* @return Request
*/
public function Post($url = null, $params = array())
{
if (null !== $url)
$this->url = $url;
if (!empty($params))
$this->parameters = $params;
$this->response = $this->GetResponse('POST');
return $this;
}
/**
* Makes a PUT request (currently unused)
* @param string $url A URL in which to make a PUT request
* @param Array $params An associative array of key/value pairs to pass to said URL
* @return void
*/
public function Put($url = null, $params = array())
{
}
/**
* Makes a DELETE request
* @param string $url A URL in which to make a DELETE request
* @param Array $params An associative array of key/value pairs to pass to said URL
* @return Request
*/
public function Delete($url = null, $params = array())
{
if (null !== $url)
$this->url = $url;
if (!empty($params))
$this->parameters = $params;
$this->response = $this->GetResponse('DELETE');
return $this;
}
/**
* Makes a request
* @param string $url A URL in which to make a GET request
* @param Array $params An associative array of key/value pairs to pass to said URL
* @access private
* @return Response
*/
private function GetResponse($method = 'GET')
{
//-- if there's no url, can't make a request
if (null == $this->url)
trigger_error('No URL to make a request', E_USER_ERROR);
//-- since there's no option to use anything other curl, this check is kinda useless
//-- I had high hopes with this one using sockets and whatnot, but alas, time is of
//-- the essence... in internet time
if ($this->useCurl) {
//-- no curl handle? create one
if ($this->ch === null)
$this->ch = curl_init();
$opts = $this->curl_opts;
$query = '';
switch (strtolower($method)) {
case 'post':
$opts[CURLOPT_POST] = true;
$opts[CURLOPT_POSTFIELDS] = $this->parameters;
break;
case 'delete':
$opts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
foreach ($this->parameters as $key => $val)
$query .= ((strlen($query) == 0) ? '?' : '&') . $key . '=' . urlencode($val);
break;
default:
foreach ($this->parameters as $key => $val)
$query .= ((strlen($query) == 0) ? '?' : '&') . $key . '=' . urlencode($val);
break;
}
$this->url .= $query;
$opts[CURLOPT_URL] = $this->url;
$response = new Response;
if (curl_setopt_array($this->ch, $opts)) {
if (false !== ($res = curl_exec($this->ch))) {
$response->info = curl_getinfo($this->ch);
$response->json = $res;
$response = Response::Create($this, &$response);
} else {
$response->error = new Error('cURLError', curl_errno($this->ch), curl_error($this->ch), $opts[CURLOPT_URL]);
}
}
return $response;
}
}
/**
* Checks to see if cURL extension is available
* @access private
* @return boolean
*/
private static function HasCurl()
{
return function_exists('curl_init');
}
/**
* Determines whether or not curl will follow redirects over SSL
* See the constructor for details, but there are cases in which
* if curl can't verify the certificate of an SSL request, AND
* PHP is in safe_mode OR there are open_basedir restrictions, it will
* not follow a redirect. There's a fix for this that involves
* parsing all the response headers from a request and detecting
* a Location header, but that's kind of a hack as it bypasses the
* whole point of SSL. This method left for posterity. Or something...
*
* @return boolean
* @access private
**/
private function WillFollowRedirects()
{
$open_basedir = ini_get('open_basedir');
$safe_mode = strtolower(ini_get('safe_mode'));
if (empty($open_basedir) && $safe_mode == 'off') {
return true;
}
return false;
}
}
}