Rabu, 04 April 2012

Class HTTP Query





in this example, i have create a class of httpquery. It script is very simple used.
Bellow is code:
(save as class.httprequest.php or etc you want)


<?php
#By ArRay Dec 2nd, 2010
class HTTPRequest {
 public $URL;
 public $POST   = false;
 public $GET    = true;
 public $Data;
 public $Port   = 80;
 public $Response;
 public $Referer  = "www.ExploreCrew.org";
 public $Accept  = "*/*";
 public $UserAgent = "NetExplorer/5.0";
 public $Connection = "Close";
 
 function Query(){
  $url = str_replace("http://","",$this->URL);
  $host = explode("/",$url);
  for($i=1;$i<count($host);$i++){
   $path .= "/".$host[$i];
  }
  if(!$this->POST) $path .= "?".$this->Data;
  $sock = @fsockopen($host[0],$this->Port,$errno,$errstr,30);
  if ($sock) {
   $request   = (($this->POST)?"POST":"GET")." ".$path." HTTP/1.1\r\n";
   $request  .= "Host: ".$host[0]."\r\n";
   $request  .= "Referer: ".$this->Referer."\r\n";
   $request  .= "Accept: ".$this->Accept."\r\n";
   $request  .= "User-Agent: ".$this->UserAgent."\r\n";
   $request  .= ($this->POST)?"Content-type: application/x-www-form-urlencoded\r\n":"";
   $request  .= ($this->POST)?"Content-length: ".strlen($this->Data)."\r\n":"";
   $request  .= "Connection: ".$this->Connection."\r\n\r\n";
   @fputs($sock,$request);
   if($this->POST)@fputs($sock,$this->Data);
   while (!@feof($sock)) { 
    $output .= @trim(@fgets($sock, 3600))."\n";            
   }
   @fclose($sock);
  }
  else {
   $output = "Can not connect to ".$host[0]." on port ".$this->Port;
  }
  $this->Response = $output;
 }
}
?>



Then, How to use it? See Example bellow.


# Using GET method


By default, this code is using get method. So it can used without no option.


<?php

include_once("class.httprequest.php");  //include class 

$http = new HTTPRequest; //create an object of httpquery
$http->URL = "www.your-site.com/action.php"; //page you want to request
$http->Data = "user=ArRay&id=4&val=zzz"; //a variable you want to send by GET method
$http->Query(); //do queries

if($http->Response){ //if request is done
 echo "done";
 echo $http->Response; // do it if you want to know the server response
}
exit;

?>


that queries same as:

http://www.your-site.com/action.php?user=ArRay&id=4&val=zzz



# Using POST method


to POST data to the server, just activate post method by add line bellow:


$http->POST = true;


then, all data you want to post, write in line. example:


Name=ArRay&Address=Kediri


it mean as:

Post variable Name with value ArRay
and variable Address with value Kediri

see example

<?php

include_once("class.httprequest.php");

$http = new HTTPRequest; 
$http->URL = "www.your-site.com/action.php"; 
$http->Data = "Name=ArRay&Address=Kediri"; //a variable you want to send by POST method
$http->POST = true;  // activate post method
$http->Query(); 

if($http->Response){ 
 echo "done";
 echo $http->Response; 
}
exit;

?>

sumber : Class HTTP Query

Posting Lebih Baru Posting Lama Beranda

0 komentar:

Posting Komentar