Advertisement
Guest User

HttpTransport

a guest
Sep 4th, 2010
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. class HttpTransport extends Observable {
  2.     protected $lastRequest = '';
  3.     protected $lastResponse = '';
  4.    
  5.     function __construct() {
  6.         $this->registerEvents(array('request'));
  7.     }
  8.    
  9.     function getLastRequest() {
  10.         return $this->lastRequest;
  11.     }
  12.    
  13.     function getLastResponse() {
  14.         return $this->lastResponse;
  15.     }
  16.    
  17.     function doRequest($url, $params=null, array $curlOptions=null) {
  18.         $this->lastRequest = '';
  19.        
  20.         $ch = curl_init($url);
  21.         curl_setopt($ch, CURLOPT_HEADER,0);
  22.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  23.         curl_setopt($ch, CURLOPT_FAILONERROR, true);
  24.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  25.        
  26.         $fields = array();
  27.         if ( !is_null($params) ) {
  28.             if ( is_array($params) ) {
  29.                 foreach ( $params as $param => $value ) {
  30.                     $fields[] = urlencode($param) . '=' . urlencode($value);
  31.                 }
  32.                 $this->lastRequest = implode('&',$fields);
  33.             }else{
  34.                 $this->lastRequest = $params;
  35.             }
  36.             curl_setopt($ch, CURLOPT_POST, true);
  37.             curl_setopt($ch, CURLOPT_POSTFIELDS, $this->lastRequest);
  38.         }
  39.         if ( $curlOptions ) {
  40.             foreach ( $curlOptions as $option => $value ) {
  41.                 curl_setopt($ch, $option, $value);
  42.             }
  43.         }
  44.         $this->lastResponse = curl_exec($ch);
  45.         $errno = curl_errno($ch);
  46.         $error = curl_error($ch);
  47.         curl_close($ch);
  48.         $this->fireEvent('request', $url, ($errno ? false : true),
  49.             print_r($params,true),$this->lastResponse);
  50.         if ( $errno ) {
  51.             throw new HttpTransportException($error);
  52.         }
  53.         return $this->lastResponse;
  54.     }
  55.    
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement