#native_company# #native_desc#
#native_cta#

getting an url

By Daniel Bwetter
on March 9, 2001

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, [email protected]
** http://www.peppermind.de
**
** Patrice Labracherie, [email protected]
**
**
** 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 [email protected] as found on
** https://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 .= "rnCookie: $cookie";

	if (!$req_hdr["Host"]) $msg .= "rn"."Host: $host";
	if (!$reg_hdr["Accept"]) $msg .= "rn"."Accept: text/html";

	$h = array_keys($req_hdr);
	for ($i = 0; $i < count($h); $i++)
	    $msg .= "rn".$h[$i].": ".$req_hdr[$h[$i]];

	$msg.="rnrnrnrn";
	fputs($sock,$msg);

	$chunked=0;

	// read response header

	$lc = 1;
	$ret_code = "";
	while ($buffer = fgets($sock, 4096)) {

		if ($buffer == "rn") 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+(.+)rn/U",
			$buffer, $parts))
		    if (strtoupper($parts[1]) == "CHUNKED") $chunked=1;
		    else echo "**???".$parts[1]."??rn";

		$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*rn/i", $buffer, $parts))
			{
				echo "**chunk size not found : $bufferrn";
				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 : $sizern";
				echo "**readed : ".strlen($buffer)."rn";
				return 0;
			}

			$page.=$buffer;
//			echo $buffer;
			$buffer=fgets($sock, 4096);
			if ($buffer!="rn") {
			    echo "**CRLF not found : $bufferrn";
			    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;

?>