Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HttpTransport extends Observable {
- protected $lastRequest = '';
- protected $lastResponse = '';
- function __construct() {
- $this->registerEvents(array('request'));
- }
- function getLastRequest() {
- return $this->lastRequest;
- }
- function getLastResponse() {
- return $this->lastResponse;
- }
- function doRequest($url, $params=null, array $curlOptions=null) {
- $this->lastRequest = '';
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_HEADER,0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FAILONERROR, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $fields = array();
- if ( !is_null($params) ) {
- if ( is_array($params) ) {
- foreach ( $params as $param => $value ) {
- $fields[] = urlencode($param) . '=' . urlencode($value);
- }
- $this->lastRequest = implode('&',$fields);
- }else{
- $this->lastRequest = $params;
- }
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $this->lastRequest);
- }
- if ( $curlOptions ) {
- foreach ( $curlOptions as $option => $value ) {
- curl_setopt($ch, $option, $value);
- }
- }
- $this->lastResponse = curl_exec($ch);
- $errno = curl_errno($ch);
- $error = curl_error($ch);
- curl_close($ch);
- $this->fireEvent('request', $url, ($errno ? false : true),
- print_r($params,true),$this->lastResponse);
- if ( $errno ) {
- throw new HttpTransportException($error);
- }
- return $this->lastResponse;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement