70 if (class_exists(
'idna_convert'))
72 $idn =
new idna_convert();
78 if (preg_match(
'/^http(s)?:\/\//i',
$url))
89 if (!$force_fsockopen && function_exists(
'curl_exec'))
96 $headers2[] =
"$key: $value";
100 curl_setopt($fp, CURLOPT_ENCODING,
'');
102 curl_setopt($fp, CURLOPT_URL,
$url);
103 curl_setopt($fp, CURLOPT_HEADER, 1);
104 curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
105 curl_setopt($fp, CURLOPT_TIMEOUT, $timeout);
106 curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout);
107 curl_setopt($fp, CURLOPT_REFERER,
$url);
108 curl_setopt($fp, CURLOPT_USERAGENT,
$useragent);
109 curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
112 curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1);
113 curl_setopt($fp, CURLOPT_MAXREDIRS,
$redirects);
116 $this->headers = curl_exec($fp);
117 if (curl_errno($fp) === 23 || curl_errno($fp) === 61)
119 curl_setopt($fp, CURLOPT_ENCODING,
'none');
120 $this->headers = curl_exec($fp);
124 $this->error =
'cURL error ' . curl_errno($fp) .
': ' . curl_error($fp);
125 $this->success =
false;
129 $info = curl_getinfo($fp);
131 $this->headers = explode(
"\r\n\r\n", $this->headers, $info[
'redirect_count'] + 1);
132 $this->headers = array_pop($this->headers);
134 if ($parser->parse())
136 $this->headers = $parser->headers;
137 $this->body = $parser->body;
138 $this->status_code = $parser->status_code;
139 if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers[
'location']) && $this->redirects <
$redirects)
151 $url_parts = parse_url(
$url);
152 $socket_host = $url_parts[
'host'];
153 if (isset($url_parts[
'scheme']) && strtolower($url_parts[
'scheme']) ===
'https')
155 $socket_host =
"ssl://$url_parts[host]";
156 $url_parts[
'port'] = 443;
158 if (!isset($url_parts[
'port']))
160 $url_parts[
'port'] = 80;
162 $fp = @fsockopen($socket_host, $url_parts[
'port'], $errno, $errstr, $timeout);
165 $this->error =
'fsockopen error: ' . $errstr;
166 $this->success =
false;
170 stream_set_timeout($fp, $timeout);
171 if (isset($url_parts[
'path']))
173 if (isset($url_parts[
'query']))
175 $get =
"$url_parts[path]?$url_parts[query]";
179 $get = $url_parts[
'path'];
186 $out =
"GET $get HTTP/1.1\r\n";
187 $out .=
"Host: $url_parts[host]\r\n";
188 $out .=
"User-Agent: $useragent\r\n";
189 if (extension_loaded(
'zlib'))
191 $out .=
"Accept-Encoding: x-gzip,gzip,deflate\r\n";
194 if (isset($url_parts[
'user']) && isset($url_parts[
'pass']))
196 $out .=
"Authorization: Basic " . base64_encode(
"$url_parts[user]:$url_parts[pass]") .
"\r\n";
198 foreach (
$headers as $key => $value)
200 $out .=
"$key: $value\r\n";
202 $out .=
"Connection: Close\r\n\r\n";
205 $info = stream_get_meta_data($fp);
208 while (!$info[
'eof'] && !$info[
'timed_out'])
210 $this->headers .= fread($fp, 1160);
211 $info = stream_get_meta_data($fp);
213 if (!$info[
'timed_out'])
216 if ($parser->parse())
218 $this->headers = $parser->headers;
219 $this->body = $parser->body;
220 $this->status_code = $parser->status_code;
221 if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers[
'location']) && $this->redirects <
$redirects)
227 if (isset($this->headers[
'content-encoding']))
230 switch (strtolower(trim($this->headers[
'content-encoding'],
"\x09\x0A\x0D\x20")))
235 if (!$decoder->parse())
237 $this->error =
'Unable to decode HTTP "gzip" stream';
238 $this->success =
false;
242 $this->body = $decoder->data;
247 if (($decompressed = gzinflate($this->body)) !==
false)
249 $this->body = $decompressed;
251 else if (($decompressed = gzuncompress($this->body)) !==
false)
253 $this->body = $decompressed;
255 else if (function_exists(
'gzdecode') && ($decompressed = gzdecode($this->body)) !==
false)
257 $this->body = $decompressed;
261 $this->error =
'Unable to decode HTTP "deflate" stream';
262 $this->success =
false;
267 $this->error =
'Unknown content coding';
268 $this->success =
false;
275 $this->error =
'fsocket timed out';
276 $this->success =
false;
285 if (!$this->body = file_get_contents(
$url))
287 $this->error =
'file_get_contents could not read the file';
288 $this->success =
false;