note 56770 added to function.get-headers

From: Date: Wed, 14 Sep 2005 19:52:41 +0000
Subject: note 56770 added to function.get-headers
Groups: php.notes 
Request: Send a blank email to php-notes+get-95186@lists.php.net to get a copy of this message
hey, i came across this afew weeks ago and used the function in an app for recording info about domains that my company owns, and found that the status this returns was wrong most of the time (400 bad request or void for sites that were clearly online). then looking into it i noticed the problem was that it wasn't able to get the correct info about sites with redirections. but thats not the full problem because everything on my server was returning the wrong status too. i searched around on php.net for other info and found that fsockopen's example worked better and only needed some tweeking. heres the function i put together from it and a small change. <? if(!function_exists('get_headers')) { function get_headers($url,$format=0,$httpn=0){ $fp = fsockopen($url, 80, $errno, $errstr, 30); if ($fp) { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: $url\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $var.=fgets($fp, 1280); } $var=explode("<",$var); $var=$var[0]; $var=explode("\n",$var); fclose($fp); return $var; } } } ?> this returns an array of the header (only problem being that if the site doesn't have correct html it'll pull in some content too). hope this'll help someone else. ---- Manual Page -- http://www.php.net/manual/en/function.get-headers.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56770 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56770&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56770&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56770&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56770&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56770&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56770&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56770&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95186) next »