Advertisement
Guest User

WebClient

a guest
Feb 27th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.61 KB | None | 0 0
  1. <?php
  2. class WebClient {
  3.     private $ch;
  4.     private $url;
  5.     private $headers;
  6.     private $content;
  7.     private $cookies = array();
  8.     private $info;
  9.     private $queryString;
  10.    
  11.     ###############
  12.     ### SETTERS ###
  13.     ###############
  14.    
  15.     public function SetUrl($url){
  16.         if (!is_string($url))
  17.             throw new InvalidArgumentException('WebClient::SetQueryString expects parameter 1 to be string. '.gettype($queryString).' given.');
  18.        
  19.         $this->url = $url;
  20.     }
  21.    
  22.     public function SetQueryString($queryString) {
  23.         if (!is_string($queryString))
  24.             throw new InvalidArgumentException('WebClient::SetQueryString expects parameter 1 to be string. '.gettype($queryString).' given.');
  25.        
  26.         $this->queryString = $queryString;         
  27.     }
  28.    
  29.     public function SetOpt($option, $value) {      
  30.         curl_setopt($this->ch, $option, $value);
  31.     }
  32.    
  33.     ###############
  34.     ### GETTERS ###
  35.     ###############
  36.    
  37.     public function GetUrl() {
  38.         return $this->url;
  39.     }
  40.    
  41.     public function GetQueryString() {
  42.         return $this->queryString;
  43.     }
  44.    
  45.     public function GetUrlWithQueryString() {
  46.         $return = $this->url;
  47.        
  48.         if ($this->queryString !== null) {
  49.             if (strpos($return, '?') !== false) {
  50.                 $return .= '&'.$this->queryString;
  51.             }
  52.             else {
  53.                 $return .= '?'.$this->queryString;
  54.             }
  55.         }
  56.        
  57.         return $return;
  58.     }
  59.    
  60.     public function GetHeaders() {
  61.         return $this->headers;
  62.     }
  63.    
  64.     public function GetContent() {
  65.         return $this->content;
  66.     }
  67.    
  68.     public function GetCookies() {
  69.         return $this->cookies;
  70.     }
  71.    
  72.     public function GetCookiesAsString() {
  73.         $cookies = array();
  74.        
  75.         foreach ($this->cookies as $key => $value) {
  76.             $cookies[] = $key.'='.$value;
  77.         }
  78.        
  79.         return implode(';', $cookies);
  80.     }
  81.    
  82.     public function GetInfo() {
  83.         return $this->info;
  84.     }
  85.    
  86.     ######################
  87.     ### Public Methods ###
  88.     ######################
  89.    
  90.     public function MakePostRequest($values) {
  91.         if (!is_array($values) && !is_string($values))
  92.             throw new InvalidArgumentException('WebClient::MakePostRequest expects parameter 1 to be string or array. "'.gettype($values).'" given."');
  93.        
  94.         $this->SetOpt(CURLOPT_POST, true);
  95.         $this->SetOpt(CURLOPT_POSTFIELDS, $values);
  96.         return $this->Execute();
  97.     }
  98.    
  99.     public function MakeGetRequest() {
  100.         return $this->Execute();
  101.     }
  102.    
  103.     ############
  104.     ### Ctor ###
  105.     ############
  106.    
  107.     public function __construct($url = null){
  108.         $this->url = $url;
  109.         $this->InitializeResource();
  110.     }
  111.    
  112.     ###############
  113.     ### Helpers ###
  114.     ###############
  115.    
  116.     private function InitializeResource() {
  117.         $this->ch = curl_init();
  118.         $this->SetOpt(CURLOPT_HEADER, 1);
  119.         $this->SetOpt(CURLOPT_RETURNTRANSFER, 1);
  120.     }
  121.    
  122.     private function Execute() {
  123.         $this->SetOpt(CURLOPT_URL, $this->GetUrlWithQueryString());
  124.         $this->SetOpt(CURLOPT_COOKIE, $this->GetCookiesAsString());
  125.        
  126.         $response = curl_exec($this->ch);
  127.         $info = curl_getinfo($this->ch);
  128.         curl_close($this->ch);
  129.        
  130.         if ($response === false)
  131.             return false;
  132.        
  133.         $this->info = $info;
  134.         $this->headers = mb_substr($response, 0, $this->info['header_size']);
  135.         $this->content = mb_substr($response, $this->info['header_size'], mb_strlen($response));
  136.         $this->GetCookiesFromHeader();
  137.        
  138.         $this->InitializeResource();
  139.         return true;
  140.     }
  141.    
  142.     private function GetCookiesFromHeader() {
  143.         $headerInfo = $this->ParseHTTPHeaders();
  144.         $cookieInfo = isset($headerInfo['Set-Cookie']) ? $headerInfo['Set-Cookie'] : null;
  145.         $this->GetCookiesFromCookieInfo($cookieInfo);
  146.     }
  147.    
  148.     private function GetCookiesFromCookieInfo($cookieInfo)
  149.     {
  150.         if (is_string($cookieInfo)) {
  151.             $items = explode(';', $cookieInfo);
  152.            
  153.             foreach ($items as $item) {
  154.                 preg_match_all('/\s*(\w+)\s*=\s*(\w+)/', $item, $matches); //Regex might be improved
  155.                
  156.                 if (count($matches[1]) > 0 && count($matches[2]) > 0)
  157.                     $this->cookies = array_merge(array_combine($matches[1], $matches[2]), $this->cookies);
  158.             }
  159.         } else if (is_array($cookieInfo)) {
  160.             foreach ($cookieInfo as $value) {
  161.                 $this->GetCookiesFromCookieInfo($value);
  162.             }
  163.         }
  164.     }
  165.    
  166.     private function ParseHTTPHeaders() {
  167.         $return = array();
  168.         $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $this->headers));
  169.        
  170.         foreach($fields as $field) {
  171.             if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
  172.                 $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
  173.                
  174.                 if( isset($return[$match[1]]) ) {
  175.                     $return[$match[1]] = array($return[$match[1]], $match[2]);
  176.                 } else {
  177.                     $return[$match[1]] = trim($match[2]);
  178.                 }
  179.             }
  180.         }
  181.        
  182.         return $return;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement