Version: 1.2
Type: Function
Category: HTTP
License: GNU General Public License
Description: function that read an url with http header from a web site. usefull for : - detecting or sending cookie - manually launch php3 script without browser interaction
<?php /*
**
** http_get_url.inc
**
** Daniel Boesswetter, boesswetter@peppermind.de
** http://www.peppermind.de
**
** Patrice Labracherie, tahmer@free.fr
**
**
** V1.2 Mar-08-2001
**
** - changed &$header from beeing a scalar to an array containing
** key/value pairs corresponding to the header fields and their
** contents (keys are lowercased)
**
** - added http_get_url_follow() to follow redirects (at least some :)
**
** - added &$resp_line
**
**
**
** V1.1 Mar-03-2k+1
**
** heavily based on GetURL 1.0 by tahmer@free.fr as found on
** http://www.phpbuilder.com, thanx a lot!
**
** added the $port, $req_hdr and $pver arguments and the basic_auth_hdr
** function and renamed the function to http_get_url()
**
** usage is as follows:
**
** $ret = http_get_url($host, $port, $url, $cookie, $req_hdr, $page,
** $header[, $pver]);
**
** with the $host and $port (usually 80) you want to contact, the
** absolute $path on this server, additional request-headers (an array
** containing "fieldname" => "value" pairs, e.g. login-credentials),
** variables that will contain the contents of the $page and response
** $header resp. and an optional protocol-version (defaults to HTTP/1.0)
**
** this function returns -1 on network error, 0 on protocol error and
** the HTTP-return-code on "success" (whatever that means)
**
** additionally there is a new helper function
** basic_auth_hdr($login, $password), that takes $login and $password
** and returns an array suitable for beeing passed to http_get_url()
** in the $req_hdr field.
**
** example:
**
** $ret = http_get_url("www.someserver.com", 80, "/", "",
** basic_auth_hdr("someuser", "secret"), $page, $header);
**
** $ret will be 200 if everthing went as expected, 401 if your login
** or password were wrong, 500 on an internal server error ...
**
**
*/
if (!defined("http_get_url.inc")):
define("http_get_url.inc", true);
function http_get_url($host, $port, $url, $cookie, $req_hdr, &$page,
&$header, &$resp_line, $pver="HTTP/1.0")
{
$page="";
$header = array();
$sock = fsockopen($host, $port);
if (!$sock) return -1;
// build and send request
$msg="GET $url ".$pver;
if ($cookie != "" and !$req_hdr["Cookie"])
$msg .= "\r\nCookie: $cookie";
if (!$req_hdr["Host"]) $msg .= "\r\n"."Host: $host";
if (!$reg_hdr["Accept"]) $msg .= "\r\n"."Accept: text/html";
$h = array_keys($req_hdr);
for ($i = 0; $i < count($h); $i++)
$msg .= "\r\n".$h[$i].": ".$req_hdr[$h[$i]];
$msg.="\r\n\r\n\r\n\r\n";
fputs($sock,$msg);
$chunked=0;
// read response header
$lc = 1;
$ret_code = "";
while ($buffer = fgets($sock, 4096)) {
if ($buffer == "\r\n") break;
if ($lc == 1) {
$resp_line = $buffer;
// first line of response
preg_match("/^HTTP\/\S+\s+(\d{3})\s+\w*/", $buffer, $a);
$ret_code = $a[1];
} else {
$a = split(":", $buffer);
$key = array_shift($a);
$key = strtolower($key);
$val = join(":", $a);
$val = preg_replace("/^\s+|\s+$/", "", $val);
$header[$key] = $val;
}
if (preg_match("/Transfer-Encoding:\s+(.+)\r\n/U",
$buffer, $parts))
if (strtoupper($parts[1]) == "CHUNKED") $chunked=1;
else echo "**???".$parts[1]."??\r\n";
$lc++;
}
if ($chunked == 0) {
while ($buffer = fgets($sock, 4096)) {
$page .= $buffer;
// echo $buffer;
}
} else {
while ($buffer = fgets($sock, 4096)) {
if (!preg_match("/\s*([\dABCDEFabcdef]+)\s*\r\n/i", $buffer, $parts))
{
echo "**chunk size not found : $buffer\r\n";
return 0;
}
$size = (int)base_convert(strtoupper($parts[1]), 16, 10);
if ($size == 0) break;
$buffer = fread($sock, $size);
if (strlen($buffer) != $size) {
echo "**reading : $size\r\n";
echo "**readed : ".strlen($buffer)."\r\n";
return 0;
}
$page.=$buffer;
// echo $buffer;
$buffer=fgets($sock, 4096);
if ($buffer!="\r\n") {
echo "**CRLF not found : $buffer\r\n";
return 0;
}
}
}
fclose($sock);
return $ret_code;
}
function basic_auth_hdr($login, $pw) {
return(array("Authorization" => "Basic "
.base64_encode($login.":".$pw)));
}
// follows redirects of the form http://host:port/path
function http_get_url_follow($host, $port, $url, $cookie, $req_hdr, &$page,
&$header, &$resp_line, $pver="HTTP/1.0")
{
$header = array();
$header["location"] = "http://".$host.":".$port.$url;
$ret = "300";
while (ereg("^3", $ret)) {
preg_match("/^http:\/\/([^:\/]+)(:(\d+))?(\/.*)$/", $header["location"], $f);
$my_host = $f[1];
$my_port = $f[3];
$my_url = $f[4];
$ret = http_get_url($my_host, $my_port, $my_url, $cookie, $req_hdr, &$page, &$header, &$resp_line, $pver);
}
}
endif;
?>