diff --git a/.gitignore b/.gitignore index 609ae46..f8734df 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -lib/curl_cookie.txt \ No newline at end of file +lib/curl_cookie.txt +.idea/* +.DS_Store \ No newline at end of file diff --git a/README.markdown b/README.markdown index 743e538..b2fd844 100644 --- a/README.markdown +++ b/README.markdown @@ -14,9 +14,9 @@ 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; + use Curl\Curl; + $curl = new Curl; ### Performing a Request diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..21caa76 --- /dev/null +++ b/composer.json @@ -0,0 +1,13 @@ +{ + "name": "duantianyu/curl", + "type": "library", + "description": "A basic CURL wrapper for PHP Edit", + "keywords": ["curl","php curl"], + "version": "0.1.1", + "require": { + "php": ">=5.3" + }, + "autoload": { + "psr-4": {"Curl\\": "lib/"} + } +} diff --git a/curl.php b/curl.php deleted file mode 100644 index e452354..0000000 --- a/curl.php +++ /dev/null @@ -1,4 +0,0 @@ -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)'; } @@ -88,7 +106,7 @@ function __construct() { * @param array|string $vars * @return CurlResponse object **/ - function delete($url, $vars = array()) { + public function delete($url, $vars = array()) { return $this->request('DELETE', $url, $vars); } @@ -97,7 +115,7 @@ function delete($url, $vars = array()) { * * @return string **/ - function error() { + public function error() { return $this->error; } @@ -110,7 +128,7 @@ function error() { * @param array|string $vars * @return CurlResponse **/ - function get($url, $vars = array()) { + public function get($url, $vars = array()) { if (!empty($vars)) { $url .= (stripos($url, '?') !== false) ? '&' : '?'; $url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&'); @@ -127,7 +145,7 @@ function get($url, $vars = array()) { * @param array|string $vars * @return CurlResponse **/ - function head($url, $vars = array()) { + public function head($url, $vars = array()) { return $this->request('HEAD', $url, $vars); } @@ -138,7 +156,7 @@ function head($url, $vars = array()) { * @param array|string $vars * @return CurlResponse|boolean **/ - function post($url, $vars = array()) { + public function post($url, $vars = array()) { return $this->request('POST', $url, $vars); } @@ -151,7 +169,7 @@ function post($url, $vars = array()) { * @param array|string $vars * @return CurlResponse|boolean **/ - function put($url, $vars = array()) { + public function put($url, $vars = array()) { return $this->request('PUT', $url, $vars); } @@ -165,7 +183,7 @@ function put($url, $vars = array()) { * @param array|string $vars * @return CurlResponse|boolean **/ - function request($method, $url, $vars = array()) { + public function request($method, $url, $vars = array()) { $this->error = ''; $this->request = curl_init(); if (is_array($vars)) $vars = http_build_query($vars, '', '&'); @@ -173,7 +191,8 @@ function request($method, $url, $vars = array()) { $this->set_request_method($method); $this->set_request_options($url, $vars); $this->set_request_headers(); - + $this->set_request_timeout(); + $response = curl_exec($this->request); if ($response) { @@ -244,6 +263,9 @@ 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->cookie){ + curl_setopt($this->request,CURLOPT_COOKIE, $this->cookie); + } if ($this->follow_redirects) curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true); if ($this->referer) curl_setopt($this->request, CURLOPT_REFERER, $this->referer); @@ -253,4 +275,14 @@ protected function set_request_options($url, $vars) { } } + /** + * Set timeout to the current request + * + * @return void + * @access protected + **/ + protected function set_request_timeout() { + if(is_numeric($this->timeout)) curl_setopt($this->request, CURLOPT_TIMEOUT, $this->timeout); + } + } \ No newline at end of file diff --git a/lib/curl_response.php b/lib/CurlResponse.php similarity index 99% rename from lib/curl_response.php rename to lib/CurlResponse.php index 324bafd..da1a69c 100644 --- a/lib/curl_response.php +++ b/lib/CurlResponse.php @@ -1,5 +1,7 @@