R. Rajesh Jeba Anbiah wrote:
If you don't mind to share your thoughts, you can post a piece/part
of your code so that it helps others.
Hi,
sure, I found it between some old pizza boxes;)
I have not tested it again, so I don't know if it will work with a more
recent PHP version. Thus it might not work, it might have bugs, but it
provides a starting point.
Have fun
Frank
When I recall correctly, you can use it as follows:
$this->Connection=n ew HTTPConnection( new URL("http:://www.whatever.co m"),
"", 30, NONBLOCKING);
if (!$this->Connection->makeGETRequest ())
die($this->Connection->getErrorMessag e());
while($this->Connection->readBlock())
{}
print $this->Connection->getContents( );
file http_class.inc. php
<?php
include_once "class.url.inc. php";
//define constants for use with choice of blocking/nonblocking mode
/** @global string For use with HTTPConnection constructor, sets connection
blocking */
define ("BLOCKING", 1);
/** @global string For use with HTTPConnection constructor, sets connection
nonblocking */
define ("NONBLOCKIN G", 0);
/** Objects of this class provide a way to access http-resources.
* Currently only the GET-Method is supported
*/
class HTTPConnection
{
//-----------------
//-- public methods
/** Constructor creates an unconnected object and initializes
member-variables
* @acces public
* @param &$url a reference to an url-object
* @param $sendhd string containing a valid http-header to send along,
default value is ""
* @param $timeout numeric timeout in seconds for the connect call of
fsockopen, default is 1s
* @param $blocking boolean flag, if set to true (default) the socket
will be opened in blocking mode
*/
function HTTPConnection( &$url, $sendhd="", $timeout=1,
$blockmode=BLOC KING)
{
$this->Status=false ;
$this->URL = $url;
$this->IP = false;
$this->Socket=false ;
$this->Timeout=$timeo ut;
$this->Buffer=false ;
$this->Header=false ;
$this->Contents=false ;
$this->SendHeader=$se ndhd;
$this->Blocking=$bloc kmode;
}
/** This method opens a socket to a http-resource and sends a GET-Request
* If the connection is opened in blocking mode, the respond of the
resource
* is fetched, parsed and stored in the internal buffers
* When using an unblocking connection, the client has to call readBlock
to fetch
* the respond
*
* @access public
* @returns true if the request was successfull, false otherwise
* @see readBlock()
*/
function makeGETRequest( )
{
//make dns-lookup and connect socket
if ($this->findIP())
if($this->openSocket() )
{
$path=$this->URL->getPath();
if (strlen($qs=$th is->URL->getQueryString ()))
$path.="?$qs";
$str="GET $path HTTP/1.0\r\nHost:".$ this->URL->getHost()."\r\ n"
$this->SendHeader."\r \n\r\n";
//write http-request
if (fputs($this->Socket, $str) != strlen($str))
{
$this->ErrorMessage=" Unknown error writing request to socket";
return false;
}
//clear buffer
$this->Buffer="";
//when using blocking mode fetch, parse and store respond
if ($this->Blocking)
{
while (!feof($this->Socket))
$this->Buffer.=fread( $this->Socket, 1024);
return $this->parseBuffer( );
}
return true;
}
return false;
}
/** Returns the header of the repond from the last http-request as an
associative array
*
* @access public
* @returns array
*/
function getHeader()
{
return $this->Header;
}
/** Returns the body of the repond from the last http-request
*
* @access public
* @returns string
*/
function getContents()
{
return $this->Contents;
}
/** Returns the textual description of the last error, may be an empty
string, if no error occured so far
*
* @access public
* @returns string
*/
function getErrorMessage ()
{
return $this->ErrorMessage ;
}
/** Reads $blocksize bytes from socket and appends them to internal buffer
*
* @access public
* @returns false if an error occured, false otherwise
*/
function readBlock($bloc ksize=1024)
{
$this->Buffer.=fread( $this->Socket, $blocksize);
if (!feof($this->Socket))
return true;
$this->parseBuffer( );
return false;
}
/** Closes the socket
*
* @access public
* @returns true if the socket could be closed, false otherwise
*/
function close()
{
return fclose($this->Socket);
}
//-----------------
//-- private methods
/** Look up IP of host and store it
*
* @access private
* @returns IP when dns-lookup was succesfull, false otherwise
*/
function findIP()
{
$h=$this->URL->getHost();
if (ereg("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}", $h))
return $this->IP=$h;
if ( ($ip=gethostbyn ame($h)) == $h )
{
$this->ErrorMessage=" Could not find DNS-Entry for host: $h";
return false;
}
return $this->IP=$ip;
}
/** Opens a socket
*
* @access private
* @returns true if the socket could be opened, false otherwise
*/
function openSocket()
{
if ($this->Socket=fsockop en($this->IP, $this->URL->getPort(), &$errno,
&$errstr, $this->Timeout))
{
socket_set_bloc king($this->Socket, $this->Blocking);
return true;
}
if (!strlen($errst r))
$this->ErrorMessage=" Unknown error opening Socket";
else
$this->ErrorMessage=" Error $errno opening Socket: $errstr";
return false;
}
/** Parse http-respond as stored in buffer into header and body
*
* @access private
* @returns true
*/
function parseBuffer()
{
$this->Header="";
if ( ($p=strpos($thi s->Buffer, "\r\n\r\n"))!== false )
$this->parseHeader(su bstr($this->Buffer, 0, $p));
else
$p=-4;
$this->Contents=subst r($this->Buffer, $p+4);
return true;
}
/** Parse http-header into associative array
* The array keys are the names of the headerfields in uppercase letters
*
* @access priavate
* @param $hd string holding the http-header
* @returns void
*/
function parseHeader($hd )
{
$lines=explode( "\n", $hd);
$n=count($lines );
for ($i=0; $i<$n; ++$i)
if (strlen(trim($l ines[$i])))
{
$parts=split("^[a-zA-Z0-9_-]+:", $lines[$i]);
if (count($parts)> 1)
{
$key=substr($li nes[$i], 0, strpos($lines[$i], ":"));
$this->Header[strtoupper(trim ($key))]=trim($parts[1]);
}
}
}
};
?>
file class.url.inc.p hp
<?php
function URL($urlstr)
{
$u=trim($urlstr );
if (strlen($u))
$this->parseURL($u) ;
}
function parseURL($urlst r)
{
$this->IsValid=fals e;
//--- ist ein Protokol angegeben?
$parts=explode( ":/", $urlstr);
if (count($parts)< 2)
$urlstr="http://$urlstr";
$parts=@parse_u rl($urlstr);
$this->Protocol = strtolower($par ts["scheme"]);
$this->Host = strtolower($par ts["host"]);
$this->Path = $parts["path"];
$this->Port = $parts["port"];
$this->Query = $parts["query"];
$this->Fragment = $parts["fragment"];
$this->User = $parts["user"];
$this->Password = $parts["pass"];
if (!strlen($this->Protocol))
$this->Protocol="http ";
if (!strlen($this->Port))
$this->Port="80";
if (!strlen($this->Path))
$this->Path="/";
if ($this->Host)
$this->IsValid=true ;
else return false;
$this->buildCanonical ();
return true;
}
function getHost() { return $this->Host; }
function getPath(){ return $this->Path; }
function getPort() { return $this->Port; }
function getQueryString( ){ return $this->Query;}
function getAsString()
{
return $this->Canonical;
}
function buildCanonical( )
{
$this->Canonical= strtolower($thi s->Protocol).
"://".
strtolower($thi s->Host);
if ($this->Port!="80")
$this->Canonical.=":" .$this->Port;
$this->Canonical.=$th is->Path;
if (strlen($this->Query))
$this->Canonical.="?" .$this->Query;
if (strlen($this->Fragment))
$this->Canonical.="#" .$this->Fragment;
return true;
}
function isValid()
{
return $this->IsValid;
}
var $Canonical=fals e;
var $IsValid=false;
var $Host="";
var $Port=80;
var $Protocol="http ";
var $Path="/";
}
?>