Class HttpRequest
{
var $sHostAdd;
var $sUri;
var $iPort;
var $sRequestHeader;
var $sResponse;
function HttpRequest ($SURL) {
$sPatternUrlPart = '/http://([a-z-.0-9]+) (:(d+)) {0,1} (. *)/I ';
$arMatchUrlPart = Array ();
Preg_match ($sPatternUrlPart, $SURL, $arMatchUrlPart);
$this->shostadd = gethostbyname ($arMatchUrlPart [1]);
if (Empty ($arMatchUrlPart [4])) {
$this->suri = '/';
}else{
$this->suri = $arMatchUrlPart [4];
}
if (Empty ($arMatchUrlPart [3])) {
$this->iport = 80;
}else{
$this->iport = $arMatchUrlPart [3];
}
$this->addrequestheader (' Host: '. $arMatchUrlPart [1]);
$this->addrequestheader (' connection:close ');
}
function Addrequestheader ($sHeader) {
$this->srequestheader. = Trim ($sHeader). ";
}
function SendRequest ($sMethod = ' get ', $sPostData = ') {
$sRequest = $sMethod. " ". $this->suri." http/1.1 ";
$sRequest. = $this->srequestheader;
if ($sMethod = = ' POST ') {
$sRequest. = "content-type:application/x-www-form-urlencoded";
$sRequest. = "Content-length:". strlen ($sPostData). ";
$sRequest. = "";
$sRequest. = $sPostData. " ";
}
$sRequest. = "";
$sockHttp = Socket_create (Af_inet, Sock_stream, sol_tcp);
if (! $sockHttp) {
Die (' Socket_create () failed! ');
}
$resSockHttp = Socket_connect ($sockHttp, $this->shostadd, $this->iport);
if (! $resSockHttp) {
Die (' Socket_connect () failed! ');
}
Socket_write ($sockHttp, $sRequest, strlen ($sRequest));
$this->sresponse = ';
while ($sRead = Socket_read ($sockHttp, 4096)) {
$this->sresponse. = $sRead;
}
Socket_close ($sockHttp);
}
function GetResponse () {
return $this->sresponse;
}
function Getresponsebody () {
$sPatternSeperate = '//';
$arMatchResponsePart = Preg_split ($sPatternSeperate, $this->sresponse, 2);
return $arMatchResponsePart [1];
}
function Getresponsehead () {
$sPatternSeperate = '//';
$arMatchResponsePart = Preg_split ($sPatternSeperate, $this->sresponse, 2);
return $arMatchResponsePart [0];
}
}