Skip to content

Instantly share code, notes, and snippets.

@BinaryMuse
Created August 2, 2010 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryMuse/504948 to your computer and use it in GitHub Desktop.
Save BinaryMuse/504948 to your computer and use it in GitHub Desktop.
PHP cURL wrapper

Note: Curl.php was originally written for a CodeIgniter application; however, the code will stand on its own. Furthermore, if you do not wish to use the generator class, simply remove the Curl class definition and rename Curl_Worker to Curl, and use the new Curl class directly.


The Curl class defined in Curl.php and loaded with $CI->load->library("curl") is just a wrapper to return new Curl_Workers using $CI->curl->get(). The Curl_Worker does all the heavy lifting.


$this->load->library("curl");
$curl = $this->curl->get(); // or $curl = new Curl_Worker();
$curl->set_url("http://some.endpoint/action");
// URL can also be set in $curl = $this->curl->get("url") or $curl = new Curl_Worker("url")
$data = $curl->exec();
if($curl->status() != 200) {
    die("Something is wrong!");
}

By default, CURLOPT_RETURNTRANSFER is set to true. This can be overridden by calling set_return(false). Any of the CURLOPT_XXXX options can be set via $curl_worker->set_xxxx, where xxxx is the lowercase version of the last part of the CURLOPT_XXXX constant (so, for example, $curl_worker->set_post(true) is the same as $curl_worker->set_opt(CURLOPT_POST, true)).

After exec() is called, you may call status() on the worker to get the HTTP status code returned during the request. status() will return null if there isn’t a status (e.g., the cURL request hasn’t been executed yet). You may also pass true as an optional parameter to exec() to return the data and status together in an array:


$this->load->library("curl");
$curl = $this->curl->get(); // or $curl = new Curl_Worker();
$curl->set_url("http://some.endpoint/action");
$data = $curl->exec(true);
$real_data = $data['data'];
$http_code = $data['status'];

If necessary, a Curl_Worker can be reused by resetting it via its reset() method; however, if using the library in CodeIgniter classes, it it just as well to get a new one via $this->curl->get().

<?php
/**
* Wrapper class that creates Curl_Workers to do
* the heavy lifting for cURL requests.
*/
class Curl {
/**
* Creates and returns a new Curl_Worker
*/
public function get($url = '') {
return new Curl_Worker($url);
}
}
/**
* Curl_Worker does all the heavy lifting for cURL requests.
*/
class Curl_Worker {
private $handle = null;
private $status = null;
function __construct($url = '') {
$this->init();
if(!empty($url)) {
$this->set_url($url);
}
}
/**
* Builds clean variables for a new run of cURL.
* Used by the constructor and by reset().
*/
private function init() {
if(!is_null($this->handle)) {
@curl_close($this->handle);
}
$this->handle = curl_init();
$this->status = null;
$this->set_return(true); // return data by default
}
/**
* Wrapper for set_opt() that matches the function syntax of
* curl_setopt (without the underscore).
*/
public function setopt($option, $value) {
return $this->set_opt($option, $value);
}
/**
* Set a cURL option via curl_setopt()
*/
public function set_opt($option, $value) {
return curl_setopt($this->handle, $option, $value);
}
/**
* Set a URL for this cURL instance.
* Wraps setopt()
*/
public function set_url($url) {
return $this->set_opt(CURLOPT_URL, $url);
}
/**
* True to return the output of curl_exec() instead of outputting it.
* Defaults to TRUE when using the Curl wrapper class.
*/
public function set_return($value) {
return $this->set_opt(CURLOPT_RETURNTRANSFER, $value);
}
/**
* Magic method to handle calls to undefined methods.
* Allows calling set_XXXX where XXXX is a value
* CURLOPT_XXXX -- e.g., set_post(true) would be the same
* as set_opt(CURLOPT_POST, true).
*
* For a list of options, see http://us.php.net/curl_setopt
*/
public function __call($name, $args) {
// Check to make sure $name starts with "set_"
// Be sure to use === or !== as strpos returns false if not found.
if(strpos($name, "set_") !== 0) {
return;
}
// Also make sure we only got one argument.
if(count($args) !== 0) {
return;
}
$args = $args[0];
// Set the second portion of the string
// and create a CURLOPT out of it.
// set_post
$option = substr($name, 4);
$option = "CURLOPT_" . strtoupper($option);
// Just SET IT, and FORGET IT!
return $this->set_opt($option, $args);
}
/**
* Execute a cURL call via curl_exec(). Returns the data from
* curl_exec if $return_status is false, or returns an array
* with both the data and the status if true.
*
* @param $return_status - true to return the status with the data in
* an array, false to return just the data as a string
* @return either the data from curl_exec() as a string, or an array
* with the keys 'data' and 'status' to return the status as well.
* See the $return_status parameter
*/
public function exec($return_status = false) {
$this->status = null;
$data = curl_exec($this->handle);
$info = curl_getinfo($this->handle);
$this->status = $info['http_code'];
if($return_status == true) {
$data = array(
"data" => $data,
"status" => $this->status
);
}
return $data;
}
/**
* Gets the HTTP status code from the last call to exec().
* Returns null if exec hasn't been called.
*/
public function status() {
return $this->status;
}
/**
* Resets the object so it can be used again, if necessary.
*/
public function reset() {
$this->init();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment