forked from sesser/Instaphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.php
247 lines (217 loc) · 7.76 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
<?php
/**
* Instaphp
*
* Copyright (c) 2011 randy sesser <[email protected]>
*
* 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 <[email protected]>
* @copyright 2011, randy sesser
* @license http://www.opensource.org/licenses/mit-license The MIT License
* @package Instaphp
* @filesource
*/
namespace Instaphp {
use Instaphp\Config;
use Instaphp\Cache;
use Instaphp\WebRequest;
/**
* 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 <[email protected]>
*/
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;
/**
*
* @var iCache Cache object used for caching
* @access private
*/
private $_cache = null;
/**
* 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())
{
$this->useCurl = self::HasCurl();
$this->parameters = $params;
$this->url = $url;
/*
$cacheConfig = Config::Instance()->GetSection("Instaphp/Cache");
if (!empty($cacheConfig) && count($cacheConfig) > 0) {
$cacheConfig = $cacheConfig[0];
if ($cacheConfig["Enabled"]) {
$engine = (string)$cacheConfig["Engine"];
$this->_cache = Cache\Cache::Instance($engine);
// $method = new \ReflectionMethod("Instaphp\\Cache\\".$engine, 'Instance');
// $this->_cache = $method->invoke(null, null);
// $this->_cache = Cache\File::Instance();
}
}
*/
}
/**
* 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;
$query = '';
foreach ($this->parameters as $k => $v)
$query .= ((strlen ($query) == 0) ? '?' : '&') . sprintf('%s=%s', $k, $v);
if (null !== $this->_cache) {
$key = sha1($url.$query);
if (false === ($response = $this->_cache->Get($key))) {
$response = $this->GetResponse();
if (empty ($response->error)) {
$this->_cache->Set($key, $response);
}
}
} else {
$response = $this->GetResponse();
}
$this->response = $response;
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')
{
//-- 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) {
$response = new Response;
$http = WebRequest::Instance();
$res = $http->Create($this->url, $method, $this->parameters);
if ($res instanceof Error)
return $res;
$response->info = $res->Info;
$response->json = $res->Content;
$response = Response::Create($this, $response);
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;
}
}
}