Skip to content

Instantly share code, notes, and snippets.

@dfreerksen
Created August 17, 2012 16:19

Revisions

  1. dfreerksen revised this gist Sep 25, 2012. 2 changed files with 110 additions and 91 deletions.
    196 changes: 107 additions & 89 deletions Curl.php
    Original file line number Diff line number Diff line change
    @@ -7,47 +7,47 @@ class Curl {
    *
    * @var string
    */
    static protected $_method = 'GET';
    protected $_method = 'GET';


    /**
    * cURL URL
    * Currently accepted cURL request method types
    *
    * @var string
    */
    static protected $_url = '';
    protected $_methods = array('POST', 'GET');


    /**
    * cURL data params
    * cURL URL
    *
    * @var string
    */
    static protected $_params = array();
    protected $_url = '';


    /**
    * cURL options
    * cURL data params
    *
    * @var array
    * @var string
    */
    static protected $_options = array();
    protected $_params = array();


    /**
    * cURL user agent
    * cURL options
    *
    * @var array
    */
    static protected $_agent = 'ground(ctrl) engine';
    protected $_options = array();


    /**
    * cURL response
    * cURL user agent
    *
    * @var object
    * @var array
    */
    static protected $_response = null;
    protected $_agent = 'ground(ctrl) engine';


    /**
    @@ -76,32 +76,6 @@ static public function post($url = '', $params = array(), $options = array()) {
    }


    /**
    * PUT request
    *
    * @param string $url
    * @param array $params
    * @param array $options
    * @return mixed
    */
    static public function put($url = '', $params = array(), $options = array()) {
    return self::make($url, $params, $options, 'PUT');
    }


    /**
    * DELETE request
    *
    * @param string $url
    * @param array $params
    * @param array $options
    * @return mixed
    */
    static public function delete($url = '', $params = array(), $options = array()) {
    return self::make($url, $params, $options, 'DELETE');
    }


    /**
    * Make request
    *
    @@ -112,25 +86,58 @@ static public function delete($url = '', $params = array(), $options = array())
    * @return Curl
    */
    static public function make($url = '', $params = array(), $options = array(), $method = null) {
    return new self($url, $params, $options, $method);
    }


    public function __construct($url = '', $params = array(), $options = array(), $method = null) {
    // Set request method
    if ($method) {
    self::$_method = $method;
    $this->_method = $method;
    }

    // Explode the URL to get the URL params
    $url_split = explode('?', $url);

    // Request URL is everything before the ? (if it exists)
    self::$_url = $url_split[0];
    $this->_url = $url_split[0];

    // If there were URL params, add it to the params array
    self::$_params = array_merge(self::$_params, self::_queryString($url), (array) $params);
    $this->_params = array_merge($this->_params, $this->_queryString($url), (array) $params);

    // Set the passed options
    self::_options($options);
    $this->_options($options);
    }


    /**
    * Add multiple params
    *
    * @param array $keys
    * @return Curl
    */
    public function params($keys = array()) {
    $this->_params = array_merge($this->_params, (array) $keys);

    return $this;
    }


    /**
    * Add a single param
    *
    * @param string $key
    * @param string $value
    * @return Curl
    */
    public function param($key = '', $value = '') {
    if ( ! empty($key) && is_string($key)) {
    $key = array(
    $key => (string) $value
    );
    }

    // Return the instance
    return new self(self::$_url, self::$_params, self::$_options, self::$_method);
    return $this->params($key);
    }


    @@ -141,7 +148,7 @@ static public function make($url = '', $params = array(), $options = array(), $m
    * @return Curl
    */
    public function options($options = array()) {
    self::_options($options);
    $this->_options($options);

    return $this;
    }
    @@ -155,7 +162,7 @@ public function options($options = array()) {
    * @return Curl
    */
    public function option($key = '', $value = '') {
    self::_option($key, $value);
    $this->_option($key, $value);

    return $this;
    }
    @@ -168,7 +175,7 @@ public function option($key = '', $value = '') {
    * @return Curl
    */
    public function method($method = null) {
    self::$_method = $method;
    $this->_method = $method;

    return $this;
    }
    @@ -277,20 +284,20 @@ public function ssl($verify_peer = true, $verify_host = 2, $path_to_cert = null)
    * Execute request
    *
    * @return Curl
    * @throws CurlException
    * @throws Exception
    */
    public function call() {
    // cURL is not enabled
    if ( ! $this->_isEnabled()) {
    throw new CurlException(__CLASS__.': PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.');
    throw new Exception(__CLASS__.': PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.');
    }

    // Request method
    $method = (self::$_method) ? strtoupper(self::$_method) : 'GET';
    $method = ($this->_method) ? strtoupper($this->_method) : 'GET';

    // Unrecognized request method?
    if ( ! in_array($method, array('POST', 'GET', 'PUT', 'DELETE'))) {
    throw new CurlException(__CLASS__.': Unrecognized request method of '.self::$_method);
    if ( ! in_array($method, $this->_methods)) {
    throw new Exception(__CLASS__.': Unrecognized request method of '.$this->_method);
    }

    return $this->_execute($method);
    @@ -303,7 +310,7 @@ public function call() {
    * @return Curl
    */
    public function execute() {
    return self::call();
    return $this->call();
    }


    @@ -318,77 +325,79 @@ private function _execute($method) {
    switch ($method) {
    case 'GET' :
    // Append GET params to URL
    self::$_url .= '?'.http_build_query(self::$_params);
    $this->_url .= '?'.http_build_query($this->_params);

    // Set options
    self::options('CURLOPT_HTTPGET', 1);
    $this->options('CURLOPT_HTTPGET', 1);
    break;

    case 'POST' :
    // Set options
    self::options(array(
    $this->options(array(
    'CURLOPT_POST' => true,
    'CURLOPT_POSTFIELDS' => self::$_params
    'CURLOPT_POSTFIELDS' => $this->_params
    ));
    break;

    // Mostly for future use
    case 'PUT' :
    // Set options
    self::options(array(
    $this->options(array(
    'CURLOPT_PUT' => true,
    'CURLOPT_HTTPHEADER' => array('Content-Type: '.strlen(self::$_params)),
    'CURLOPT_POSTFIELDS' => self::$_params
    'CURLOPT_HTTPHEADER' => array('Content-Type: '.strlen($this->_params)),
    'CURLOPT_POSTFIELDS' => $this->_params
    ));
    break;

    // Mostly for future use
    case 'DELETE' :
    // Set options
    self::option('CURLOPT_CUSTOMREQUEST', 'DELETE');
    self::option('CURLOPT_POSTFIELDS', self::$_params);
    $this->option('CURLOPT_CUSTOMREQUEST', 'DELETE');
    $this->option('CURLOPT_POSTFIELDS', $this->_params);
    break;
    }

    // Set timeout option if it isn't already set
    if ( ! array_key_exists('CURLOPT_TIMEOUT', self::$_options)) {
    self::option('CURLOPT_TIMEOUT', 30);
    if ( ! array_key_exists('CURLOPT_TIMEOUT', $this->_options)) {
    $this->option('CURLOPT_TIMEOUT', 30);
    }

    // Set returntransfer option if it isn't already set
    if ( ! array_key_exists('CURLOPT_RETURNTRANSFER', self::$_options)) {
    self::option('CURLOPT_RETURNTRANSFER', true);
    if ( ! array_key_exists('CURLOPT_RETURNTRANSFER', $this->_options)) {
    $this->option('CURLOPT_RETURNTRANSFER', true);
    }

    // Set failonerror option if it isn't already set
    if ( ! array_key_exists('CURLOPT_FAILONERROR', self::$_options)) {
    self::option('CURLOPT_FAILONERROR', true);
    if ( ! array_key_exists('CURLOPT_FAILONERROR', $this->_options)) {
    $this->option('CURLOPT_FAILONERROR', true);
    }

    // Set user agent option if it isn't already set
    if ( ! array_key_exists('CURLOPT_USERAGENT', self::$_options)) {
    self::option('CURLOPT_USERAGENT', self::$_agent);
    if ( ! array_key_exists('CURLOPT_USERAGENT', $this->_options)) {
    $this->option('CURLOPT_USERAGENT', $this->_agent);
    }

    // Only set follow location if not running securely
    if ( ! ini_get('safe_mode') && ! ini_get('open_basedir')) {
    // Ok, follow location is not set already so lets set it to true
    if ( ! array_key_exists('CURLOPT_FOLLOWLOCATION', self::$_options)) {
    self::option('CURLOPT_FOLLOWLOCATION', true);
    if ( ! array_key_exists('CURLOPT_FOLLOWLOCATION', $this->_options)) {
    $this->option('CURLOPT_FOLLOWLOCATION', true);
    }
    }

    // Initialize cURL request
    $ch = curl_init(self::$_url);
    $ch = curl_init($this->_url);

    // Can't set the options in batch
    if ( ! function_exists('curl_setopt_array')) {
    foreach (self::$_options as $key => $value) {
    foreach ($this->_options as $key => $value) {
    curl_setopt($ch, $key, $value);
    }
    }

    // Set options in batch
    else {
    curl_setopt_array($ch, self::$_options);
    curl_setopt_array($ch, $this->_options);
    }

    // Execute
    @@ -399,7 +408,10 @@ private function _execute($method) {
    $response->error_code = curl_errno($ch);

    // Reset the options
    self::$_options = array();

    $this->_url = '';
    $this->_params = '';
    $this->_options = array();

    // Close cURL request
    curl_close($ch);
    @@ -413,9 +425,9 @@ private function _execute($method) {
    *
    * @param array $options
    */
    static private function _options($options = array()) {
    foreach ((array)$options as $key => $value) {
    self::_option($key, $value);
    protected function _options($options = array()) {
    foreach ((array) $options as $key => $value) {
    $this->_option($key, $value);
    }
    }

    @@ -425,20 +437,29 @@ static private function _options($options = array()) {
    *
    * @param string $key
    * @param string $value
    * @throws Exception
    */
    static private function _option($key = '', $value = '') {
    protected function _option($key = '', $value = '') {
    if (is_string($key) && ! is_numeric($key)) {
    $key = constant(strtoupper($key));
    $const = strtoupper($key);

    if (defined($const)) {
    $key = constant(strtoupper($key));
    }

    else {
    throw new Exception('Curl: Constant ['.$const.'] not defined.');
    }
    }

    // Custom header
    if ($key == CURLOPT_HTTPHEADER) {
    self::$_options[$key][] = $value;
    $this->_options[$key][] = $value;
    }

    // Not a custom header
    else {
    self::$_options[$key] = $value;
    $this->_options[$key] = $value;
    }
    }

    @@ -449,7 +470,7 @@ static private function _option($key = '', $value = '') {
    * @param $uri
    * @return array
    */
    static private function _queryString($uri) {
    protected function _queryString($uri) {
    $query_data = array();

    $query_array = html_entity_decode(parse_url($uri, PHP_URL_QUERY));
    @@ -473,11 +494,8 @@ static private function _queryString($uri) {
    *
    * @return bool
    */
    private function _isEnabled() {
    protected function _isEnabled() {
    return function_exists('curl_init');
    }

    }


    class CurlException extends Exception { }
    5 changes: 3 additions & 2 deletions curl_examples.php
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,11 @@
    'include_entities' => false,
    'include_rts' => false,
    'screen_name' => 'dfreerksen',
    'count' => 2
    //'count' => 2
    );

    $twitter = Curl::get('https://api.twitter.com/1/statuses/user_timeline.json', $params)
    ->param('count', 2)
    ->call();

    //$twitter = Curl::get('https://api.twitter.com/1/statuses/user_timeline.json?include_entities=false&include_rts=false&screen_name=dfreerksen&count=2')
    @@ -16,4 +17,4 @@
    var_dump( $twitter->result );
    var_dump( $twitter->info );
    var_dump( $twitter->error );
    var_dump( $twitter->error_code );
    var_dump( $twitter->error_code );
  2. dfreerksen revised this gist Aug 24, 2012. 1 changed file with 41 additions and 41 deletions.
    82 changes: 41 additions & 41 deletions Curl.php
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,7 @@ class Curl {
    * @return mixed
    */
    static public function get($url = '', $params = array(), $options = array()) {
    return static::make($url, $params, $options, 'GET');
    return self::make($url, $params, $options, 'GET');
    }


    @@ -72,7 +72,7 @@ static public function get($url = '', $params = array(), $options = array()) {
    * @return mixed
    */
    static public function post($url = '', $params = array(), $options = array()) {
    return static::make($url, $params, $options, 'POST');
    return self::make($url, $params, $options, 'POST');
    }


    @@ -85,7 +85,7 @@ static public function post($url = '', $params = array(), $options = array()) {
    * @return mixed
    */
    static public function put($url = '', $params = array(), $options = array()) {
    return static::make($url, $params, $options, 'PUT');
    return self::make($url, $params, $options, 'PUT');
    }


    @@ -98,7 +98,7 @@ static public function put($url = '', $params = array(), $options = array()) {
    * @return mixed
    */
    static public function delete($url = '', $params = array(), $options = array()) {
    return static::make($url, $params, $options, 'DELETE');
    return self::make($url, $params, $options, 'DELETE');
    }


    @@ -114,23 +114,23 @@ static public function delete($url = '', $params = array(), $options = array())
    static public function make($url = '', $params = array(), $options = array(), $method = null) {
    // Set request method
    if ($method) {
    static::$_method = $method;
    self::$_method = $method;
    }

    // Explode the URL to get the URL params
    $url_split = explode('?', $url);

    // Request URL is everything before the ? (if it exists)
    static::$_url = $url_split[0];
    self::$_url = $url_split[0];

    // If there were URL params, add it to the params array
    static::$_params = array_merge(static::$_params, static::_queryString($url), (array) $params);
    self::$_params = array_merge(self::$_params, self::_queryString($url), (array) $params);

    // Set the passed options
    static::_options($options);
    self::_options($options);

    // Return the instance
    return new static(static::$_url, static::$_params, static::$_options, static::$_method);
    return new self(self::$_url, self::$_params, self::$_options, self::$_method);
    }


    @@ -141,7 +141,7 @@ static public function make($url = '', $params = array(), $options = array(), $m
    * @return Curl
    */
    public function options($options = array()) {
    static::_options($options);
    self::_options($options);

    return $this;
    }
    @@ -155,7 +155,7 @@ public function options($options = array()) {
    * @return Curl
    */
    public function option($key = '', $value = '') {
    static::_option($key, $value);
    self::_option($key, $value);

    return $this;
    }
    @@ -168,7 +168,7 @@ public function option($key = '', $value = '') {
    * @return Curl
    */
    public function method($method = null) {
    static::$_method = $method;
    self::$_method = $method;

    return $this;
    }
    @@ -286,11 +286,11 @@ public function call() {
    }

    // Request method
    $method = (static::$_method) ? strtoupper(static::$_method) : 'GET';
    $method = (self::$_method) ? strtoupper(self::$_method) : 'GET';

    // Unrecognized request method?
    if ( ! in_array($method, array('POST', 'GET', 'PUT', 'DELETE'))) {
    throw new CurlException(__CLASS__.': Unrecognized request method of '.static::$_method);
    throw new CurlException(__CLASS__.': Unrecognized request method of '.self::$_method);
    }

    return $this->_execute($method);
    @@ -303,7 +303,7 @@ public function call() {
    * @return Curl
    */
    public function execute() {
    return static::call();
    return self::call();
    }


    @@ -318,77 +318,77 @@ private function _execute($method) {
    switch ($method) {
    case 'GET' :
    // Append GET params to URL
    static::$_url .= '?'.http_build_query(static::$_params);
    self::$_url .= '?'.http_build_query(self::$_params);

    // Set options
    static::options('CURLOPT_HTTPGET', 1);
    self::options('CURLOPT_HTTPGET', 1);
    break;

    case 'POST' :
    // Set options
    static::options(array(
    self::options(array(
    'CURLOPT_POST' => true,
    'CURLOPT_POSTFIELDS' => static::$_params
    'CURLOPT_POSTFIELDS' => self::$_params
    ));
    break;

    case 'PUT' :
    // Set options
    static::options(array(
    self::options(array(
    'CURLOPT_PUT' => true,
    'CURLOPT_HTTPHEADER' => array('Content-Type: '.strlen(static::$_params)),
    'CURLOPT_POSTFIELDS' => static::$_params
    'CURLOPT_HTTPHEADER' => array('Content-Type: '.strlen(self::$_params)),
    'CURLOPT_POSTFIELDS' => self::$_params
    ));
    break;

    case 'DELETE' :
    // Set options
    static::option('CURLOPT_CUSTOMREQUEST', 'DELETE');
    static::option('CURLOPT_POSTFIELDS', static::$_params);
    self::option('CURLOPT_CUSTOMREQUEST', 'DELETE');
    self::option('CURLOPT_POSTFIELDS', self::$_params);
    break;
    }

    // Set timeout option if it isn't already set
    if ( ! array_key_exists('CURLOPT_TIMEOUT', static::$_options)) {
    static::option('CURLOPT_TIMEOUT', 30);
    if ( ! array_key_exists('CURLOPT_TIMEOUT', self::$_options)) {
    self::option('CURLOPT_TIMEOUT', 30);
    }

    // Set returntransfer option if it isn't already set
    if ( ! array_key_exists('CURLOPT_RETURNTRANSFER', static::$_options)) {
    static::option('CURLOPT_RETURNTRANSFER', true);
    if ( ! array_key_exists('CURLOPT_RETURNTRANSFER', self::$_options)) {
    self::option('CURLOPT_RETURNTRANSFER', true);
    }

    // Set failonerror option if it isn't already set
    if ( ! array_key_exists('CURLOPT_FAILONERROR', static::$_options)) {
    static::option('CURLOPT_FAILONERROR', true);
    if ( ! array_key_exists('CURLOPT_FAILONERROR', self::$_options)) {
    self::option('CURLOPT_FAILONERROR', true);
    }

    // Set user agent option if it isn't already set
    if ( ! array_key_exists('CURLOPT_USERAGENT', static::$_options)) {
    static::option('CURLOPT_USERAGENT', static::$_agent);
    if ( ! array_key_exists('CURLOPT_USERAGENT', self::$_options)) {
    self::option('CURLOPT_USERAGENT', self::$_agent);
    }

    // Only set follow location if not running securely
    if ( ! ini_get('safe_mode') && ! ini_get('open_basedir')) {
    // Ok, follow location is not set already so lets set it to true
    if ( ! array_key_exists('CURLOPT_FOLLOWLOCATION', static::$_options)) {
    static::option('CURLOPT_FOLLOWLOCATION', true);
    if ( ! array_key_exists('CURLOPT_FOLLOWLOCATION', self::$_options)) {
    self::option('CURLOPT_FOLLOWLOCATION', true);
    }
    }

    // Initialize cURL request
    $ch = curl_init(static::$_url);
    $ch = curl_init(self::$_url);

    // Can't set the options in batch
    if ( ! function_exists('curl_setopt_array')) {
    foreach (static::$_options as $key => $value) {
    foreach (self::$_options as $key => $value) {
    curl_setopt($ch, $key, $value);
    }
    }

    // Set options in batch
    else {
    curl_setopt_array($ch, static::$_options);
    curl_setopt_array($ch, self::$_options);
    }

    // Execute
    @@ -399,7 +399,7 @@ private function _execute($method) {
    $response->error_code = curl_errno($ch);

    // Reset the options
    static::$_options = array();
    self::$_options = array();

    // Close cURL request
    curl_close($ch);
    @@ -415,7 +415,7 @@ private function _execute($method) {
    */
    static private function _options($options = array()) {
    foreach ((array)$options as $key => $value) {
    static::_option($key, $value);
    self::_option($key, $value);
    }
    }

    @@ -433,12 +433,12 @@ static private function _option($key = '', $value = '') {

    // Custom header
    if ($key == CURLOPT_HTTPHEADER) {
    static::$_options[$key][] = $value;
    self::$_options[$key][] = $value;
    }

    // Not a custom header
    else {
    static::$_options[$key] = $value;
    self::$_options[$key] = $value;
    }
    }

  3. dfreerksen created this gist Aug 17, 2012.
    483 changes: 483 additions & 0 deletions Curl.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,483 @@
    <?php

    class Curl {

    /**
    * cURL request method
    *
    * @var string
    */
    static protected $_method = 'GET';


    /**
    * cURL URL
    *
    * @var string
    */
    static protected $_url = '';


    /**
    * cURL data params
    *
    * @var string
    */
    static protected $_params = array();


    /**
    * cURL options
    *
    * @var array
    */
    static protected $_options = array();


    /**
    * cURL user agent
    *
    * @var array
    */
    static protected $_agent = 'ground(ctrl) engine';


    /**
    * cURL response
    *
    * @var object
    */
    static protected $_response = null;


    /**
    * GET request
    *
    * @param string $url
    * @param array $params
    * @param array $options
    * @return mixed
    */
    static public function get($url = '', $params = array(), $options = array()) {
    return static::make($url, $params, $options, 'GET');
    }


    /**
    * POST request
    *
    * @param string $url
    * @param array $params
    * @param array $options
    * @return mixed
    */
    static public function post($url = '', $params = array(), $options = array()) {
    return static::make($url, $params, $options, 'POST');
    }


    /**
    * PUT request
    *
    * @param string $url
    * @param array $params
    * @param array $options
    * @return mixed
    */
    static public function put($url = '', $params = array(), $options = array()) {
    return static::make($url, $params, $options, 'PUT');
    }


    /**
    * DELETE request
    *
    * @param string $url
    * @param array $params
    * @param array $options
    * @return mixed
    */
    static public function delete($url = '', $params = array(), $options = array()) {
    return static::make($url, $params, $options, 'DELETE');
    }


    /**
    * Make request
    *
    * @param string $url
    * @param array $params
    * @param array $options
    * @param string $method
    * @return Curl
    */
    static public function make($url = '', $params = array(), $options = array(), $method = null) {
    // Set request method
    if ($method) {
    static::$_method = $method;
    }

    // Explode the URL to get the URL params
    $url_split = explode('?', $url);

    // Request URL is everything before the ? (if it exists)
    static::$_url = $url_split[0];

    // If there were URL params, add it to the params array
    static::$_params = array_merge(static::$_params, static::_queryString($url), (array) $params);

    // Set the passed options
    static::_options($options);

    // Return the instance
    return new static(static::$_url, static::$_params, static::$_options, static::$_method);
    }


    /**
    * Add multiple options
    *
    * @param array $options
    * @return Curl
    */
    public function options($options = array()) {
    static::_options($options);

    return $this;
    }


    /**
    * Add single option
    *
    * @param string $key
    * @param string $value
    * @return Curl
    */
    public function option($key = '', $value = '') {
    static::_option($key, $value);

    return $this;
    }


    /**
    * Request method
    *
    * @param string $method
    * @return Curl
    */
    public function method($method = null) {
    static::$_method = $method;

    return $this;
    }


    /**
    * User agent
    *
    * @param string $agent
    * @return Curl
    */
    public function agent($agent = '') {
    if ($agent) {
    return $this->option('CURLOPT_USERAGENT', $agent);
    }

    return $this;
    }


    /**
    * Proxy helper
    *
    * @param string $url
    * @param int $port
    * @return Curl
    */
    public function proxy($url = '', $port = 80) {
    return $this->options(array(
    'CURLOPT_HTTPPROXYTUNNEL' => true,
    'CURLOPT_PROXY' => $url.':'.$port
    ));
    }


    /**
    * Custom header helper
    *
    * @param string $header
    * @param string $content
    * @return Curl
    */
    public function httpHeader($header, $content = null) {
    $header = ($content) ? $header.':'.$content : $header;

    return $this->option('CURLOPT_HTTPHEADER', $header);
    }


    /**
    * HTTP login helper
    *
    * @param string $username
    * @param string $password
    * @param string $type
    * @return Curl
    */
    public function httpLogin($username = '', $password = '', $type = 'ANY') {
    return $this->options(array(
    'CURLOPT_HTTPAUTH' => constant('CURLAUTH_'.strtoupper($type)),
    'CURLOPT_USERPWD' => $username.':'.$password
    ));
    }


    /**
    * Proxy login helper
    *
    * @param string $username
    * @param string $password
    * @return Curl
    */
    public function proxyLogin($username = '', $password = '') {
    return $this->option('CURLOPT_PROXYUSERPWD', $username.':'.$password);
    }


    /**
    * SSL helper
    *
    * @param bool $verify_peer
    * @param int $verify_host
    * @param string $path_to_cert
    * @return Curl
    */
    public function ssl($verify_peer = true, $verify_host = 2, $path_to_cert = null) {
    if ($verify_peer) {
    $options = array(
    'CURLOPT_SSL_VERIFYPEER' => true,
    'CURLOPT_SSL_VERIFYHOST' => $verify_host,
    'CURLOPT_CAINFO' => $path_to_cert
    );
    }

    else {
    $options = array(
    'CURLOPT_SSL_VERIFYPEER' => false
    );
    }

    return $this->options($options);
    }


    /**
    * Execute request
    *
    * @return Curl
    * @throws CurlException
    */
    public function call() {
    // cURL is not enabled
    if ( ! $this->_isEnabled()) {
    throw new CurlException(__CLASS__.': PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.');
    }

    // Request method
    $method = (static::$_method) ? strtoupper(static::$_method) : 'GET';

    // Unrecognized request method?
    if ( ! in_array($method, array('POST', 'GET', 'PUT', 'DELETE'))) {
    throw new CurlException(__CLASS__.': Unrecognized request method of '.static::$_method);
    }

    return $this->_execute($method);
    }


    /**
    * Alias for call();
    *
    * @return Curl
    */
    public function execute() {
    return static::call();
    }


    /**
    * Execute request
    *
    * @param string $method
    * @return Curl
    */
    private function _execute($method) {
    // Method specific options
    switch ($method) {
    case 'GET' :
    // Append GET params to URL
    static::$_url .= '?'.http_build_query(static::$_params);

    // Set options
    static::options('CURLOPT_HTTPGET', 1);
    break;

    case 'POST' :
    // Set options
    static::options(array(
    'CURLOPT_POST' => true,
    'CURLOPT_POSTFIELDS' => static::$_params
    ));
    break;

    case 'PUT' :
    // Set options
    static::options(array(
    'CURLOPT_PUT' => true,
    'CURLOPT_HTTPHEADER' => array('Content-Type: '.strlen(static::$_params)),
    'CURLOPT_POSTFIELDS' => static::$_params
    ));
    break;

    case 'DELETE' :
    // Set options
    static::option('CURLOPT_CUSTOMREQUEST', 'DELETE');
    static::option('CURLOPT_POSTFIELDS', static::$_params);
    break;
    }

    // Set timeout option if it isn't already set
    if ( ! array_key_exists('CURLOPT_TIMEOUT', static::$_options)) {
    static::option('CURLOPT_TIMEOUT', 30);
    }

    // Set returntransfer option if it isn't already set
    if ( ! array_key_exists('CURLOPT_RETURNTRANSFER', static::$_options)) {
    static::option('CURLOPT_RETURNTRANSFER', true);
    }

    // Set failonerror option if it isn't already set
    if ( ! array_key_exists('CURLOPT_FAILONERROR', static::$_options)) {
    static::option('CURLOPT_FAILONERROR', true);
    }

    // Set user agent option if it isn't already set
    if ( ! array_key_exists('CURLOPT_USERAGENT', static::$_options)) {
    static::option('CURLOPT_USERAGENT', static::$_agent);
    }

    // Only set follow location if not running securely
    if ( ! ini_get('safe_mode') && ! ini_get('open_basedir')) {
    // Ok, follow location is not set already so lets set it to true
    if ( ! array_key_exists('CURLOPT_FOLLOWLOCATION', static::$_options)) {
    static::option('CURLOPT_FOLLOWLOCATION', true);
    }
    }

    // Initialize cURL request
    $ch = curl_init(static::$_url);

    // Can't set the options in batch
    if ( ! function_exists('curl_setopt_array')) {
    foreach (static::$_options as $key => $value) {
    curl_setopt($ch, $key, $value);
    }
    }

    // Set options in batch
    else {
    curl_setopt_array($ch, static::$_options);
    }

    // Execute
    $response = new stdClass();
    $response->result = curl_exec($ch);
    $response->info = curl_getinfo($ch);
    $response->error = curl_error($ch);
    $response->error_code = curl_errno($ch);

    // Reset the options
    static::$_options = array();

    // Close cURL request
    curl_close($ch);

    return $response;
    }


    /**
    * Add multiple options
    *
    * @param array $options
    */
    static private function _options($options = array()) {
    foreach ((array)$options as $key => $value) {
    static::_option($key, $value);
    }
    }


    /**
    * Add single option
    *
    * @param string $key
    * @param string $value
    */
    static private function _option($key = '', $value = '') {
    if (is_string($key) && ! is_numeric($key)) {
    $key = constant(strtoupper($key));
    }

    // Custom header
    if ($key == CURLOPT_HTTPHEADER) {
    static::$_options[$key][] = $value;
    }

    // Not a custom header
    else {
    static::$_options[$key] = $value;
    }
    }


    /**
    * Get query string from URL
    *
    * @param $uri
    * @return array
    */
    static private function _queryString($uri) {
    $query_data = array();

    $query_array = html_entity_decode(parse_url($uri, PHP_URL_QUERY));

    if ( ! empty($query_array)) {
    $query_array = explode('&', $query_array);

    foreach($query_array as $val) {
    $x = explode('=', $val);

    $query_data[$x[0]] = $x[1];
    }
    }

    return $query_data;
    }


    /**
    * Check if cURL is enabled
    *
    * @return bool
    */
    private function _isEnabled() {
    return function_exists('curl_init');
    }

    }


    class CurlException extends Exception { }
    19 changes: 19 additions & 0 deletions curl_examples.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    require_once('Curl.php');

    $params = array(
    'include_entities' => false,
    'include_rts' => false,
    'screen_name' => 'dfreerksen',
    'count' => 2
    );

    $twitter = Curl::get('https://api.twitter.com/1/statuses/user_timeline.json', $params)
    ->call();

    //$twitter = Curl::get('https://api.twitter.com/1/statuses/user_timeline.json?include_entities=false&include_rts=false&screen_name=dfreerksen&count=2')
    //->call();

    var_dump( $twitter->result );
    var_dump( $twitter->info );
    var_dump( $twitter->error );
    var_dump( $twitter->error_code );