Advertisement
nuit

HTTPRequest.class

Mar 20th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.41 KB | None | 0 0
  1. define('ASARRAY',1);
  2.  
  3. class HTTPRequest extends Socket {
  4.  
  5.     // Set Private Variables
  6.     private $recvHeader,$recvContent;
  7.     private $parsedHeader=array();
  8.     private $sendHeader=array();
  9.     private $PostVars=array();
  10.     private $GetVars=array();
  11.     private $method='GET';
  12.     private $StatusCode;
  13.  
  14.     function setMethod( $method='GET' ) {
  15.         $method = strtoupper($method);
  16.         $this->method = $method;
  17.         return $method;
  18.     }
  19.  
  20.     function setHeader( $key,$value ) {
  21.         if(is_array($key) AND is_array($value) AND count($key) == count($value)) {
  22.             for($i = 0; $i < count($key); $i++) {
  23.                 $this->sendHeader[$key[$i]] = $value[$i];
  24.             }
  25.  
  26.             return true;
  27.         } elseif(!is_array($key) AND !is_array($value)) {
  28.             $this->sendHeader[$key] = $value;
  29.  
  30.             return true;
  31.         }
  32.  
  33.         return false;
  34.     }
  35.  
  36.     function setVars( $method,$key,$elem ) {
  37.         if(is_array($key) AND is_array($elem) AND count($key) == count($elem)) {
  38.             for($i = 0; $i < count($key); $i++) {
  39.                 $this->setVars( $method,$key[$i], $elem[$i] );
  40.             }
  41.         } else {
  42.             if(strtoupper($method) == 'GET') {
  43.                 $this->GetVars[$key] = $elem;
  44.             } elseif(strtoupper($method) == 'POST') {
  45.                 $this->PostVars[$key] = $elem;
  46.             }
  47.         }
  48.         return true;
  49.     }
  50.  
  51.     function PassVarsThru( $method,$vars ) {
  52.         if(strtoupper($method) == 'GET') {
  53.             $this->GetVars = array_merge($this->GetVars,$vars);
  54.         } elseif(strtoupper($method) == 'POST') {
  55.             $this->PostVars = array_merge($this->PostVars,$vars);
  56.         }
  57.  
  58.         return true;
  59.     }
  60.  
  61.     function fetchURL( $site, $method='GET',$port=80 ) {
  62.         $this->setMethod(empty($this->method) ? $method : $this->method);
  63.  
  64.         $parsed_url = parse_url($site);
  65.  
  66.         if($parsed_url['query']) {
  67.             $pairs = explode("&",$parsed_url);
  68.             foreach($pairs as $pair) {
  69.                 $split = explode('=',$pair);
  70.                 $this->addVar( 'GET', $split[0], $split[1] );
  71.             }
  72.         }
  73.  
  74.         if($this->connect($parsed_url['host'],$port)) {    
  75.             $this->send($this->_makeHeader($parsed_url['host'],$parsed_url['path']));
  76.  
  77.             if($this->_splitHeaderContent( $this->read() )) {
  78.                 $this->_statuscode();
  79.                 $this->_interpretHeader();
  80.                
  81.                 return $this->recvContent;
  82.                
  83.                 /*if(preg_match('/^30\d/',$this->getStatusCode)) {
  84.                     return $this->fetchURL( $this->parsedHeader['Location'] );
  85.                 } elseif(preg_match('/^20\d/',$this->getStatusCode)) {
  86.                     return $this->recvContent;
  87.                 }*/
  88.             }
  89.         }
  90.     }
  91.    
  92.     function getHeader( $mod ) {
  93.         return ($mod ? $this->parsedHeader : $this->recvHeader);
  94.     }
  95.  
  96.     function getContent( ) {
  97.         if(!empty($this->recvContent)) {
  98.             return $this->recvContent;
  99.         } else {
  100.             return false;
  101.         }
  102.     }
  103.  
  104.     function getStatusCode( ) {
  105.         return (empty($this->StatusCode) ? $this->_statuscode() : $this->StatusCode);
  106.     }
  107.  
  108.     private function _makeHeader( $host,$path ) {
  109.         $getvars = $this->_makeVars('GET');
  110.         $buffer = (empty($this->method) ? 'GET' : $this->method).' '.$path.(!empty($getvars) ? '?'.$getvars : '').' HTTP/1.0'."\r\n";
  111.        
  112.         $buffer .= 'Host: '.$host."\r\n";
  113.  
  114.         if($this->method == 'POST') {
  115.             $postvars = $this->_makeVars('POST');
  116.             $this->addHeader( 'Content-Type','application/x-www-form-urlencoded');
  117.             $this->addHeader( 'Content-length',strlen($postvars));
  118.         }
  119.  
  120.         if(count($this->sendHeader) != 0) {
  121.             foreach($this->sendHeader as $key=>$value) {
  122.                 $buffer .= $key.': '.$value."\r\n";
  123.             }
  124.         }
  125.  
  126.         if($this->method == 'POST') {
  127.             $buffer .= "\r\n".$postvars;
  128.         }
  129.         #print $buffer;
  130.        return $buffer;
  131.     }
  132.    
  133.     private function _interpretHeader( ) {
  134.         preg_match_all("/([A-Za-z\-]+): (.*?)\r\n/",$this->recvHeader,$match);
  135.  
  136.         for($i = 0; $i < count($match[1]); $i++) {
  137.             $this->parsedHeader[$match[1][$i]] = $match[2][$i];
  138.         }
  139.         return $this->parsedHeader;
  140.     }
  141.    
  142.     private function _splitHeaderContent( $buffer ) {
  143.         $this->recvHeader = substr($buffer,0,strpos($buffer,"\r\n\r\n"));
  144.         $this->recvContent = trim(substr($buffer,strpos($buffer,"\r\n\r\n")));
  145.  
  146.         return true;
  147.     }
  148.  
  149.     private function _statuscode( ) {
  150.         $arrHeader = explode("\r\n",$this->recvHeader);
  151.         $this->parsedHeader['Query'] = trim($arrHeader[0]);
  152.  
  153.         preg_match_all("/HTTP\/\d\.\d (\d{3}) ([a-zA-Z\s\-]+)/",$arrHeader[0],$match);
  154.  
  155.         $this->StatusCode = $match[1][0];
  156.  
  157.         return $match[1][0];
  158.     }
  159.  
  160.     private function _makeVars( $method ) {
  161.         $vars = (strtoupper($method) == 'GET' ? $this->GetVars : (strtoupper($method) == 'POST' ? $this->PostVars : ''));
  162.  
  163.         if(!empty($vars)) {
  164.             $output = '';
  165.             foreach($vars as $key=>$elem) {
  166.                 $output .= $key.'='.$elem.'&';
  167.             }
  168.  
  169.             return substr($output,0,-1);
  170.         } else {
  171.             return false;
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement