cvs: pear /PHP_Compat/Compat/Function get_headers.php
| From: | Arpad Ray | Date: | Mon, 02 Jan 2006 21:56:58 +0000 |
| Subject: | cvs: pear /PHP_Compat/Compat/Function get_headers.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-37268@lists.php.net to get a copy of this message | ||
arpad Mon Jan 2 21:56:58 2006 UTC
Modified files:
/pear/PHP_Compat/Compat/Function get_headers.php
Log:
ability to follow redirects
http://cvs.php.net/viewcvs.cgi/pear/PHP_Compat/Compat/Function/get_headers.php?r1=1.2&r2=1.3&diff_format=u
Index: pear/PHP_Compat/Compat/Function/get_headers.php
diff -u pear/PHP_Compat/Compat/Function/get_headers.php:1.2
pear/PHP_Compat/Compat/Function/get_headers.php:1.3
--- pear/PHP_Compat/Compat/Function/get_headers.php:1.2 Mon Dec 19 23:45:29 2005
+++ pear/PHP_Compat/Compat/Function/get_headers.php Mon Jan 2 21:56:58 2006
@@ -15,8 +15,9 @@
// | Authors: Aidan Lister <aidan@php.net> |
// +----------------------------------------------------------------------+
//
-// $Id: get_headers.php,v 1.2 2005/12/19 23:45:29 aidan Exp $
+// $Id: get_headers.php,v 1.3 2006/01/02 21:56:58 arpad Exp $
+define('PHP_COMPAT_GET_HEADERS_MAX_REDIRECTS', 5);
/**
* Replace get_headers()
@@ -27,12 +28,39 @@
* @author Aeontech <aeontech@gmail.com>
* @author Cpurruc <cpurruc@fh-landshut.de>
* @author Aidan Lister <aidan@php.net>
- * @version $Revision: 1.2 $
+ * @author Arpad Ray <arpad@php.net>
+ * @version $Revision: 1.3 $
* @since PHP 5.0.0
* @require PHP 4.0.0 (user_error)
*/
function php_compat_get_headers($url, $format = 0)
{
+ $result = array();
+ for ($i = 0; $i < PHP_COMPAT_GET_HEADERS_MAX_REDIRECTS; $i++) {
+ $headers = php_compat_get_headers_helper($url, $format);
+ if ($headers === false) {
+ return false;
+ }
+ $result = array_merge($result, $headers);
+ if ($format == 1 && isset($headers['Location'])) {
+ $url = $headers['Location'];
+ continue;
+ }
+ if ($format == 0) {
+ for ($j = count($headers); $j--;) {
+ if (preg_match('/^Location: (.*)$/i', $headers[$j], $matches)) {
+ $url = $matches[1];
+ continue 2;
+ }
+ }
+ }
+ return $result;
+ }
+ return empty($result) ? false : $result;
+}
+
+function php_compat_get_headers_helper($url, $format)
+{
// Init
$urlinfo = parse_url($url);
$port = isset($urlinfo['port']) ? $urlinfo['port'] : 80;
@@ -44,13 +72,14 @@
}
// Send request
- $head = 'HEAD ' . $urlinfo['path'] .
+ $head = 'HEAD ' . (isset($urlinfo['path']) ? $urlinfo['path'] :
'/') .
(isset($urlinfo['query']) ? '?' . $urlinfo['query'] :
'') .
' HTTP/1.0' . "\r\n" .
'Host: ' . $urlinfo['host'] . "\r\n\r\n";
fputs($fp, $head);
// Read
+ $headers = array();
while (!feof($fp)) {
if ($header = trim(fgets($fp, 1024))) {
list($key) = explode(':', $header);
@@ -69,10 +98,11 @@
}
}
+ fclose($fp);
+
return $headers;
}
-
// Define
if (!function_exists('get_headers')) {
function get_headers($url, $format = 0)