<?php
 
class httpclient{
 
//  global $_SERVER;
 
  public $port=0; // socket port
 
  public $host=''; // host www.example.net
 
  public $path=''; // PATH /foo.php
 
  public $dirname='';
 
  public $filename='';
 
  public $scheme='';
 
  public $argument=''; // ?foo=bar&foo1=bar
 
  public $post_data=''; // foo=bar&foo1=bar
 
  public $request_header=''; // header sent to server
 
  public $cookie_array=array(); // array('ssid' => 'ssid', 'foo' => 'bar')
 
  public $response_data=''; // data returned from server
 
  public $response_header=''; // header data from server
 
  public $response_body='';
 
  public $response_status=0;
 
  
 
  function open($url,$post_data=''){
 
  $this->port=(parse_url($url, PHP_URL_PORT) ? parse_url($url, PHP_URL_PORT) : 80); //set port
 
  $this->host=parse_url($url, PHP_URL_HOST); 
 
  $this->path=parse_url($url, PHP_URL_PATH);
 
  $this->dirname=dirname($this->path);
 
  $this->filename=dirname($this->path);
 
  $this->argument=parse_url($url, PHP_URL_QUERY);
 
  $this->scheme=parse_url($url, PHP_URL_SCHEME);
 
  $this->post_data=$post_data;
 
 
  $this->request_header=($this->post_data=='' ? $this->get_request_header($this->host,$this->path.'?'.$this->argument,$this->cookie_to_text($this->cookie_array)) : $this->post_request_header($this->host,$this->path.'?'.$this->argument,$this->post_data,$this->cookie_to_text($this->cookie_array)));
 
  $this->response_data=$this->socket_open($this->host,$this->port,$this->request_header);
 
  $this->response_header=$this->get_response_header($this->response_data);  
 
  $this->cookie_array=$this->get_cookie($this->response_header);
 
  $this->response_status=$this->get_status($this->response_header);
 
  $this->response_body=$this->get_response_body($this->response_data);
 
 }
 
 
  function get_request_header($host,$path,$cookie){
 
  
 
  $header='';
 
  $header.="GET $path HTTP/1.1";
 
  $header.="\r\n";
 
  $header.="Host: $host";
 
  $header.="\r\n";
 
  $header.="User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; sk; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"; 
 
//  $header="User-Agent: ".$_SERVER['HTTP_USER_AGENT'];
 
  $header.="\r\n";
 
  $header.="Accept: */*";
 
  $header.="\r\n";
 
  $header.="Content-Type: application/x-www-form-urlencoded";
 
  $header.="\r\n";
 
  $header.="Keep-Alive: 300";
 
  $header.="\r\n";
 
  $header.="Connection: keep-alive";
 
  $header.="\r\n";
 
  $header.="Pragma: no-cache";
 
  $header.="\r\n";
 
  $header.="Cache-Control: no-cache";
 
  $header.="\r\n";
 
  $header.="Cookie: $cookie";
 
  $header.="\r\n";
 
  $header.="\r\n";
 
 
 
  return $header;
 
 }
 
 
 
   function post_request_header($host,$path,$post_data,$cookie){
 
  $post_data_length=strlen($post_data);
 
  
 
  $header='';
 
  $header.="POST $path HTTP/1.1";
 
  $header.="\r\n";
 
  $header.="Host: $host";
 
  $header.="\r\n";
 
  $header.="User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; sk; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"; 
 
//  $header="User-Agent: ".$_SERVER['HTTP_USER_AGENT'];
 
  $header.="\r\n";
 
  $header.="Accept: */*";
 
  $header.="\r\n";
 
  $header.="Content-Length: $post_data_length";
 
  $header.="\r\n";
 
  $header.="Content-Type: application/x-www-form-urlencoded";
 
  $header.="\r\n";
 
  $header.="Keep-Alive: 300";
 
  $header.="\r\n";
 
  $header.="Connection: keep-alive";
 
  $header.="\r\n";
 
  $header.="Pragma: no-cache";
 
  $header.="\r\n";
 
  $header.="Cache-Control: no-cache";
 
  $header.="\r\n";
 
  $header.="Cookie: $cookie";
 
  $header.="\r\n";
 
  $header.="\r\n";
 
  $header.="$post_data";
 
 
 
  return $header;
 
 }
 
 
  function cookie_to_text($cookie_array){
 
  $cookie_to_text='';
 
  foreach($cookie_array as $key => $value) { $cookie_to_text.=$key.'='.$value;}
 
  return $cookie_to_text;
 
 }
 
 
  function socket_open($host,$port,$header){
 
  $socket_open = '';
 
  $fp = fsockopen($host, $port);
 
      fputs($fp, $header);
 
      while(!feof($fp)) {
 
        $socket_open .= fgets($fp, 128); // receive the results of the request
 
        }
 
      fclose($fp);   // close the socket connection:
 
    return $socket_open;
 
 }
 
 
 
  function get_response_header($response){
 
  $header=explode( "\r\n\r\n" , $response,2);
 
  return $header[0];
 
 }
 
 
 
  function get_cookie($header){
 
  $cookie=array();
 
  $lines=explode("\r\n",$header);
 
  foreach($lines as $line) {
 
   $fields=explode(" ",$line);
 
   $columns=explode("=",$fields[1]);
 
   if ($fields[0]=='Set-Cookie:') {$cookie[$columns[0]]=$columns[1];}
 
  }
 
  return $cookie;
 
 }
 
 
 
  function get_status($header){
 
  preg_match("/(HTTP[^ ]*) ([^ ]*) ([^\r\n]*)/m", $header,$status);
 
  return $status[2]; 
 
 }
 
 
 
  function get_response_body($response){
 
  $body=explode( "\r\n\r\n" , $response,2);
 
  return $body[1];
 
 }
 
  function save($file){
 
  $fp=Fopen($file,'w');
 
   Fputs($fp,$this->request_body);
 
   Fclose($fp); 
 
  }
 
  
 
 function show_data(){
 
 echo $this->response_body;
 
 }
 
  
 
}
 
 
?>
 
 
 |