Browse files

Restructure and add documentation

  • Loading branch information...
1 parent 6a53322 commit 0f3d840505a74eb083dcdae8329f00c9b710a735 @shuber committed Sep 4, 2009
Showing with 189 additions and 414 deletions.
  1. +0 −1 .gitignore
  2. +0 −73 CHANGELOG
  3. +2 −143 curl.php
  4. +0 −197 curl.php4
  5. +110 −0 lib/curl.php
  6. +77 −0 lib/curl_response.php
View
1 .gitignore
@@ -1,2 +1 @@
-.DS_Store
curl_cookie.txt
View
73 CHANGELOG
@@ -1,73 +0,0 @@
-2009-04-29 - Sean Huber (shuber@huberry.com)
- * POSTFIELDS are not set unless specified
-
-2009-03-30 - Sean Huber (shuber@huberry.com)
- * Set the POSTFIELDS after setting the request method
-
-2008-12-19 - Sean Huber (shuber@huberry.com)
- * Use dirname(__FILE__) instead of realpath('.')
-
-2008-06-12 - Sean Huber (shuber@huberry.com)
- * $curl->cookie_file defaults to curl_cookie.txt using an absolute path
-
-2008-06-05 - Sean Huber (shuber@huberry.com)
- * Post variables can be passed to the request() method as a query string or array
- * Specified the argument separator for the http_build_query() function
-
-2008-05-16 - Simone Carletti (weppos@weppos.net)
- * FIXED: $this->user_agent raises a notice when HTTP_USER_AGENT server variable is empty.
-
-2008-05-16 - Simone Carletti (weppos@weppos.net)
- * FIXED: PHP 4 compatibility (__construct and http_build_query methods)
- * CHANGED: Various code touch up to be compatibile with Zend Framework coding standard (just because actually it's the best collection of PHP coding standard available)
-
-2008-05-13 - Sean Huber (shuber@huberry.com)
- * Added protected $handle property to use during requests
- * Added php4 version of the curl class
-
-2008-05-13 - Sean Huber (shuber@huberry.com)
- * Fixed bug: post variables were not being sent correctly
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Replaced '1' with 'true' in default curl_setopt options
- * Updated README
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Updated README
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Updated README
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Updated README
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Updated README
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Fixed Bug: Custom headers were not being sent correctly for requests
- * Updated README
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Converted README to hard tabs
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Updated README
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Updated README
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Converted curl.php to hard tabs
- * Updated README
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Removed test.php
- * Ignored curl_cookie.txt
-
-2008-05-07 - Sean Huber (shuber@huberry.com)
- * Added CHANGELOG
- * Added test.php
- * Added curl error numbers to error strings
- * Curl options prefixed with CURLOPT_ will still work
- * Fixed Bug: Renamed CurlResponse#toString() to CurlResponse#__toString()
View
145 curl.php
@@ -1,145 +1,4 @@
<?php
-# Curl, CurlResponse
-#
-# Author Sean Huber - shuber@huberry.com
-# Date May 2008
-#
-# A basic CURL wrapper for PHP
-#
-# See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP
-
-class Curl
-{
- public $cookie_file;
- public $headers = array();
- public $options = array();
- public $referer = '';
- public $user_agent = '';
-
- protected $error = '';
- protected $handle;
-
-
- public function __construct()
- {
- $this->cookie_file = dirname(__FILE__).'/curl_cookie.txt';
- $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ?
- $_SERVER['HTTP_USER_AGENT'] :
- 'Curl/PHP ' . PHP_VERSION . ' (http://github.com/shuber/curl/)';
- }
-
- public function delete($url, $vars = array())
- {
- return $this->request('DELETE', $url, $vars);
- }
-
- public function error()
- {
- return $this->error;
- }
-
- public function get($url, $vars = array())
- {
- if (!empty($vars)) {
- $url .= (stripos($url, '?') !== false) ? '&' : '?';
- $url .= http_build_query($vars, '', '&');
- }
- return $this->request('GET', $url);
- }
-
- public function post($url, $vars = array())
- {
- return $this->request('POST', $url, $vars);
- }
-
- public function put($url, $vars = array())
- {
- return $this->request('PUT', $url, $vars);
- }
-
- protected function request($method, $url, $vars = array())
- {
- $this->handle = curl_init();
-
- # Determine the request method and set the correct CURL option
- switch ($method) {
- case 'GET':
- curl_setopt($this->handle, CURLOPT_HTTPGET, true);
- break;
- case 'POST':
- curl_setopt($this->handle, CURLOPT_POST, true);
- break;
- default:
- curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
- }
-
- # Set some default CURL options
- curl_setopt($this->handle, CURLOPT_COOKIEFILE, $this->cookie_file);
- curl_setopt($this->handle, CURLOPT_COOKIEJAR, $this->cookie_file);
- curl_setopt($this->handle, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($this->handle, CURLOPT_HEADER, true);
- if (!empty($vars)) curl_setopt($this->handle, CURLOPT_POSTFIELDS, (is_array($vars) ? http_build_query($vars, '', '&') : $vars));
- curl_setopt($this->handle, CURLOPT_REFERER, $this->referer);
- curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->handle, CURLOPT_URL, $url);
- curl_setopt($this->handle, CURLOPT_USERAGENT, $this->user_agent);
-
- # Format custom headers for this request and set CURL option
- $headers = array();
- foreach ($this->headers as $key => $value) {
- $headers[] = $key.': '.$value;
- }
- curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
-
- # Set any custom CURL options
- foreach ($this->options as $option => $value) {
- curl_setopt($this->handle, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
- }
-
- $response = curl_exec($this->handle);
- if ($response) {
- $response = new CurlResponse($response);
- } else {
- $this->error = curl_errno($this->handle).' - '.curl_error($this->handle);
- }
- curl_close($this->handle);
- return $response;
- }
-
-}
-
-class CurlResponse
-{
- public $body = '';
- public $headers = array();
-
- public function __construct($response)
- {
- # Extract headers from response
- $pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
- preg_match_all($pattern, $response, $matches);
- $headers = split("\r\n", str_replace("\r\n\r\n", '', array_pop($matches[0])));
-
- # Extract the version and status from the first header
- $version_and_status = array_shift($headers);
- preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
- $this->headers['Http-Version'] = $matches[1];
- $this->headers['Status-Code'] = $matches[2];
- $this->headers['Status'] = $matches[2].' '.$matches[3];
-
- # Convert headers into an associative array
- foreach ($headers as $header) {
- preg_match('#(.*?)\:\s(.*)#', $header, $matches);
- $this->headers[$matches[1]] = $matches[2];
- }
-
- # Remove the headers from the response body
- $this->body = preg_replace($pattern, '', $response);
- }
-
- public function __toString()
- {
- return $this->body;
- }
-}
+require_once 'lib'.DIRECTORY_SEPARATOR.'curl.php';
+require_once 'lib'.DIRECTORY_SEPARATOR.'curl_response.php';
View
197 curl.php4
@@ -1,197 +0,0 @@
-<?php
-
-# Curl, CurlResponse
-#
-# Author Sean Huber - shuber@huberry.com
-# Date May 2008
-#
-# A basic CURL wrapper for PHP
-#
-# See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP
-
-class Curl
-{
- var $cookie_file;
- var $headers = array();
- var $options = array();
- var $referer = '';
- var $user_agent = '';
-
- # Protected
- var $error = '';
- var $handle;
-
-
- function Curl()
- {
- $this->__construct();
- }
-
- function __construct()
- {
- $this->cookie_file = dirname(__FILE__).'/curl_cookie.txt';
- $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ?
- $_SERVER['HTTP_USER_AGENT'] :
- 'Curl/PHP ' . PHP_VERSION . ' (http://github.com/shuber/curl/)';
- }
-
- function delete($url, $vars = array())
- {
- return $this->request('DELETE', $url, $vars);
- }
-
- function error()
- {
- return $this->error;
- }
-
- function get($url, $vars = array())
- {
- if (!empty($vars)) {
- $url .= (stripos($url, '?') !== false) ? '&' : '?';
- $url .= http_build_query($vars, '', '&');
- }
- return $this->request('GET', $url);
- }
-
- function post($url, $vars = array())
- {
- return $this->request('POST', $url, $vars);
- }
-
- function put($url, $vars = array())
- {
- return $this->request('PUT', $url, $vars);
- }
-
- # Protected
- function request($method, $url, $vars = array())
- {
- $this->handle = curl_init();
-
- # Determine the request method and set the correct CURL option
- switch ($method) {
- case 'GET':
- curl_setopt($this->handle, CURLOPT_HTTPGET, true);
- break;
- case 'POST':
- curl_setopt($this->handle, CURLOPT_POST, true);
- break;
- default:
- curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
- }
-
- # Set some default CURL options
- curl_setopt($this->handle, CURLOPT_COOKIEFILE, $this->cookie_file);
- curl_setopt($this->handle, CURLOPT_COOKIEJAR, $this->cookie_file);
- curl_setopt($this->handle, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($this->handle, CURLOPT_HEADER, true);
- if (!empty($vars)) curl_setopt($this->handle, CURLOPT_POSTFIELDS, (is_array($vars) ? http_build_query($vars, '', '&') : $vars));
- curl_setopt($this->handle, CURLOPT_REFERER, $this->referer);
- curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->handle, CURLOPT_URL, $url);
- curl_setopt($this->handle, CURLOPT_USERAGENT, $this->user_agent);
-
- # Format custom headers for this request and set CURL option
- $headers = array();
- foreach ($this->headers as $key => $value) {
- $headers[] = $key.': '.$value;
- }
- curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
-
- # Set any custom CURL options
- foreach ($this->options as $option => $value) {
- curl_setopt($this->handle, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
- }
-
- $response = curl_exec($this->handle);
- if ($response) {
- $response = new CurlResponse($response);
- } else {
- $this->error = curl_errno($this->handle).' - '.curl_error($this->handle);
- }
- curl_close($this->handle);
- return $response;
- }
-}
-
-class CurlResponse
-{
- var $body = '';
- var $headers = array();
-
-
- function CurlResponse($response)
- {
- $this->__construct($response);
- }
-
- function __construct($response)
- {
- # Extract headers from response
- $pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
- preg_match_all($pattern, $response, $matches);
- $headers = split("\r\n", str_replace("\r\n\r\n", '', array_pop($matches[0])));
-
- # Extract the version and status from the first header
- $version_and_status = array_shift($headers);
- preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
- $this->headers['Http-Version'] = $matches[1];
- $this->headers['Status-Code'] = $matches[2];
- $this->headers['Status'] = $matches[2].' '.$matches[3];
-
- # Convert headers into an associative array
- foreach ($headers as $header) {
- preg_match('#(.*?)\:\s(.*)#', $header, $matches);
- $this->headers[$matches[1]] = $matches[2];
- }
-
- # Remove the headers from the response body
- $this->body = preg_replace($pattern, '', $response);
- }
-
- function __toString()
- {
- return $this->body;
- }
-}
-
-/**
- * http_build_query exists from PHP >= 5.0
- * If !function_exists then declare it.
- */
-if(!function_exists('http_build_query')) {
-
- /**
- * Generate URL-encoded query string.
- * See http://php.net/http_build_query for more details.
- *
- * @param mixed $formdata
- * @param string $numeric_prefix
- * @param string $arg_separator
- * @param string $key
- * @return string
- * @link http://php.net/http_build_query
- */
- function http_build_query($formdata, $numeric_prefix = null, $arg_separator = null, $key = null) {
- $res = array();
-
- foreach ((array)$formdata as $k => $v) {
- $tmp_key = urlencode(is_int($k) ? $numeric_prefix.$k : $k);
- if ($key !== null) {
- $tmp_key = $key.'['.$tmp_key.']';
- }
- if (is_array($v) || is_object($v)) {
- $res[] = http_build_query($v, null, $arg_separator, $tmp_key);
- } else {
- $res[] = $tmp_key . "=" . urlencode($v);
- }
- }
-
- if ($arg_separator === null) {
- $arg_separator = ini_get("arg_separator.output");
- }
-
- return implode($arg_separator, $res);
- }
-}
View
110 lib/curl.php
@@ -0,0 +1,110 @@
+<?php
+
+# Curl, CurlResponse
+#
+# Author Sean Huber - shuber@huberry.com
+# Date May 2008
+#
+# A basic CURL wrapper for PHP
+#
+# See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP
+
+class Curl
+{
+ public $cookie_file;
+ public $headers = array();
+ public $options = array();
+ public $referer = '';
+ public $user_agent = '';
+
+ protected $error = '';
+ protected $handle;
+
+
+ public function __construct()
+ {
+ $this->cookie_file = dirname(__FILE__).'/curl_cookie.txt';
+ $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ?
+ $_SERVER['HTTP_USER_AGENT'] :
+ 'Curl/PHP ' . PHP_VERSION . ' (http://github.com/shuber/curl/)';
+ }
+
+ public function delete($url, $vars = array())
+ {
+ return $this->request('DELETE', $url, $vars);
+ }
+
+ public function error()
+ {
+ return $this->error;
+ }
+
+ public function get($url, $vars = array())
+ {
+ if (!empty($vars)) {
+ $url .= (stripos($url, '?') !== false) ? '&' : '?';
+ $url .= http_build_query($vars, '', '&');
+ }
+ return $this->request('GET', $url);
+ }
+
+ public function post($url, $vars = array())
+ {
+ return $this->request('POST', $url, $vars);
+ }
+
+ public function put($url, $vars = array())
+ {
+ return $this->request('PUT', $url, $vars);
+ }
+
+ protected function request($method, $url, $vars = array())
+ {
+ $this->handle = curl_init();
+
+ # Determine the request method and set the correct CURL option
+ switch ($method) {
+ case 'GET':
+ curl_setopt($this->handle, CURLOPT_HTTPGET, true);
+ break;
+ case 'POST':
+ curl_setopt($this->handle, CURLOPT_POST, true);
+ break;
+ default:
+ curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
+ }
+
+ # Set some default CURL options
+ curl_setopt($this->handle, CURLOPT_COOKIEFILE, $this->cookie_file);
+ curl_setopt($this->handle, CURLOPT_COOKIEJAR, $this->cookie_file);
+ curl_setopt($this->handle, CURLOPT_FOLLOWLOCATION, true);
+ curl_setopt($this->handle, CURLOPT_HEADER, true);
+ if (!empty($vars)) curl_setopt($this->handle, CURLOPT_POSTFIELDS, (is_array($vars) ? http_build_query($vars, '', '&') : $vars));
+ curl_setopt($this->handle, CURLOPT_REFERER, $this->referer);
+ curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($this->handle, CURLOPT_URL, $url);
+ curl_setopt($this->handle, CURLOPT_USERAGENT, $this->user_agent);
+
+ # Format custom headers for this request and set CURL option
+ $headers = array();
+ foreach ($this->headers as $key => $value) {
+ $headers[] = $key.': '.$value;
+ }
+ curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
+
+ # Set any custom CURL options
+ foreach ($this->options as $option => $value) {
+ curl_setopt($this->handle, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
+ }
+
+ $response = curl_exec($this->handle);
+ if ($response) {
+ $response = new CurlResponse($response);
+ } else {
+ $this->error = curl_errno($this->handle).' - '.curl_error($this->handle);
+ }
+ curl_close($this->handle);
+ return $response;
+ }
+
+}
View
77 lib/curl_response.php
@@ -0,0 +1,77 @@
+<?php
+
+/**
+ * Parses the response from a Curl request into an object containing
+ * the response body and an associative array of headers
+ *
+ * @package curl
+ * @author Sean Huber <shuber@huberry.com>
+**/
+class CurlResponse {
+
+ /**
+ * The body of the response without the headers block
+ *
+ * @var string
+ **/
+ public $body = '';
+
+ /**
+ * An associative array containing the response's headers
+ *
+ * @var array
+ **/
+ public $headers = array();
+
+ /**
+ * Accepts the result of a curl request as a string
+ *
+ * <code>
+ * $response = new CurlResponse(curl_exec($curl_handle));
+ * echo $response->body;
+ * echo $response->headers['Status'];
+ * </code>
+ *
+ * @param string $response
+ **/
+ public function __construct($response) {
+ # Headers
+ $pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
+
+ # Extract headers from response
+ preg_match_all($pattern, $response, $matches);
+ $headers = split("\r\n", str_replace("\r\n\r\n", '', array_pop($matches[0])));
+
+ # Remove headers from the response body
+ $this->body = preg_replace($pattern, '', $response);
+
+ # Extract the version and status from the first header
+ $version_and_status = array_shift($headers);
+ preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
+ $this->headers['Http-Version'] = $matches[1];
+ $this->headers['Status-Code'] = $matches[2];
+ $this->headers['Status'] = $matches[2].' '.$matches[3];
+
+ # Convert headers into an associative array
+ foreach ($headers as $header) {
+ preg_match('#(.*?)\:\s(.*)#', $header, $matches);
+ $this->headers[$matches[1]] = $matches[2];
+ }
+ }
+
+ /**
+ * Returns the response body
+ *
+ * <code>
+ * $curl = new Curl;
+ * $response = $curl->get('google.com');
+ * echo $response; # => echo $response->body;
+ * </code>
+ *
+ * @return string
+ **/
+ public function __toString() {
+ return $this->body;
+ }
+
+}

0 comments on commit 0f3d840

Please sign in to comment.