note 64073 deleted from function.get-headers by danbrown
| From: | danbrown@php.net | Date: | Mon, 09 Jan 2012 20:00:02 +0000 |
| Subject: | note 64073 deleted from function.get-headers by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185500@lists.php.net to get a copy of this message | ||
Note Submitter: rsyring+phppost at gmail dot com
----
Once again another update. The improvements are:
HTTPS support (make sure you have SSL support enabled -- PHP5 is as simple as uncommenting
extension=php_openssl.dll in php.ini)
Fixed bug with exploding the header on ':'. This caused the location header to be broken
and just return 'http'.
Set stream timeout in addition to socket timeout.
<?php
function my_get_headers($url ) {
$url_info=parse_url($url);
if (isset($url_info['scheme']) && $url_info['scheme'] ==
'https') {
$port = 443;
@$fp=fsockopen('ssl://'.$url_info['host'], $port, $errno, $errstr,
10);
} else {
$port = isset($url_info['port']) ? $url_info['port'] : 80;
@$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 10);
}
if($fp) {
stream_set_timeout($fp, 10);
$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))) {
$sc_pos = strpos( $header, ':' );
if( $sc_pos === false ) {
$headers['status'] = $header;
} else {
$label = substr( $header, 0, $sc_pos );
$value = substr( $header, $sc_pos+1 );
$headers[strtolower($label)] = trim($value);
}
}
}
return $headers;
}
else {
return false;
}
}
?>