note 56770 modified in function.get-headers by danbrown
| From: | danbrown@php.net | Date: | Mon, 09 Jan 2012 20:00:38 +0000 |
| Subject: | note 56770 modified in function.get-headers by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185503@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.
<?php
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.
--was--
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.
http://php.net/manual/en/function.get-headers.php