|
|
|
| Title : |
This gets the http response headers for a given url and returns them in an assoc array. i.e. to test if a url exists: $array = get_http_headers($url); if($array[result]=200) { } |
| Categories : |
HTTP, Arrays, PHP |
 Mark Jeftovic |
| Date : |
Jan 17th 1999 |
| Grade : |
4 of 5 (graded 3 times) |
| Viewed : |
7972 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Mark Jeftovic |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
<?php
//
// get_http_headers.php3 v1.1 1998/11/24
//
// Copyright (c) 1998 Mark Jeftovic / easyDNS Technologies Inc.
// email: markjr@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.php3($url,$proto);
// $url is in the form http://www.somewhere
// $proto is optional default is HTTP/1.0
//
// RETURNS: an associative array with the keys set to the header name
// (lowercased) with the first line of headers (the result line)
// split up into:
// $array[result] = i.e. 200 or 301 or 403 or 404, etc
// $array[protocol] = protocol server used to answer
// $array[message] = i.e. "OK" or "FORBIDDEN" etc.
//
// HISTORY: 1.1 Nov 24/98
// -1 line fix from Gary E. Bickford, garyb@slb.com
// for when $uri[path] turns out to be null
function get_http_headers ($url,$proto= "HTTP/1.0") {
if(!eregi( "^http://", $url)) { $url = "http://".$url; }
$uri = parse_url($url);
$port = ( $uri[ "port"] ? $uri[ "port"] : 80 );
# open a socket to the server
$sock = fsockopen($uri[host],$port);
if(!$sock) { return(-1); }
if (!strlen($uri[path])) {$uri[path] = "/";}
$req=sprintf( "HEAD %s %s\n\n", $uri[path], $proto);
# send the request
fputs($sock, $req);
# get the headers
while(!feof($sock)):
$this_out = fgets($sock,100);
$output.= $this_out;
endwhile;
fclose($sock);
# parse them into a hash
$hdr=split( "\n",$output);
list($array[ "protocol"], $array[ "result"], $array[ "message"]) = split("[[:space:]]+", $hdr[0], 3);
for($i=1,$num=count($hdr);$i<$num;$i++) {
list($key, $val)=split( ":[[:space:]]*", $hdr[$i],2);
$array[strtolower($key)]=$val;
}
return($array);
}
?> |
|
| The following snippet gives complete info about all submitted
HTTP_POST_VARS and HTTP_GET_VARS Categories : Variables, HTTP, PHP | | | clearing variables in php3 Categories : Variables, Arrays, PHP | | | Select with current month Categories : PHP, HTML and PHP, Date Time, Arrays | | | Simple Password example Categories : PHP, Authentication, Security, HTTP | | | Stock quotes from yahoo! Categories : PHP, Web Services, Arrays | | | How to display any array in several rows and columns of a table. Not just
in one column or in alternate rows. This example shows a nice color table
generated with PHP, but can be used with any array values(e.g. Database) Categories : Arrays, PHP, Miscellaneous, Beginner Guides, Graphics | | | Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
Categories : PHP, Directories, Variables, Arrays | | | translate.php - Assocciative array example, passing a reference to a function. Categories : PHP, Arrays, Languages, Variables | | | Finds the median in an array of numbers - Can be used with a MySql database column read into an array Categories : PHP, Arrays, Databases, MySQL | | | header -- Send a raw HTTP header Categories : PHP, PHP Functions, HTTP | | | XML To Array Categories : PHP, PHP Classes, XML, Arrays | | | BBCode Formatting String Categories : PHP, HTML, Regexps, Arrays | | | PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | |
| | | | Steve Doty wrote :1
Cool example.
Almost what I needed, I wanted something that returns the
content-length & content-type also.
To do that I had to replace the dash '-' char to a underscore
'_' in the $key value.$array[] can not use dashes in val names.
Without this you can not return content-length or content-type.
you can add a str_replace call.
$strkey = str_replace("-","_",$key);
in this 'for' statment to fix it.
for($i=1,$num=count($hdr);$i<$num;$i++) {
list($key, $val)=split( ":[[:space:]]*", $hdr[$i],2);
$strkey = str_replace("-","_",$key);
$array[strtolower($strkey)]=$val;
}
| |
|
|
|