_ch = curl_init($url); $this->_options = array(); } /** * Get cURL options * @param name cURL option **/ public function __get($name) { $result = NULL; if (defined($name)) { $value = constant($name); if (isset($this->_options[$value])) { $result = $this->_options[$value]; } } return $result; } /** * Set cURL options * @param name cURL option * @param value new cURL option value **/ public function __set($name, $value) { if (defined($name) && preg_match('/^CURLOPT_(?!POSTFIELDS)/', $name)) { $this->_options[constant($name)] = $value; } else { throw new Exception("Option '$name' invalid or protected"); } } /** * cURL option definition * @param name cURL option **/ public function __isset($name) { return (defined($name) && isset($this->_options[constant($name)])); } /** * Destroy cURL option definition * @param name cURL option **/ public function __unset($name) { if (defined($name) && isset($this->_options[constant($name)])) { unset($this->_options[constant($name)]); } } /** * Description of object **/ public function __toString() { return sprintf("%s (%s)", __CLASS__, curl_getinfo($this->_ch, CURLINFO_EFFECTIVE_URL)); } /** * Timeout of query execution * @param timeout in seconds **/ public function setTimeout($timeout) { $timeout = intval($timeout); if ($timeout > 0) { $this->CURLOPT_TIMEOUT = $timeout; $this->CURLOPT_CONNECTTIMEOUT = $timeout; } } /** * Add post data * @param field_name name of the field * @param value value of the field **/ public function addPostData($field_name, $value) { if (!isset($this->_post[$field_name]) && !is_array($value)) { $this->_post[$field_name] = $value; return TRUE; } else { return FALSE; } } /** * Add post file * @param field_name name of the field in $_FILES * @param file file to send **/ public function addPostFile($field_name, $file) { if (is_file($file)) { //$this->_post[$field_name] = '@' . realpath($file); $this->_post[$field_name] = new CurlFile(realpath($file)); } else { throw new Exception("The file '$file' not exists"); } } /** * Execute query * @param output_fil write in the output file **/ public function doRequest($output_file = FALSE) { if ($this->_options) { if (function_exists('curl_setopt_array')) { curl_setopt_array($this->_ch, $this->_options); } else { foreach ($this->_options as $option => $value) { curl_setopt($this->_ch, $option, $value); } } } if ($output_file) { @ $fp = fopen($output_file, 'w'); if (!$fp) { throw new Exception("We can't open file:'$output_file'"); } curl_setopt($this->_ch, CURLOPT_FILE, $fp); } else { curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE); } if ($this->_post) { curl_setopt($this->_ch, CURLOPT_POST, TRUE); curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $this->_post); } $ret = curl_exec($this->_ch); if ($output_file) { fclose($fp); } if ($ret === FALSE) { throw new Exception("Error:'" . curl_error($this->_ch) . "'"); } return $ret; } /** * Destruct of session **/ public function __destruct() { unset($this->_options); unset($this->_post); curl_close($this->_ch); } } ?>