Permalink
Browse files

Refactor

  • Loading branch information...
1 parent 0f3d840 commit 848b2d5fa5f4c5aac7cda5cce1c2cc4c5b5f6c79 @shuber committed Sep 5, 2009
Showing with 166 additions and 76 deletions.
  1. +1 −1 .gitignore
  2. +164 −72 lib/curl.php
  3. +1 −3 test.php
View
@@ -1 +1 @@
-curl_cookie.txt
+lib/curl_cookie.txt
View
@@ -1,110 +1,202 @@
<?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
-{
+/**
+ * A basic CURL wrapper
+ *
+ * See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP
+ *
+ * @package curl
+ * @author Sean Huber <shuber@huberry.com>
+**/
+class Curl {
+
public $cookie_file;
+ public $follow_redirects = true;
public $headers = array();
public $options = array();
- public $referer = '';
- public $user_agent = '';
-
+ 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/)';
+ protected $request;
+
+ public function __construct() {
+ $this->cookie_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'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())
- {
+
+ /**
+ * Makes an HTTP DELETE request to the specified $url with an optional array or string of $vars
+ *
+ * Returns a CurlResponse object if the request was successful, false otherwise
+ *
+ * @param string $url
+ * @param array|string $vars
+ * @return CurlResponse object
+ **/
+ public function delete($url, $vars = array()) {
return $this->request('DELETE', $url, $vars);
}
-
- public function error()
- {
+
+ /**
+ * Returns the error string of the current request if one occurred
+ *
+ * @return string
+ **/
+ public function error() {
return $this->error;
}
-
- public function get($url, $vars = array())
- {
+
+ /**
+ * Makes an HTTP GET request to the specified $url with an optional array or string of $vars
+ *
+ * Returns a CurlResponse object if the request was successful, false otherwise
+ *
+ * @param string $url
+ * @param array|string $vars
+ * @return CurlResponse
+ **/
+ public function get($url, $vars = array()) {
if (!empty($vars)) {
$url .= (stripos($url, '?') !== false) ? '&' : '?';
- $url .= http_build_query($vars, '', '&');
+ $url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&');
}
return $this->request('GET', $url);
}
-
- public function post($url, $vars = array())
- {
+
+ /**
+ * Makes an HTTP HEAD request to the specified $url with an optional array or string of $vars
+ *
+ * Returns a CurlResponse object if the request was successful, false otherwise
+ *
+ * @param string $url
+ * @param array|string $vars
+ * @return CurlResponse
+ **/
+ public function head($url, $vars = array()) {
+ return $this->request('HEAD', $url, $vars);
+ }
+
+ /**
+ * Makes an HTTP POST request to the specified $url with an optional array or string of $vars
+ *
+ * @param string $url
+ * @param array|string $vars
+ * @return CurlResponse|boolean
+ **/
+ public function post($url, $vars = array()) {
return $this->request('POST', $url, $vars);
}
-
- public function put($url, $vars = array())
- {
+
+ /**
+ * Makes an HTTP PUT request to the specified $url with an optional array or string of $vars
+ *
+ * Returns a CurlResponse object if the request was successful, false otherwise
+ *
+ * @param string $url
+ * @param array|string $vars
+ * @return CurlResponse|boolean
+ **/
+ public function put($url, $vars = array()) {
return $this->request('PUT', $url, $vars);
}
-
- protected function request($method, $url, $vars = array())
- {
- $this->handle = curl_init();
+
+ /**
+ * Makes an HTTP request of the specified $method to a $url with an optional array or string of $vars
+ *
+ * Returns a CurlResponse object if the request was successful, false otherwise
+ *
+ * @param string $method
+ * @param string $url
+ * @param array|string $vars
+ * @return CurlResponse|boolean
+ **/
+ function request($method, $url, $vars = array()) {
+ $this->error = '';
+ $this->request = curl_init();
+ if (is_array($vars)) $vars = http_build_query($vars, '', '&');
+
+ $this->set_request_method($method);
+ $this->set_request_options($url, $vars);
+ $this->set_request_headers();
+
+ $response = curl_exec($this->request);
+
+ if ($response) {
+ $response = new CurlResponse($response);
+ } else {
+ $this->error = curl_errno($this->request).' - '.curl_error($this->request);
+ }
+
+ curl_close($this->request);
- # Determine the request method and set the correct CURL option
+ return $response;
+ }
+
+ /**
+ * Formats and adds custom headers to the current request
+ *
+ * @return void
+ * @access protected
+ **/
+ protected function set_request_headers() {
+ $headers = array();
+ foreach ($this->headers as $key => $value) {
+ $headers[] = $key.': '.$value;
+ }
+ curl_setopt($this->request, CURLOPT_HTTPHEADER, $headers);
+ }
+
+ /**
+ * Set the associated CURL options for a request method
+ *
+ * @param string $method
+ * @return void
+ * @access protected
+ **/
+ protected function set_request_method($method) {
switch ($method) {
+ case 'HEAD':
+ curl_setopt($this->request, CURLOPT_NOBODY, true);
+ break;
case 'GET':
- curl_setopt($this->handle, CURLOPT_HTTPGET, true);
+ curl_setopt($this->request, CURLOPT_HTTPGET, true);
break;
case 'POST':
- curl_setopt($this->handle, CURLOPT_POST, true);
+ curl_setopt($this->request, CURLOPT_POST, true);
break;
default:
- curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
+ curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method);
}
+ }
+
+ /**
+ * Sets the CURLOPT options for the current request
+ *
+ * @param string $url
+ * @param string $vars
+ * @return void
+ * @access protected
+ **/
+ protected function set_request_options($url, $vars) {
+ curl_setopt($this->request, CURLOPT_URL, $url);
+ if (!empty($vars)) curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars);
# 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->request, CURLOPT_HEADER, true);
+ curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($this->request, CURLOPT_USERAGENT, $this->user_agent);
+ if ($this->cookie_file) {
+ curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file);
+ curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file);
}
- curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
+ if ($this->follow_redirects) curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true);
+ if ($this->referer) curl_setopt($this->request, CURLOPT_REFERER, $this->referer);
# Set any custom CURL options
foreach ($this->options as $option => $value) {
- curl_setopt($this->handle, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
+ curl_setopt($this->request, 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
@@ -3,6 +3,4 @@
require 'curl.php';
$curl = new Curl;
-print_r($curl->get('google.com')->headers);
-
-?>
+print_r($curl->get('google.com')->headers);

0 comments on commit 848b2d5

Please sign in to comment.