home *** CD-ROM | disk | FTP | other *** search
- Http headers
-
- This gets the http response headers for a given url and returns them in an assoc. array. i.e.
-
- <?php
- #
- # get_http_headers()
- # v1.3 - 1999/06/08
- #
- # Copyright (c) 1998,1999 easyDNS Technologies Inc.
- # http://www.easyDNS.com
- # info@easyDNS.com
- # All rights reserved.
- #
- # This code provided "As Is" with no warrantees express or implied.
- # The author and contributors are not liable for anything good or
- # bad that results from your use of this code.
- #
- # You are free to distribute this for free provided this notice is
- # included. Please forward fixes/enhancements to the author for
- # inclusion in the next revision.
- #
- # USAGE:
- # array get_http_headers( str url, str [proto], int [timeout]);
- #
- # url is in the form "http://www.somewhere.com" or "www.somewhere.com"
- # proto is optional, default is "HTTP/1.0"
- # timeout is optional, default is 10 (seconds)
- #
- # array is an associative array with the keys set to the header name
- # (lowercase) with the first line of headers (the result line)
- # split up into:
- # $array[protocol] = protocol server used to answer
- # $array[result] = i.e. 200 or 301 or 403 or 404, etc
- # $array[message] = i.e. "OK" or "FORBIDDEN" etc.
- #
- # $array[time_used] will be the approximate number of seconds the
- # that the request took
- #
- # If the request times out, $array[result] will be set to 502.
- #
- # If the URL is invalid, false is returned
- #
- #
- # HISTORY:
- #
- # v1.3
- # 1999/06/08
- # from Mark Jeftovic, markjr@easyDNS.com
- # - reduced buffer size in polling loop to 80 from 128
- # - setsockblocking = 0 by default but 1 if proto
- # request is HTTP/1.1 (in which case our own
- # timeout is ignored for some reason)
- # - only send Host header for HTTP/1.1 requests
- # - stripped extra \n before Host header
- # - moved parsing to parse_output() function so
- # we can still return partial headers on a timeout
- #
- # v1.2
- # 1999/02/24
- # from Colin Viebrock, cmv@easyDNS.com
- # Speed enhancements and rewrite:
- # - check that host exists before trying to open socket
- # - socket blocking for a timeout
- # - returns $array[time_used] ... just for fun.
- #
- # v1.1
- # 1998/11/24
- # from Gary E. Bickford, garyb@slb.com
- # Fix for when $path turns out to be null
- #
- # v1.0
- # from Mark Jeftovic, markjr@easyDNS.com
- # Original code
- #
- #
-
- function get_http_headers($url, $proto="HTTP/1.0", $timeout=10) {
- $return = false;
- if (substr($url,0,7)=="http://") {
- $url = substr($url,7);
- }
-
- $parts = parse_url("http://".$url);
-
- $ips = gethostbynamel($parts["host"]);
-
- if ($ips[0]) {
- $ip = $ips[0];
- $host = $parts["host"];
- $path = ($parts["path"]) ? $parts["path"] : "/";
- $port = ($parts["port"]) ? $parts["port"] : 80;
-
- $start = time();
- $timeout = $timeout + $start;
-
- if($sock = fsockopen($host, $port)) {
- set_socket_blocking($sock, 0);
- switch($proto) {
- case "HTTP/1.1":
- set_socket_blocking($sock, 1);
- fputs($sock, sprintf("HEAD %s %s\n", $path, $proto));
- fputs($sock, sprintf("Host: %s\n\n", $host));
- break;
- default:
- fputs($sock, sprintf("HEAD %s %s\n\n", $path, $proto));
- }
-
- while(!feof($sock) && $t<$timeout) {
- $line .= fgets($sock,1);
- $t = time();
- }
- fclose($sock);
- $end = time();
-
- if ($t>=$timeout) {
- $http = parse_output($line);
- $http["result"] = 502;
- $http["message"] = "Timed Out";
- $http["time_used"] = $end - $start;
- $return = $http;
- } elseif($line) {
- $http = parse_output($line);
- $http["time_used"] = $end - $start;
- $return = $http;
- }
- }
- }
- return $return;
- }
-
- function parse_output($line) {
- $lines = explode("\n", $line);
- if(substr($lines[0],0,4)=="HTTP") {
- list($http["protocol"], $http["result"], $http["message"]) = split("[[:space:]]+",$lines[0],3);
- } else if(substr($lines[0],0,7)=="Server:") {
- $http["server"] = substr($lines[0],8);
- }
- for ($i=1; $i<count($lines); $i++) {
- list($key, $val) = split(":[[:space:]]*", $lines[$i],2);
- $key = strtolower(trim($key));
- if ($key) {
- $http[$key] = trim($val);
- } else {
- break;
- }
- }
- return($http);
- };
- ?>
-
-