Created
August 17, 2012 16:19
Revisions
-
dfreerksen revised this gist
Sep 25, 2012 . 2 changed files with 110 additions and 91 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,47 +7,47 @@ class Curl { * * @var string */ protected $_method = 'GET'; /** * Currently accepted cURL request method types * * @var string */ protected $_methods = array('POST', 'GET'); /** * cURL URL * * @var string */ protected $_url = ''; /** * cURL data params * * @var string */ protected $_params = array(); /** * cURL options * * @var array */ protected $_options = array(); /** * cURL user agent * * @var array */ protected $_agent = 'ground(ctrl) engine'; /** @@ -76,32 +76,6 @@ static public function post($url = '', $params = array(), $options = array()) { } /** * 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) { $this->_method = $method; } // Explode the URL to get the URL params $url_split = explode('?', $url); // Request URL is everything before the ? (if it exists) $this->_url = $url_split[0]; // If there were URL params, add it to the params array $this->_params = array_merge($this->_params, $this->_queryString($url), (array) $params); // Set the passed 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 $this->params($key); } @@ -141,7 +148,7 @@ static public function make($url = '', $params = array(), $options = array(), $m * @return Curl */ public function options($options = array()) { $this->_options($options); return $this; } @@ -155,7 +162,7 @@ public function options($options = array()) { * @return Curl */ public function 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) { $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 Exception */ public function call() { // cURL is not enabled if ( ! $this->_isEnabled()) { throw new Exception(__CLASS__.': PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.'); } // Request method $method = ($this->_method) ? strtoupper($this->_method) : 'GET'; // Unrecognized request 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 $this->call(); } @@ -318,77 +325,79 @@ private function _execute($method) { switch ($method) { case 'GET' : // Append GET params to URL $this->_url .= '?'.http_build_query($this->_params); // Set options $this->options('CURLOPT_HTTPGET', 1); break; case 'POST' : // Set options $this->options(array( 'CURLOPT_POST' => true, 'CURLOPT_POSTFIELDS' => $this->_params )); break; // Mostly for future use case 'PUT' : // Set options $this->options(array( 'CURLOPT_PUT' => true, 'CURLOPT_HTTPHEADER' => array('Content-Type: '.strlen($this->_params)), 'CURLOPT_POSTFIELDS' => $this->_params )); break; // Mostly for future use case 'DELETE' : // Set options $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', $this->_options)) { $this->option('CURLOPT_TIMEOUT', 30); } // Set returntransfer option if it isn't already set 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', $this->_options)) { $this->option('CURLOPT_FAILONERROR', true); } // Set user agent option if it isn't already set 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', $this->_options)) { $this->option('CURLOPT_FOLLOWLOCATION', true); } } // Initialize cURL request $ch = curl_init($this->_url); // Can't set the options in batch if ( ! function_exists('curl_setopt_array')) { foreach ($this->_options as $key => $value) { curl_setopt($ch, $key, $value); } } // Set options in batch else { curl_setopt_array($ch, $this->_options); } // Execute @@ -399,7 +408,10 @@ private function _execute($method) { $response->error_code = curl_errno($ch); // Reset the options $this->_url = ''; $this->_params = ''; $this->_options = array(); // Close cURL request curl_close($ch); @@ -413,9 +425,9 @@ private function _execute($method) { * * @param array $options */ 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 */ protected function _option($key = '', $value = '') { if (is_string($key) && ! is_numeric($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) { $this->_options[$key][] = $value; } // Not a custom header else { $this->_options[$key] = $value; } } @@ -449,7 +470,7 @@ static private function _option($key = '', $value = '') { * @param $uri * @return array */ 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 */ protected function _isEnabled() { return function_exists('curl_init'); } } This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,10 +4,11 @@ 'include_entities' => false, 'include_rts' => false, 'screen_name' => 'dfreerksen', //'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 ); -
dfreerksen revised this gist
Aug 24, 2012 . 1 changed file with 41 additions and 41 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 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 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 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) { self::$_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]; // If there were URL params, add it to the params array self::$_params = array_merge(self::$_params, self::_queryString($url), (array) $params); // Set the passed options self::_options($options); // Return the instance 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()) { self::_options($options); return $this; } @@ -155,7 +155,7 @@ public function options($options = array()) { * @return Curl */ public function 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) { self::$_method = $method; return $this; } @@ -286,11 +286,11 @@ public function call() { } // Request method $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 '.self::$_method); } return $this->_execute($method); @@ -303,7 +303,7 @@ public function call() { * @return Curl */ public function execute() { return self::call(); } @@ -318,77 +318,77 @@ private function _execute($method) { switch ($method) { case 'GET' : // Append GET params to URL self::$_url .= '?'.http_build_query(self::$_params); // Set options self::options('CURLOPT_HTTPGET', 1); break; case 'POST' : // Set options self::options(array( 'CURLOPT_POST' => true, 'CURLOPT_POSTFIELDS' => self::$_params )); break; case 'PUT' : // Set options self::options(array( 'CURLOPT_PUT' => true, 'CURLOPT_HTTPHEADER' => array('Content-Type: '.strlen(self::$_params)), 'CURLOPT_POSTFIELDS' => self::$_params )); break; case 'DELETE' : // Set options 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', self::$_options)) { self::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); } // Set failonerror option if it isn't already set 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', 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', self::$_options)) { self::option('CURLOPT_FOLLOWLOCATION', true); } } // Initialize cURL request $ch = curl_init(self::$_url); // Can't set the options in batch if ( ! function_exists('curl_setopt_array')) { foreach (self::$_options as $key => $value) { curl_setopt($ch, $key, $value); } } // Set options in batch else { curl_setopt_array($ch, self::$_options); } // Execute @@ -399,7 +399,7 @@ private function _execute($method) { $response->error_code = curl_errno($ch); // Reset the options 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) { self::_option($key, $value); } } @@ -433,12 +433,12 @@ static private function _option($key = '', $value = '') { // Custom header if ($key == CURLOPT_HTTPHEADER) { self::$_options[$key][] = $value; } // Not a custom header else { self::$_options[$key] = $value; } } -
dfreerksen created this gist
Aug 17, 2012 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 { } This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 );