note 54820 added to function.get-headers

From: Date: Fri, 15 Jul 2005 20:37:21 +0000
Subject: note 54820 added to function.get-headers
Groups: php.notes 
Request: Send a blank email to php-notes+get-92040@lists.php.net to get a copy of this message
For anyone reading the previous comments, here is code that takes into account all the previous suggestions and includes a bugfix, too. This code basically provides the "get_headers" function even on systems that are not running PHP 5.0. It uses strtolower() on the keys, as suggested. It uses the $h2 array instead of the $key, as suggested. It removes a line about unsetting the $key -- no reason to unset something which is no longer used. And I've changed the status header to be named "status" (instead of "0") in the array. Note that if more than one header is returned without a label, they'll be stuck in "status" -- but I think status is the only header that comes back without a label, so it works for me. So, first the code, then a sample of the usage: <?php if(!function_exists('get_headers')) { /** * @return array * @param string $url * @param int $format * @desc Fetches all the headers * @author cpurruc fh-landshut de * @modified by dotpointer * @modified by aeontech */ function get_headers($url,$format=0) { $url_info=parse_url($url); $port = isset($url_info['port']) ? $url_info['port'] : 80; $fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30); if($fp) { $head = "HEAD ".@$url_info['path']."?".@$url_info['query']; $head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n"; fputs($fp, $head); while(!feof($fp)) { if($header=trim(fgets($fp, 1024))) { if($format == 1) { $h2 = explode(':',$header); // the first element is the http header type, such as HTTP/1.1 200 OK, // it doesn't have a separate name, so we have to check for it. if($h2[0] == $header) { $headers['status'] = $header; } else { $headers[strtolower($h2[0])] = trim($h2[1]); } } else { $headers[] = $header; } } } return $headers; } else { return false; } } } ?> OK? Here's the usage: <?php $response = get_headers('http://www.example.com/', 1); if (!$response) { echo 'Unable to initiate connection.'; } else { print_r($response); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.get-headers.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54820 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54820&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54820&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54820&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54820&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54820&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54820&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54820&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#92040) next »