Skip to content

Instantly share code, notes, and snippets.

@cbandy
Created March 10, 2011 20:57
Show Gist options
  • Save cbandy/864916 to your computer and use it in GitHub Desktop.
Save cbandy/864916 to your computer and use it in GitHub Desktop.
cURL wrapper object
<?php
/**
* Wrapper around PHP's procedural API
*/
class Kohana_cURL
{
public static function factory(string $url = NULL)
{
// Raises E_WARNING upon error
$handle = curl_init($url);
return $handle ? new Kohana_cURL($handle) : NULL;
}
protected $_handle;
public function __construct(resource $handle)
{
$this->_handle = $handle;
}
public function __destruct()
{
curl_close($this->_handle);
}
public function errno()
{
return curl_errno($this->_handle);
}
public function error()
{
return curl_error($this->_handle);
}
public function exec()
{
return curl_exec($this->_handle);
}
public function getinfo(int $opt = 0)
{
return curl_getinfo($this->_handle, $opt);
}
public function setopt(int $option, mixed $value)
{
return curl_setopt($this->_handle, $option, $value);
}
public function setopt_array(array $options)
{
return curl_setopt_array($this->_handle, $options);
}
}
@cbandy
Copy link
Author

cbandy commented Mar 10, 2011

Moved resource creation into a factory method so errors during initialization never create an instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment