I'm having a terrible time trying to get HTTP status codes from web pages (404, 200, 301, etc.). I think a big part of the problem is that we're behind a proxy.
How would I modify the following code examples to connect through a proxy in order to get the status code?
function http_file_status($url, $followRedirects = true)
{
$url_parsed = parse_url($url);
if (!@$url_parsed['scheme']) $url_parsed = parse_url('http://'.$url);
extract($url_parsed);
if(!@$port) $port = 80;
if(!@$path) $path = '/';
if(@$query) $path .= '?'.$query;
$out = "HEAD $path HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n\r\n";
if(!$fp = @fsockopen($host, $port, $es, $en, 5)){
return false;
}
fwrite($fp, $out);
while (!feof($fp)) {
$s = fgets($fp, 128);
if(($followRedirects) && (preg_match('/^Location:/i', $s) != false)){
fclose($fp);
return http_file_status(trim(preg_replace("/Location:/i", "", $s)));
}
if(preg_match('@HTTP[/]1[.][01x][\s]{1,}([1-5][01][0-9])[\s].*$@', $s, $matches))
{
$status = $matches[1];
}
}
fclose($fp);
if(!empty($status))
{
return $status;
}
return false;
}
OR:
set_time_limit(20);
ob_start();
header("Content-Type: text/plain");
$host = "www.webmasterworld.com";
$port = 80;
$post = "HEAD / HTTP/1.1\r\n";
$post .= "Host: www.webmasterworld.com\r\n";
$post .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2\r\n";
$post .= "Keep-Alive: 200\r\n";
$post .= "Connection: keep-alive\r\n\r\n";
$sock = fsockopen($host, $port, $errno, $errstr, 20.0);
fwrite($sock, $post, strlen($post));
while (!feof($sock)){
echo fgets($sock);
}
ob_end_flush();