note 57131 deleted from function.get-headers by danbrown

From: Date: Mon, 09 Jan 2012 20:00:08 +0000
Subject: note 57131 deleted from function.get-headers by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-185502@lists.php.net to get a copy of this message
Note Submitter: tylerxxdurden at web dot de ---- my version cleans the input URL a little bit (you should be able to put in almost every shit, and it should still work) Should work for: http://test.de http://test.de/ www.test.de http://test.de/ http://test.de/test.html http://test.de/test.html?testme ... not nice, but seems to work for me <? function get_headers2($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) { if(!$url_info['path']){ $url_info['path'] = "/"; } if($url_info['path'] && !$url_info['host']){ $url_info['host'] = $url_info['path']; $url_info['path'] = "/"; } if( $url_info['host'][(strlen($url_info['host'])-1)] == "/" ){ $url_info['host'][(strlen($url_info['host'])-1)] = ""; } if(!$url_array[scheme]){ $url_array[scheme] = "http"; //we always use http links } $head = "HEAD ".@$url_info['path']; if( $url_info['query'] ){ $head .= "?".@$url_info['query']; } print_r($url_info); $head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n"; echo $head; 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; } } ?>

« previous php.notes (#185502) next »