Line 1... |
Line 1... |
1 |
<?php |
1 |
<?php |
2 |
// +----------------------------------------------------------------------+ |
2 |
// +----------------------------------------------------------------------+ |
3 |
// | PHP Version 4 | |
3 |
// | PHP Version 4 | |
4 |
// +----------------------------------------------------------------------+ |
4 |
// +----------------------------------------------------------------------+ |
5 |
// | Copyright (c) 1997-2004 The PHP Group | |
5 |
// | Copyright (c) 1997-2004 The PHP Group | |
6 |
// +----------------------------------------------------------------------+ |
6 |
// +----------------------------------------------------------------------+ |
7 |
// | This source file is subject to version 3.0 of the PHP license, | |
7 |
// | This source file is subject to version 3.0 of the PHP license, | |
8 |
// | that is bundled with this package in the file LICENSE, and is | |
8 |
// | that is bundled with this package in the file LICENSE, and is | |
9 |
// | available at through the world-wide-web at | |
9 |
// | available at through the world-wide-web at | |
10 |
// | http://www.php.net/license/3_0.txt. | |
10 |
// | http://www.php.net/license/3_0.txt. | |
11 |
// | If you did not receive a copy of the PHP license and are unable to | |
11 |
// | If you did not receive a copy of the PHP license and are unable to | |
12 |
// | obtain it through the world-wide-web, please send a note to | |
12 |
// | obtain it through the world-wide-web, please send a note to | |
13 |
// | license@php.net so we can mail you a copy immediately. | |
13 |
// | license@php.net so we can mail you a copy immediately. | |
14 |
// +----------------------------------------------------------------------+ |
14 |
// +----------------------------------------------------------------------+ |
15 |
// | Authors: Aidan Lister <aidan@php.net> | |
15 |
// | Authors: Aidan Lister <aidan@php.net> | |
16 |
// +----------------------------------------------------------------------+ |
16 |
// +----------------------------------------------------------------------+ |
17 |
// |
17 |
// |
18 |
// $Id: get_headers.php,v 1.1 2005/05/10 07:50:53 aidan Exp $ |
18 |
// $Id: get_headers.php,v 1.1 2005/05/10 07:50:53 aidan Exp $ |
19 |
|
19 |
|
20 |
|
20 |
|
21 |
/** |
21 |
/** |
22 |
* Replace get_headers() |
22 |
* Replace get_headers() |
23 |
* |
23 |
* |
24 |
* @category PHP |
24 |
* @category PHP |
25 |
* @package PHP_Compat |
25 |
* @package PHP_Compat |
26 |
* @link http://php.net/function.get_headers |
26 |
* @link http://php.net/function.get_headers |
27 |
* @author Aeontech <aeontech@gmail.com> |
27 |
* @author Aeontech <aeontech@gmail.com> |
28 |
* @author Cpurruc <cpurruc@fh-landshut.de> |
28 |
* @author Cpurruc <cpurruc@fh-landshut.de> |
29 |
* @author Aidan Lister <aidan@php.net> |
29 |
* @author Aidan Lister <aidan@php.net> |
30 |
* @version $Revision: 1.1 $ |
30 |
* @version $Revision: 1.1 $ |
31 |
* @since PHP 5.0.0 |
31 |
* @since PHP 5.0.0 |
32 |
* @require PHP 4.0.0 (user_error) |
32 |
* @require PHP 4.0.0 (user_error) |
33 |
*/ |
33 |
*/ |
34 |
if (!function_exists('get_headers')) { |
34 |
if (!function_exists('get_headers')) { |
35 |
function get_headers($url, $format = 0) |
35 |
function get_headers($url, $format = 0) |
36 |
{ |
36 |
{ |
37 |
// Init |
37 |
// Init |
38 |
$urlinfo = parse_url($url); |
38 |
$urlinfo = parse_url($url); |
39 |
$port = isset($urlinfo['port']) ? $urlinfo['port'] : 80; |
39 |
$port = isset($urlinfo['port']) ? $urlinfo['port'] : 80; |
40 |
|
40 |
|
41 |
// Connect |
41 |
// Connect |
42 |
$fp = fsockopen($urlinfo['host'], $port, $errno, $errstr, 30); |
42 |
$fp = fsockopen($urlinfo['host'], $port, $errno, $errstr, 30); |
43 |
if ($fp === false) { |
43 |
if ($fp === false) { |
44 |
return false; |
44 |
return false; |
45 |
} |
45 |
} |
46 |
|
46 |
|
47 |
// Send request |
47 |
// Send request |
48 |
$head = 'HEAD ' . $urlinfo['path'] . |
48 |
$head = 'HEAD ' . $urlinfo['path'] . |
49 |
(isset($urlinfo['query']) ? '?' . $urlinfo['query'] : '') . |
49 |
(isset($urlinfo['query']) ? '?' . $urlinfo['query'] : '') . |
50 |
' HTTP/1.0' . "\r\n" . |
50 |
' HTTP/1.0' . "\r\n" . |
51 |
'Host: ' . $urlinfo['host'] . "\r\n\r\n"; |
51 |
'Host: ' . $urlinfo['host'] . "\r\n\r\n"; |
52 |
fputs($fp, $head); |
52 |
fputs($fp, $head); |
53 |
|
53 |
|
54 |
// Read |
54 |
// Read |
55 |
while (!feof($fp)) { |
55 |
while (!feof($fp)) { |
56 |
if ($header = trim(fgets($fp, 1024))) { |
56 |
if ($header = trim(fgets($fp, 1024))) { |
57 |
list($key) = explode(':', $header); |
57 |
list($key) = explode(':', $header); |
58 |
|
58 |
|
59 |
if ($format === 1) { |
59 |
if ($format === 1) { |
60 |
// First element is the HTTP header type, such as HTTP 200 OK |
60 |
// First element is the HTTP header type, such as HTTP 200 OK |
61 |
// It doesn't have a separate name, so check for it |
61 |
// It doesn't have a separate name, so check for it |
62 |
if ($key == $header) { |
62 |
if ($key == $header) { |
63 |
$headers[] = $header; |
63 |
$headers[] = $header; |
64 |
} else { |
64 |
} else { |
65 |
$headers[$key] = substr($header, strlen($key)+2); |
65 |
$headers[$key] = substr($header, strlen($key)+2); |
66 |
} |
66 |
} |
67 |
} else { |
67 |
} else { |
68 |
$headers[] = $header; |
68 |
$headers[] = $header; |
69 |
} |
69 |
} |
70 |
} |
70 |
} |
71 |
} |
71 |
} |
72 |
|
72 |
|
73 |
return $headers; |
73 |
return $headers; |
74 |
} |
74 |
} |
75 |
} |
75 |
} |
76 |
|
76 |
|
77 |
?> |
77 |
?> |
78 |
|
78 |
|