diff --git a/.gitignore b/.gitignore index 609ae46..6498e0e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,7 @@ -lib/curl_cookie.txt \ No newline at end of file +lib/veqryn/Curl/curl_cookie.txt +.idea/ +vendor/ +build/ +composer.lock +composer.phar +/logs/* diff --git a/.gitmodules b/.gitmodules index c782e85..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "test/ztest"] - path = test/ztest - url = git://github.com/jaz303/ztest.git diff --git a/README.markdown b/README.markdown index 743e538..7e3301f 100644 --- a/README.markdown +++ b/README.markdown @@ -5,7 +5,13 @@ A basic CURL wrapper for PHP (see [http://php.net/curl](http://php.net/curl) for ## Installation -Click the `download` link above or `git clone git://github.com/shuber/curl.git` +Click the `download` link above or `git clone git@github.com:veqryn/curl.git` + +To install into your project, add these lines to your composer.json: + + "require": { + "veqryn/curl": "*" + } ## Usage @@ -14,8 +20,15 @@ Click the `download` link above or `git clone git://github.com/shuber/curl.git` Simply require and initialize the `Curl` class like so: - require_once 'curl.php'; - $curl = new Curl; + require_once('vendor/autoload.php'); + + use veqryn\Curl\Curl; + use veqryn\Curl\CurlResponse; + use veqryn\Curl\CurlException; + + // ... + + $curl = new Curl(); ### Performing a Request @@ -34,9 +47,9 @@ To use a custom request methods, you can call the `request` method: All of the built in request methods like `put` and `get` simply wrap the `request` method. For example, the `post` method is implemented like: - function post($url, $vars = array()) { - return $this->request('POST', $url, $vars); - } + public function post($url, $vars = array(), $enctype = null) { + return $this->request('POST', $url, $vars, $enctype); + } Examples: @@ -47,7 +60,7 @@ Examples: $response = $curl->post('test.com/posts', array('title' => 'Test', 'body' => 'This is a test')); -All requests return a CurlResponse object (see below) or false if an error occurred. You can access the error string with the `$curl->error()` method. +All requests return a CurlResponse object (see below) or throw a CurlException if an error occurred. You can access the error string with the `$curl->error()` method. ### The CurlResponse Object @@ -130,11 +143,14 @@ You can set/override many different options for CURL requests (see the [curl_set ## Testing -Uses [ztest](http://github.com/jaz303/ztest), simply download it to `path/to/curl/test/ztest` (or anywhere else in your php include_path) +Uses [phpunit](https://phpunit.de/). Simply run phpunit in the 'test' directory. +Example on linux (assuming php is on your path): + + cd /test + ../vendor/bin/phpunit -Then run `test/runner.php` ## Contact -Problems, comments, and suggestions all welcome: [shuber@huberry.com](mailto:shuber@huberry.com) \ No newline at end of file +Problems, comments, and suggestions all welcome: [shuber@huberry.com](mailto:shuber@huberry.com) and/or VEQRYN [at] hotmail dot com diff --git a/composer.json b/composer.json index 8f0df6e..8ecc841 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,48 @@ { - "name": "shuber/curl", + "name": "veqryn/curl", "license": "MIT", - "keywords": ["curl","spider", "bot", "scraper"], + "keywords": [ + "curl", + "spider", + "bot", + "scraper", + "php", + "wrapper" + ], + "homepage": "https://github.com/veqryn/curl", "type": "library", "description": "PHP Wrapper for Curl", + "authors": [ + { + "name": "Sean Huber", + "email": "shuber@huberry.com", + "homepage": "https://github.com/shuber" + }, + { + "name": "Hugo Chinchilla Carbonell", + "email": "hugo@bulma.net", + "homepage": "https://github.com/hugochinchilla" + }, + { + "name": "Robert Hamstar", + "email": "hamstar@telescum.co.nz", + "homepage": "https://github.com/hamstar" + }, + { + "name": "Chris Duncan", + "email": "VEQRYN@hotmail.com", + "homepage": "https://github.com/veqryn" + } + ], + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "4.3.*" + }, "autoload": { - "files": ["curl.php"] + "psr-0": { + "veqryn\\Curl": "lib/" + } } } \ No newline at end of file diff --git a/curl.php b/curl.php deleted file mode 100644 index a41c113..0000000 --- a/curl.php +++ /dev/null @@ -1,5 +0,0 @@ - 'CURLE_ABORTED_BY_CALLBACK', - CURLE_BAD_CALLING_ORDER => 'CURLE_BAD_CALLING_ORDER', - CURLE_BAD_CONTENT_ENCODING => 'CURLE_BAD_CONTENT_ENCODING', - CURLE_BAD_FUNCTION_ARGUMENT => 'CURLE_BAD_FUNCTION_ARGUMENT', - CURLE_BAD_PASSWORD_ENTERED => 'CURLE_BAD_PASSWORD_ENTERED', - CURLE_COULDNT_CONNECT => 'CURLE_COULDNT_CONNECT', - CURLE_COULDNT_RESOLVE_HOST => 'CURLE_COULDNT_RESOLVE_HOST', - CURLE_COULDNT_RESOLVE_PROXY => 'CURLE_COULDNT_RESOLVE_PROXY', - CURLE_FAILED_INIT => 'CURLE_FAILED_INIT', - CURLE_FILE_COULDNT_READ_FILE => 'CURLE_FILE_COULDNT_READ_FILE', - CURLE_FILESIZE_EXCEEDED => 'CURLE_FILESIZE_EXCEEDED', - CURLE_FTP_ACCESS_DENIED => 'CURLE_FTP_ACCESS_DENIED', - CURLE_FTP_BAD_DOWNLOAD_RESUME => 'CURLE_FTP_BAD_DOWNLOAD_RESUME', - CURLE_FTP_CANT_GET_HOST => 'CURLE_FTP_CANT_GET_HOST', - CURLE_FTP_CANT_RECONNECT => 'CURLE_FTP_CANT_RECONNECT', - CURLE_FTP_COULDNT_GET_SIZE => 'CURLE_FTP_COULDNT_GET_SIZE', - CURLE_FTP_COULDNT_RETR_FILE => 'CURLE_FTP_COULDNT_RETR_FILE', - CURLE_FTP_COULDNT_SET_ASCII => 'CURLE_FTP_COULDNT_SET_ASCII', - CURLE_FTP_COULDNT_SET_BINARY => 'CURLE_FTP_COULDNT_SET_BINARY', - CURLE_FTP_COULDNT_STOR_FILE => 'CURLE_FTP_COULDNT_STOR_FILE', - CURLE_FTP_COULDNT_USE_REST => 'CURLE_FTP_COULDNT_USE_REST', - CURLE_FTP_PORT_FAILED => 'CURLE_FTP_PORT_FAILED', - CURLE_FTP_QUOTE_ERROR => 'CURLE_FTP_QUOTE_ERROR', - CURLE_FTP_SSL_FAILED => 'CURLE_FTP_SSL_FAILED', - CURLE_FTP_USER_PASSWORD_INCORRECT => 'CURLE_FTP_USER_PASSWORD_INCORRECT', - CURLE_FTP_WEIRD_227_FORMAT => 'CURLE_FTP_WEIRD_227_FORMAT', - CURLE_FTP_WEIRD_PASS_REPLY => 'CURLE_FTP_WEIRD_PASS_REPLY', - CURLE_FTP_WEIRD_PASV_REPLY => 'CURLE_FTP_WEIRD_PASV_REPLY', - CURLE_FTP_WEIRD_SERVER_REPLY => 'CURLE_FTP_WEIRD_SERVER_REPLY', - CURLE_FTP_WEIRD_USER_REPLY => 'CURLE_FTP_WEIRD_USER_REPLY', - CURLE_FTP_WRITE_ERROR => 'CURLE_FTP_WRITE_ERROR', - CURLE_FUNCTION_NOT_FOUND => 'CURLE_FUNCTION_NOT_FOUND', - CURLE_GOT_NOTHING => 'CURLE_GOT_NOTHING', - CURLE_HTTP_NOT_FOUND => 'CURLE_HTTP_NOT_FOUND', - CURLE_HTTP_PORT_FAILED => 'CURLE_HTTP_PORT_FAILED', - CURLE_HTTP_POST_ERROR => 'CURLE_HTTP_POST_ERROR', - CURLE_HTTP_RANGE_ERROR => 'CURLE_HTTP_RANGE_ERROR', - CURLE_LDAP_CANNOT_BIND => 'CURLE_LDAP_CANNOT_BIND', - CURLE_LDAP_INVALID_URL => 'CURLE_LDAP_INVALID_URL', - CURLE_LDAP_SEARCH_FAILED => 'CURLE_LDAP_SEARCH_FAILED', - CURLE_LIBRARY_NOT_FOUND => 'CURLE_LIBRARY_NOT_FOUND', - CURLE_MALFORMAT_USER => 'CURLE_MALFORMAT_USER', - CURLE_OBSOLETE => 'CURLE_OBSOLETE', - CURLE_OPERATION_TIMEOUTED => 'CURLE_OPERATION_TIMEOUTED', - CURLE_OUT_OF_MEMORY => 'CURLE_OUT_OF_MEMORY', - CURLE_PARTIAL_FILE => 'CURLE_PARTIAL_FILE', - CURLE_READ_ERROR => 'CURLE_READ_ERROR', - CURLE_RECV_ERROR => 'CURLE_RECV_ERROR', - CURLE_SEND_ERROR => 'CURLE_SEND_ERROR', - CURLE_SHARE_IN_USE => 'CURLE_SHARE_IN_USE', - CURLE_SSH => 'CURLE_SSH', - CURLE_SSL_CACERT => 'CURLE_SSL_CACERT', - CURLE_SSL_CERTPROBLEM => 'CURLE_SSL_CERTPROBLEM', - CURLE_SSL_CIPHER => 'CURLE_SSL_CIPHER', - CURLE_SSL_CONNECT_ERROR => 'CURLE_SSL_CONNECT_ERROR', - CURLE_SSL_ENGINE_NOTFOUND => 'CURLE_SSL_ENGINE_NOTFOUND', - CURLE_SSL_ENGINE_SETFAILED => 'CURLE_SSL_ENGINE_SETFAILED', - CURLE_SSL_PEER_CERTIFICATE => 'CURLE_SSL_PEER_CERTIFICATE', - CURLE_TELNET_OPTION_SYNTAX => 'CURLE_TELNET_OPTION_SYNTAX', - CURLE_TOO_MANY_REDIRECTS => 'CURLE_TOO_MANY_REDIRECTS', - CURLE_UNKNOWN_TELNET_OPTION => 'CURLE_UNKNOWN_TELNET_OPTION', - CURLE_UNSUPPORTED_PROTOCOL => 'CURLE_UNSUPPORTED_PROTOCOL', - CURLE_URL_MALFORMAT => 'CURLE_URL_MALFORMAT', - CURLE_URL_MALFORMAT_USER => 'CURLE_URL_MALFORMAT_USER', - CURLE_WRITE_ERROR => 'CURLE_WRITE_ERROR' - ); - - function __construct( $curl_error_message, $curl_error_code ) - { - if( ! array_key_exists( $curl_error_code, self::$curl_errors ) ) - throw new Exception( "Unknown \$curl_error_code: $curl_error_code" ); - - parent::__construct( self::$curl_errors[$curl_error_code].": $curl_error_message", $curl_error_code ); - } - -} \ No newline at end of file diff --git a/lib/curl.php b/lib/veqryn/Curl/Curl.php similarity index 63% rename from lib/curl.php rename to lib/veqryn/Curl/Curl.php index b5d0991..8675d21 100644 --- a/lib/curl.php +++ b/lib/veqryn/Curl/Curl.php @@ -1,220 +1,240 @@ -**/ + **/ class Curl { - + /** * The file to read and write cookies to for requests * * @var string - **/ + **/ public $cookie_file; - + /** * Determines whether or not requests should follow redirects * * @var boolean - **/ + **/ public $follow_redirects = true; - + /** * An associative array of headers to send along with requests * * @var array - **/ + **/ public $headers = array(); - + /** * An associative array of CURLOPT options to send along with requests * * @var array - **/ + **/ public $options = array(); - + /** * The referer header to send along with requests * * @var string - **/ + **/ public $referer; - + /** * The user agent to send along with requests * * @var string - **/ + **/ public $user_agent; - + /** * Stores resource handle for the current CURL request * * @var resource * @access protected - **/ + **/ protected $request; - + /** * Stores the HTTP auth credentials * * @var $userpwd * @access protected - **/ + **/ protected $userpwd; - + /** * Initializes a Curl object * * Sets the $cookie_file to "curl_cookie.txt" in the current directory - * Also sets the $user_agent to $_SERVER['HTTP_USER_AGENT'] if it exists, 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)' otherwise - **/ - 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)'; + * Also sets the $user_agent to $_SERVER['HTTP_USER_AGENT'] if it exists, 'Curl/PHP '.PHP_VERSION.' (http://github.com/veqryn/curl)' otherwise + **/ + 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/veqryn/curl)'; } - + /** * 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 + * Returns a CurlResponse object if the request was successful. + * If unsuccessful, throws CurlException * * @param string $url - * @param array|string $vars + * @param mixed|string|array $vars * @return CurlResponse object - **/ - function delete($url, $vars = array()) { + * @throws CurlException + */ + public function delete($url, $vars = array()) { return $this->request('DELETE', $url, $vars); } - + /** * 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 + * Returns a CurlResponse object if the request was successful. + * If unsuccessful, throws CurlException * * @param string $url - * @param array|string $vars - * @return CurlResponse - **/ - function get($url, $vars = array()) { + * @param mixed|array|string $vars + * @return CurlResponse object + * @throws CurlException + **/ + public function get($url, $vars = array()) { if (!empty($vars)) { $url .= (stripos($url, '?') !== false) ? '&' : '?'; $url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&'); } return $this->request('GET', $url); } - + /** * 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 + * Returns a CurlResponse object if the request was successful. + * If unsuccessful, throws CurlException * * @param string $url - * @param array|string $vars - * @return CurlResponse - **/ - function head($url, $vars = array()) { + * @param mixed|array|string $vars + * @return CurlResponse object + * @throws CurlException + **/ + 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 * + * Returns a CurlResponse object if the request was successful. + * If unsuccessful, throws CurlException + * * @param string $url - * @param array|string $vars - * @return CurlResponse|boolean - **/ - function post($url, $vars = array(), $enctype = NULL) { + * @param mixed|array|string $vars + * @param mixed|string|null $enctype + * @return CurlResponse object + * @throws CurlException + */ + public function post($url, $vars = array(), $enctype = null) { return $this->request('POST', $url, $vars, $enctype); } - + /** * 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 + * Returns a CurlResponse object if the request was successful. + * If unsuccessful, throws CurlException * * @param string $url - * @param array|string $vars - * @return CurlResponse|boolean - **/ - function put($url, $vars = array()) { + * @param mixed|array|string $vars + * @return CurlResponse + * @throws CurlException + **/ + public function put($url, $vars = array()) { return $this->request('PUT', $url, $vars); } - + /** * 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 + * Returns a CurlResponse object if the request was successful. + * If unsuccessful, throws CurlException * * @param string $method * @param string $url - * @param array|string $vars - * @return CurlResponse|boolean - **/ - function request($method, $url, $vars = array(), $enctype = null) { + * @param mixed|array|string $vars + * @param mixed|string|null $enctype + * @return CurlResponse object + * @throws CurlException + */ + public function request($method, $url, $vars = array(), $enctype = null) { $this->request = curl_init(); - if (is_array($vars) && $enctype != 'multipart/form-data') $vars = http_build_query($vars, '', '&'); - + if (is_array($vars) && $enctype != 'multipart/form-data') { + $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) { - throw new CurlException(curl_error($this->request), curl_errno($this->request)); + throw new CurlException(curl_error($this->request), curl_errno($this->request)); } - + $response = new CurlResponse($response); - + curl_close($this->request); - + return $response; } - + /** * Sets the user and password for HTTP auth basic authentication method. * - * @param string|null $username - * @param string|null $password - * @return Curl + * @param mixed|string|null $username + * @param mixed|string|null $password + * @return Curl this for chaining */ - function setAuth($username, $password=null) - { - if (null === $username) { - $this->userpwd = null; + public function setAuth($username, $password = null) { + if (null === $username) { + $this->userpwd = null; + return $this; + } + + $this->userpwd = $username . ':' . $password; return $this; - } - - $this->userpwd = $username.':'.$password; - return $this; } - + /** * 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; + $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 (strtoupper($method)) { case 'HEAD': @@ -230,7 +250,7 @@ protected function set_request_method($method) { curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method); } } - + /** * Sets the CURLOPT options for the current request * @@ -238,11 +258,13 @@ protected function set_request_method($method) { * @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); - + if (!empty($vars)) { + curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars); + } + # Set some default CURL options curl_setopt($this->request, CURLOPT_HEADER, true); curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true); @@ -251,29 +273,33 @@ protected function set_request_options($url, $vars) { curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file); curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file); } - if ($this->follow_redirects) curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true); - if ($this->referer) curl_setopt($this->request, CURLOPT_REFERER, $this->referer); + if ($this->follow_redirects) { + curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true); + } + if ($this->referer) { + curl_setopt($this->request, CURLOPT_REFERER, $this->referer); + } if ($this->userpwd) { - curl_setopt($this->request, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - curl_setopt($this->request, CURLOPT_USERPWD, $this->userpwd); + curl_setopt($this->request, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($this->request, CURLOPT_USERPWD, $this->userpwd); } else { - curl_setopt($this->request, CURLOPT_HTTPAUTH, false); + curl_setopt($this->request, CURLOPT_HTTPAUTH, false); } - + # Set any custom CURL options foreach ($this->options as $option => $value) { - curl_setopt($this->request, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value); + curl_setopt($this->request, constant('CURLOPT_' . str_replace('CURLOPT_', '', strtoupper($option))), $value); } } - + /** - * Returns an associative array of curl options - * currently configured. + * Returns an associative array of curl options currently configured. + * Includes everything in "curl_getinfo" * * @return array Associative array of curl options */ - function get_request_options() { - return curl_getinfo( $this->request ); + public function get_request_options() { + return curl_getinfo($this->request); } -} \ No newline at end of file +} diff --git a/lib/veqryn/Curl/CurlException.php b/lib/veqryn/Curl/CurlException.php new file mode 100644 index 0000000..a3b05b6 --- /dev/null +++ b/lib/veqryn/Curl/CurlException.php @@ -0,0 +1,93 @@ + + */ +class CurlException extends \Exception { + + static public $curl_errors = array( + CURLE_ABORTED_BY_CALLBACK => 'CURLE_ABORTED_BY_CALLBACK', + CURLE_BAD_CALLING_ORDER => 'CURLE_BAD_CALLING_ORDER', + CURLE_BAD_CONTENT_ENCODING => 'CURLE_BAD_CONTENT_ENCODING', + CURLE_BAD_FUNCTION_ARGUMENT => 'CURLE_BAD_FUNCTION_ARGUMENT', + CURLE_BAD_PASSWORD_ENTERED => 'CURLE_BAD_PASSWORD_ENTERED', + CURLE_COULDNT_CONNECT => 'CURLE_COULDNT_CONNECT', + CURLE_COULDNT_RESOLVE_HOST => 'CURLE_COULDNT_RESOLVE_HOST', + CURLE_COULDNT_RESOLVE_PROXY => 'CURLE_COULDNT_RESOLVE_PROXY', + CURLE_FAILED_INIT => 'CURLE_FAILED_INIT', + CURLE_FILE_COULDNT_READ_FILE => 'CURLE_FILE_COULDNT_READ_FILE', + CURLE_FILESIZE_EXCEEDED => 'CURLE_FILESIZE_EXCEEDED', + CURLE_FTP_ACCESS_DENIED => 'CURLE_FTP_ACCESS_DENIED', + CURLE_FTP_BAD_DOWNLOAD_RESUME => 'CURLE_FTP_BAD_DOWNLOAD_RESUME', + CURLE_FTP_CANT_GET_HOST => 'CURLE_FTP_CANT_GET_HOST', + CURLE_FTP_CANT_RECONNECT => 'CURLE_FTP_CANT_RECONNECT', + CURLE_FTP_COULDNT_GET_SIZE => 'CURLE_FTP_COULDNT_GET_SIZE', + CURLE_FTP_COULDNT_RETR_FILE => 'CURLE_FTP_COULDNT_RETR_FILE', + CURLE_FTP_COULDNT_SET_ASCII => 'CURLE_FTP_COULDNT_SET_ASCII', + CURLE_FTP_COULDNT_SET_BINARY => 'CURLE_FTP_COULDNT_SET_BINARY', + CURLE_FTP_COULDNT_STOR_FILE => 'CURLE_FTP_COULDNT_STOR_FILE', + CURLE_FTP_COULDNT_USE_REST => 'CURLE_FTP_COULDNT_USE_REST', + CURLE_FTP_PORT_FAILED => 'CURLE_FTP_PORT_FAILED', + CURLE_FTP_QUOTE_ERROR => 'CURLE_FTP_QUOTE_ERROR', + CURLE_FTP_SSL_FAILED => 'CURLE_FTP_SSL_FAILED', + CURLE_FTP_USER_PASSWORD_INCORRECT => 'CURLE_FTP_USER_PASSWORD_INCORRECT', + CURLE_FTP_WEIRD_227_FORMAT => 'CURLE_FTP_WEIRD_227_FORMAT', + CURLE_FTP_WEIRD_PASS_REPLY => 'CURLE_FTP_WEIRD_PASS_REPLY', + CURLE_FTP_WEIRD_PASV_REPLY => 'CURLE_FTP_WEIRD_PASV_REPLY', + CURLE_FTP_WEIRD_SERVER_REPLY => 'CURLE_FTP_WEIRD_SERVER_REPLY', + CURLE_FTP_WEIRD_USER_REPLY => 'CURLE_FTP_WEIRD_USER_REPLY', + CURLE_FTP_WRITE_ERROR => 'CURLE_FTP_WRITE_ERROR', + CURLE_FUNCTION_NOT_FOUND => 'CURLE_FUNCTION_NOT_FOUND', + CURLE_GOT_NOTHING => 'CURLE_GOT_NOTHING', + CURLE_HTTP_NOT_FOUND => 'CURLE_HTTP_NOT_FOUND', + CURLE_HTTP_PORT_FAILED => 'CURLE_HTTP_PORT_FAILED', + CURLE_HTTP_POST_ERROR => 'CURLE_HTTP_POST_ERROR', + CURLE_HTTP_RANGE_ERROR => 'CURLE_HTTP_RANGE_ERROR', + CURLE_LDAP_CANNOT_BIND => 'CURLE_LDAP_CANNOT_BIND', + CURLE_LDAP_INVALID_URL => 'CURLE_LDAP_INVALID_URL', + CURLE_LDAP_SEARCH_FAILED => 'CURLE_LDAP_SEARCH_FAILED', + CURLE_LIBRARY_NOT_FOUND => 'CURLE_LIBRARY_NOT_FOUND', + CURLE_MALFORMAT_USER => 'CURLE_MALFORMAT_USER', + CURLE_OBSOLETE => 'CURLE_OBSOLETE', + CURLE_OPERATION_TIMEOUTED => 'CURLE_OPERATION_TIMEOUTED', + CURLE_OUT_OF_MEMORY => 'CURLE_OUT_OF_MEMORY', + CURLE_PARTIAL_FILE => 'CURLE_PARTIAL_FILE', + CURLE_READ_ERROR => 'CURLE_READ_ERROR', + CURLE_RECV_ERROR => 'CURLE_RECV_ERROR', + CURLE_SEND_ERROR => 'CURLE_SEND_ERROR', + CURLE_SHARE_IN_USE => 'CURLE_SHARE_IN_USE', + CURLE_SSH => 'CURLE_SSH', + CURLE_SSL_CACERT => 'CURLE_SSL_CACERT', + CURLE_SSL_CERTPROBLEM => 'CURLE_SSL_CERTPROBLEM', + CURLE_SSL_CIPHER => 'CURLE_SSL_CIPHER', + CURLE_SSL_CONNECT_ERROR => 'CURLE_SSL_CONNECT_ERROR', + CURLE_SSL_ENGINE_NOTFOUND => 'CURLE_SSL_ENGINE_NOTFOUND', + CURLE_SSL_ENGINE_SETFAILED => 'CURLE_SSL_ENGINE_SETFAILED', + CURLE_SSL_PEER_CERTIFICATE => 'CURLE_SSL_PEER_CERTIFICATE', + CURLE_TELNET_OPTION_SYNTAX => 'CURLE_TELNET_OPTION_SYNTAX', + CURLE_TOO_MANY_REDIRECTS => 'CURLE_TOO_MANY_REDIRECTS', + CURLE_UNKNOWN_TELNET_OPTION => 'CURLE_UNKNOWN_TELNET_OPTION', + CURLE_UNSUPPORTED_PROTOCOL => 'CURLE_UNSUPPORTED_PROTOCOL', + CURLE_URL_MALFORMAT => 'CURLE_URL_MALFORMAT', + CURLE_URL_MALFORMAT_USER => 'CURLE_URL_MALFORMAT_USER', + CURLE_WRITE_ERROR => 'CURLE_WRITE_ERROR' + ); + + /** + * @param string $curl_error_message + * @param int $curl_error_code + */ + public function __construct($curl_error_message, $curl_error_code) { + if (!array_key_exists($curl_error_code, self::$curl_errors)) { + parent::__construct("Unknown \$curl_error_code: $curl_error_code with message: " . $curl_error_message, $curl_error_code); + } else { + parent::__construct(self::$curl_errors[$curl_error_code] . ": $curl_error_message", $curl_error_code); + } + } + +} \ No newline at end of file diff --git a/lib/curl_response.php b/lib/veqryn/Curl/CurlResponse.php similarity index 86% rename from lib/curl_response.php rename to lib/veqryn/Curl/CurlResponse.php index a1b07b2..65e6526 100644 --- a/lib/curl_response.php +++ b/lib/veqryn/Curl/CurlResponse.php @@ -1,26 +1,29 @@ -**/ + **/ 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(); /** @@ -33,8 +36,11 @@ class CurlResponse { * * * @param string $response - **/ - function __construct($response) { + **/ + public function __construct($response) { + if (empty($response)) { + return; + } # Headers regex $pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims'; @@ -45,7 +51,7 @@ function __construct($response) { # Inlude all received headers in the $headers_string while (count($matches[0])) { - $headers_string = array_pop($matches[0]).$headers_string; + $headers_string = array_pop($matches[0]) . $headers_string; } # Remove all headers from the response body @@ -69,14 +75,14 @@ function __construct($response) { * Returns the response body * * - * $curl = new Curl; + * $curl = new Curl(); * $response = $curl->get('google.com'); * echo $response; # => echo $response->body; * * * @return string - **/ - function __toString() { + **/ + public function __toString() { return $this->body; } diff --git a/test/CurlResponseTest.php b/test/CurlResponseTest.php new file mode 100644 index 0000000..c3587f9 --- /dev/null +++ b/test/CurlResponseTest.php @@ -0,0 +1,46 @@ +responseMultipleHeader = new CurlResponse(file_get_contents(realpath(__DIR__ . '/data/multiple_response_header.txt'))); + $curl = new Curl(); + $this->responseGoogle = $curl->get('www.google.com'); + } + + public function testSeparateHeadersFromBody() { + $this->assertTrue(is_array($this->responseGoogle->headers) && !empty($this->responseGoogle->headers)); + $this->assertTrue(is_string($this->responseGoogle->body) && !empty($this->responseGoogle->body)); + $this->assertContains('responseGoogle->body); + $this->assertNotContains('responseGoogle->headers); + } + + public function testSetStatusHeaders() { + $this->assertEquals(200, $this->responseGoogle->headers['Status-Code']); + $this->assertEquals('200 OK', $this->responseGoogle->headers['Status']); + } + + public function testToStringEqualToBody() { + $this->assertEquals($this->responseGoogle->body, ('' . $this->responseGoogle)); + } + + public function testMultipleResponseHeadersFromBody() { + $this->assertTrue(is_array($this->responseMultipleHeader->headers) && !empty($this->responseMultipleHeader->headers)); + $this->assertTrue(is_string($this->responseMultipleHeader->body) && !empty($this->responseMultipleHeader->body)); + $this->assertContains('{example: 1}', $this->responseMultipleHeader->body); + $this->assertEquals(200, $this->responseMultipleHeader->headers['Status-Code']); + $this->assertEquals('200 OK', $this->responseMultipleHeader->headers['Status']); + } +} \ No newline at end of file diff --git a/test/CurlTest.php b/test/CurlTest.php new file mode 100644 index 0000000..bcb1a0e --- /dev/null +++ b/test/CurlTest.php @@ -0,0 +1,29 @@ +get('www.google.com'); + $this->assertEquals(200, $response->headers['Status-Code']); + $this->assertContains('google', $response->body); + } + + public function testGetFails() { + $curl = new Curl(); + try { + $response = $curl->get('diaewkaksdljf-invalid-url-dot-com.com'); + } catch (CurlException $e) { + $this->assertEquals("CURLE_COULDNT_RESOLVE_HOST: Couldn't resolve host 'diaewkaksdljf-invalid-url-dot-com.com'", $e->getMessage()); + return; + } + $this->assertTrue(false); + } +} \ No newline at end of file diff --git a/test/unit/multiple_response_header.txt b/test/data/multiple_response_header.txt similarity index 100% rename from test/unit/multiple_response_header.txt rename to test/data/multiple_response_header.txt diff --git a/test/phpunit.xml b/test/phpunit.xml new file mode 100644 index 0000000..ef4d565 --- /dev/null +++ b/test/phpunit.xml @@ -0,0 +1,11 @@ + + + + + . + + + + + + diff --git a/test/runner.php b/test/runner.php deleted file mode 100644 index 1b05e2f..0000000 --- a/test/runner.php +++ /dev/null @@ -1,14 +0,0 @@ -enable_color(); - -$suite = new ztest\TestSuite('Curl and CurlResponse unit tests'); -$suite->require_all(__DIR__.DIRECTORY_SEPARATOR.'unit'); -$suite->auto_fill(); -$suite->run($reporter); \ No newline at end of file diff --git a/test/test_helper.php b/test/test_helper.php deleted file mode 100644 index 45925ee..0000000 --- a/test/test_helper.php +++ /dev/null @@ -1,47 +0,0 @@ -response = new CurlResponse(file_get_contents(__DIR__ . '/multiple_response_header.txt')); - } - - function test_should_separate_response_headers_from_body() { - ensure(is_array($this->response->headers)); - assert_matches('#^{example: 1}#', $this->response->body); - } - - function test_should_set_status_headers() { - assert_equal(200, $this->response->headers['Status-Code']); - assert_equal('200 OK', $this->response->headers['Status']); - } - - function test_should_return_response_body_when_calling_toString() { - ob_start(); - echo $this->response; - assert_equal($this->response->body, ob_get_clean()); - } - -} \ No newline at end of file diff --git a/test/unit/curl_response_test.php b/test/unit/curl_response_test.php deleted file mode 100644 index 225c7ca..0000000 --- a/test/unit/curl_response_test.php +++ /dev/null @@ -1,26 +0,0 @@ -curl = new Curl; - $this->response = $this->curl->get('www.google.com'); - } - - function test_should_separate_response_headers_from_body() { - ensure(is_array($this->response->headers)); - assert_matches('#^response->body); - } - - function test_should_set_status_headers() { - assert_equal(200, $this->response->headers['Status-Code']); - assert_equal('200 OK', $this->response->headers['Status']); - } - - function test_should_return_response_body_when_calling_toString() { - ob_start(); - echo $this->response; - assert_equal($this->response->body, ob_get_clean()); - } - -} \ No newline at end of file diff --git a/test/unit/curl_test.php b/test/unit/curl_test.php deleted file mode 100644 index 3591e20..0000000 --- a/test/unit/curl_test.php +++ /dev/null @@ -1,22 +0,0 @@ -curl = new Curl; - $this->response = $this->curl->get('www.google.com'); - } - - function test_get() { - assert_matches('#google#', $this->response); - assert_equal(200, $this->response->headers['Status-Code']); - } - - function test_error() { - assert_throws('CurlException', function() { - $curl = new Curl(); - $curl->get('diaewkaksdljf-invalid-url-dot-com.com'); - }); - } - -} \ No newline at end of file diff --git a/test/ztest b/test/ztest deleted file mode 160000 index 0a31703..0000000 --- a/test/ztest +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 0a31703d3c5f80c17ff1ce755893e5ba79a863ea