Curl.php

Go to the documentation of this file.
00001 <?php
00002 
00026 class Robo47_Curl
00027 {
00028 
00035     protected $_options = array();
00041     protected $_curl = null;
00047     protected $_body = '';
00048 
00053     public function __construct($url = null, $returnTransfer = true)
00054     {
00055         $this->_curl = curl_init();
00056         if ($url != null) {
00057             $this->setOption(CURLOPT_URL, $url);
00058         }
00059         $this->setOption(CURLOPT_RETURNTRANSFER, $returnTransfer);
00060     }
00061 
00067     public function setOption($option, $value)
00068     {
00069         $success = curl_setopt($this->_curl, $option, $value);
00070         if (false === $success) {
00071             $message = 'Error Setting Option: ' . (string) $option;
00072             throw new Robo47_Curl_Exception($message);
00073         }
00074         $this->_options[$option] = $value;
00075         return $this;
00076     }
00077 
00082     public function setOptions(array $options)
00083     {
00084         foreach ($options as $option => $value) {
00085             $this->setOption($option, $value);
00086         }
00087         return $this;
00088     }
00089 
00093     public function getOptions()
00094     {
00095         return $this->_options;
00096     }
00097 
00102     public function getInfo($option)
00103     {
00104         return curl_getinfo($this->_curl, $option);
00105     }
00106 
00111     public function getInfoArray(array $options)
00112     {
00113         $infos = array();
00114         foreach ($options as $option) {
00115             $infos[$option] = curl_getinfo($this->_curl, $option);
00116         }
00117         return $infos;
00118     }
00119 
00123     public function exec()
00124     {
00125         $this->_body = curl_exec($this->_curl);
00126         return $this;
00127     }
00128 
00135     public function getBody($noHeaders = true)
00136     {
00137         if (true === $noHeaders) {
00138             $pos = strpos($this->_body, "\r\n\r\n");
00139             if (false !== $pos) {
00140                 return substr($this->_body, $pos);
00141             } else {
00142                 throw new Robo47_Curl_Exception('unable to find body');
00143             }
00144         }
00145         return $this->_body;
00146     }
00147 
00152     public function getHeaders($asArray = false)
00153     {
00154         if (isset($this->_options[CURLOPT_HEADER]) &&
00155             $this->_options[CURLOPT_HEADER] == true) {
00156 
00157             $pos = strpos($this->_body, "\r\n\r\n");
00158             if ($pos !== false) {
00159                 $headers = substr($this->_body, 0, $pos);
00160             } else {
00161                 $headers = '';
00162             }
00163         } else {
00164             $headers = '';
00165         }
00166         if (true === $asArray) {
00167             $headers = explode("\n", $headers);
00168             $aHeaders = array();
00169             foreach ($headers as $line) {
00170                 $pos = strpos($line, ':');
00171                 if (false !== $pos) {
00172                     $headerName = trim(substr($line, 0, $pos));
00173                     $headerValue = trim(substr($line, $pos + 1));
00174                     $aHeaders[$headerName] = $headerValue;
00175                 }
00176             }
00177             return $aHeaders;
00178         } else {
00179             return $headers;
00180         }
00181     }
00182 
00186     public function getError()
00187     {
00188         return curl_error($this->_curl);
00189     }
00190 
00194     public function getErrno()
00195     {
00196         return curl_errno($this->_curl);
00197     }
00198 
00202     public function close()
00203     {
00204         @curl_close($this->_curl);
00205         return $this;
00206     }
00207 
00213     public function __clone()
00214     {
00215         // resource can't be cloned, so we need to init a new one
00216         $this->_curl = curl_init();
00217         // needed for setting old options to the new curl-resource
00218         $this->setOptions($this->getOptions());
00219     }
00220 
00225     public function getVersion($age = 0)
00226     {
00227         return curl_version($age);
00228     }
00229 
00233     public function __destruct()
00234     {
00235         $this->close();
00236     }
00237 
00241     public function getCurl()
00242     {
00243         return $this->_curl;
00244     }
00245 
00252     public function setCurl($curl)
00253     {
00254         if (!is_resource($curl) || get_resource_type($curl) !== 'curl') {
00255             throw new Robo47_Curl_Exception('$curl is not an curl-resource');
00256         }
00257         $this->setOptions($this->getOptions());
00258         $this->_curl = $curl;
00259     }
00260 }