|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HttpRequest.php |
|
4
|
|
|
* |
|
5
|
|
|
* @package Embera |
|
6
|
|
|
* @author Michael Pratt <[email protected]> |
|
7
|
|
|
* @link http://www.michael-pratt.com/ |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Embera; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This class is in charge of doing http requests. Its a very minimal |
|
17
|
|
|
* wrapper for curl or file_get_contents |
|
18
|
|
|
*/ |
|
19
|
|
|
class HttpRequest |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var array Array with custom curl/fopen options */ |
|
22
|
|
|
protected $config = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** @var string User Agent String */ |
|
25
|
|
|
protected $userAgent = 'Mozilla/5.0 PHP/Embera'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $config |
|
31
|
|
|
* @return void |
|
|
|
|
|
|
32
|
|
|
*/ |
|
33
|
401 |
|
public function __construct(array $config = array()) |
|
34
|
|
|
{ |
|
35
|
401 |
|
$this->config = array_merge(array( |
|
36
|
401 |
|
'curl' => array(), |
|
37
|
401 |
|
'fopen' => array(), |
|
38
|
401 |
|
'force_redirects' => false, |
|
39
|
401 |
|
'prefer_curl' => true, |
|
40
|
401 |
|
), $config); |
|
41
|
401 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Executes http requests |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $url |
|
47
|
|
|
* @param array $params Additional parameters for the respective part |
|
48
|
|
|
* @return string |
|
49
|
|
|
* |
|
50
|
|
|
* @throws Exception when an error ocurred or if no way to do a request exists |
|
51
|
|
|
*/ |
|
52
|
157 |
|
public function fetch($url, array $params = array()) |
|
53
|
|
|
{ |
|
54
|
157 |
|
$params = array_merge(array( |
|
55
|
157 |
|
'curl' => array(), |
|
56
|
157 |
|
'fopen' => array(), |
|
57
|
157 |
|
), $params); |
|
58
|
|
|
|
|
59
|
157 |
|
if (function_exists('curl_init') && $this->config['prefer_curl']) { |
|
60
|
153 |
|
return $this->curl($url, $params['curl']); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
return $this->fileGetContents($url, $params['fopen']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Uses Curl to fetch data from an url |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $url |
|
70
|
|
|
* @param array $params Additional parameters for the respective part |
|
71
|
|
|
* @return string |
|
72
|
|
|
* |
|
73
|
|
|
* @throws Exception when the returned status code is not 200 or no data was found |
|
74
|
|
|
*/ |
|
75
|
153 |
|
protected function curl($url, array $params = array()) |
|
76
|
|
|
{ |
|
77
|
|
|
// Not using array_merge here because that function reindexes numeric keys |
|
78
|
153 |
|
$options = $params + $this->config['curl'] + array( |
|
79
|
153 |
|
CURLOPT_USERAGENT => $this->userAgent, |
|
80
|
153 |
|
CURLOPT_ENCODING => '', |
|
81
|
153 |
|
CURLOPT_FOLLOWLOCATION => true, |
|
82
|
153 |
|
); |
|
83
|
|
|
|
|
84
|
153 |
|
$options[CURLOPT_URL] = $url; |
|
85
|
153 |
|
$options[CURLOPT_HEADER] = true; |
|
86
|
153 |
|
$options[CURLOPT_RETURNTRANSFER] = 1; |
|
87
|
|
|
$options[CURLOPT_SSL_VERIFYPEER] = false; |
|
88
|
|
|
|
|
89
|
153 |
|
// CURLOPT_FOLLOWLOCATION doesnt play well with open_basedir/safe_mode |
|
90
|
|
|
if (ini_get('safe_mode') || ini_get('open_basedir')) { |
|
91
|
|
|
$options[CURLOPT_FOLLOWLOCATION] = false; |
|
92
|
|
|
$options[CURLOPT_TIMEOUT] = 15; |
|
93
|
|
|
$this->config['force_redirects'] = true; |
|
94
|
|
|
} |
|
95
|
153 |
|
|
|
96
|
153 |
|
$handler = curl_init(); |
|
97
|
153 |
|
curl_setopt_array($handler, $options); |
|
98
|
|
|
$response = curl_exec($handler); |
|
99
|
153 |
|
|
|
100
|
153 |
|
$status = curl_getinfo($handler, CURLINFO_HTTP_CODE); |
|
101
|
|
|
$headerSize = curl_getinfo($handler, CURLINFO_HEADER_SIZE); |
|
102
|
153 |
|
|
|
103
|
153 |
|
$header = substr($response, 0, $headerSize); |
|
104
|
153 |
|
$body = substr($response, $headerSize); |
|
105
|
|
|
curl_close($handler); |
|
106
|
153 |
|
|
|
107
|
|
|
if ($this->config['force_redirects'] && in_array($status, array('301', '302'))) { |
|
108
|
2 |
|
|
|
109
|
|
|
if (preg_match('~(?:location|uri): ?([^\n]+)~i', $header, $matches)) { |
|
110
|
2 |
|
|
|
111
|
|
|
$url = trim($matches['1']); |
|
112
|
|
|
|
|
113
|
2 |
|
// Relative redirections |
|
114
|
2 |
|
if (substr($url, 0, 1) == '/') { |
|
115
|
2 |
|
$parsed = parse_url($options[CURLOPT_URL]); |
|
116
|
2 |
|
$url = $parsed['scheme'] . '://' . rtrim($parsed['host'], '/') . $url; |
|
117
|
|
|
} |
|
118
|
2 |
|
|
|
119
|
|
|
return $this->curl($url, $options); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
153 |
|
|
|
123
|
3 |
|
if (empty($body) || !in_array($status, array('200'))) { |
|
124
|
|
|
throw new \Exception($status . ': Invalid response for ' . $url); |
|
125
|
|
|
} |
|
126
|
151 |
|
|
|
127
|
|
|
return $body; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Uses file_get_contents to fetch data from an url |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $url |
|
134
|
|
|
* @param array $params Additional parameters for the respective part |
|
135
|
|
|
* @return string |
|
136
|
|
|
* |
|
137
|
|
|
* @throws Exception when allow_url_fopen is disabled or when no data was returned |
|
138
|
4 |
|
*/ |
|
139
|
|
|
protected function fileGetContents($url, array $params = array()) |
|
140
|
4 |
|
{ |
|
141
|
|
|
if (!ini_get('allow_url_fopen')) { |
|
142
|
|
|
throw new \Exception('Could not execute lookup, allow_url_fopen is disabled'); |
|
143
|
|
|
} |
|
144
|
4 |
|
|
|
145
|
2 |
|
if (!filter_var($url, FILTER_VALIDATE_URL)) { |
|
146
|
|
|
throw new \Exception('Invalid url ' . $url); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
2 |
|
$defaultOptions = array( |
|
150
|
2 |
|
'method' => 'GET', |
|
151
|
2 |
|
'user_agent' => $this->userAgent, |
|
152
|
2 |
|
'follow_location' => 1, |
|
153
|
|
|
'max_redirects' => 20, |
|
154
|
2 |
|
'timeout' => 40 |
|
155
|
|
|
); |
|
156
|
2 |
|
|
|
157
|
2 |
|
$context = array('http' => array_merge($defaultOptions, $this->config['fopen'], $params)); |
|
158
|
2 |
|
if ($data = file_get_contents($url, false, stream_context_create($context))) { |
|
159
|
|
|
return $data; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
throw new \Exception('Invalid Server Response from ' . $url); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
?> |
|
|
|
|
|
|
167
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.