├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── example.php └── src └── CurlHttpClient.php /.gitignore: -------------------------------------------------------------------------------- 1 | !.gitignore 2 | .project 3 | .settings/ 4 | .buildpath 5 | .DS_Store 6 | *~ 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CurlHttpClient 2 | ================ 3 | Curl based HTTP Client - Simple but effective OOP wrapper around Curl php lib. 4 | It allows sending post/get requests, using proxy, binding to specific IP, storing cookies etc. 5 | 6 | # Installation 7 | Add CurlHttpClient to your composer.json 8 | 9 | ``` 10 | { 11 | "require": { 12 | "dinke/curl-http-client": "dev-master" 13 | } 14 | } 15 | ``` 16 | 17 | # Usage 18 | ```php 19 | 20 | $curl = new \Dinke\CurlHttpClient; 21 | 22 | //pretend to be Firefox 19.0 on Mac 23 | $useragent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:19.0) Gecko/20100101 Firefox/19.0"; 24 | $curl->setUserAgent($useragent); 25 | 26 | //uncomment next two lines if you want to automatically manage cookies 27 | //which will store them to file and send them on each http request 28 | //$cookies_file = "/tmp/cookies.txt"; 29 | //$curl->storeCookies($cookies_file); 30 | 31 | //Uncomment next line if you want to set credentials for basic http_auth 32 | //$curl->setCredentials($username, $password); 33 | 34 | //Uncomment next line if you want to set specific referrer 35 | //$curl->setReferer('http://www.foo.com/referer_url/'); 36 | 37 | //if you want to send some post data 38 | //form post data array like this one 39 | $post_data = ['login' => 'pera', 'password' => 'joe', 'other_foo_field' => 'foo_value']; 40 | //or like a string: $post_data = 'login=pera&password=joe&other_foo_field=foo_value'; 41 | 42 | //and send request to http://www.foo.com/login.php. Result page is stored in $html_data string 43 | $html_data = $curl->sendPostData("http://www.foo.com/login.php", $post_data); 44 | 45 | //You can also fetch data from somewhere using get method! 46 | //Fetch html from url 47 | $html_data = $curl->fetchUrl("http://www.foo.com/foobar.php?login=pera&password=joe&other_foo_field=foo_value"); 48 | 49 | //if you have more than one IP on your server, 50 | //you can also bind to specific IP address like ... 51 | //$bind_ip = "192.168.0.1"; 52 | //$curl->fetch_url("http://www.foo.com/login.php", $bind_ip); 53 | //$html_data = $curl->sendPostData("http://www.foo.com/login.php", $post_data, $bind_ip); 54 | 55 | //and there are many other things you can do like 56 | 57 | //use proxy 58 | //$curl->setProxy('http://www.proxyurl.com'); 59 | 60 | //use proxy auth 61 | //$curl->setProxyAuth('user:pass'); 62 | 63 | //get http response code for last request 64 | //$http_code = $curl->getHttpResponseCode(); 65 | 66 | //get last http request duration in sec 67 | //$duration = $curl->get_requestDuration(); 68 | 69 | ``` 70 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dinke/curl-http-client", 3 | "type": "library", 4 | "description": "Curl based HTTP Client - Simple but effective OOP wrapper around Curl php lib. It allows sending post/get requests, using proxy, binding to specific IP, storing cookies etc.", 5 | "keywords": ["curl"], 6 | "homepage": "https://github.com/dinke/curl_http_client", 7 | "license": "LGPLv3", 8 | "authors": [ 9 | { 10 | "name": "Dragan Dinic", 11 | "email": "dragan@dinke.net", 12 | "homepage": "http://www.dinke.net", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.3.0", 18 | "ext-curl": "*" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "Dinke\\": "src/" 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | 6 | * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License 7 | */ 8 | 9 | require 'vendor/autoload.php'; 10 | 11 | $curl = new \Dinke\CurlHttpClient; 12 | 13 | //pretend to be Firefox 19.0 on Mac 14 | $useragent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:19.0) Gecko/20100101 Firefox/19.0"; 15 | $curl->setUserAgent($useragent); 16 | 17 | //uncomment next two lines if you want to automatically manage cookies 18 | //which will store them to file and send them on each http request 19 | //$cookies_file = "/tmp/cookies.txt"; 20 | //$curl->storeCookies($cookies_file); 21 | 22 | //Uncomment next line if you want to set credentials for basic http_auth 23 | //$curl->setCredentials($username, $password); 24 | 25 | //Uncomment next line if you want to set specific referrer 26 | //$curl->setReferer('http://www.foo.com/referer_url/'); 27 | 28 | //if you want to send some post data 29 | //form post data array like this one 30 | $post_data = ['login' => 'pera', 'password' => 'joe', 'other_foo_field' => 'foo_value']; 31 | //or like a string: $post_data = 'login=pera&password=joe&other_foo_field=foo_value'; 32 | 33 | //and send request to http://www.foo.com/login.php. Result page is stored in $html_data string 34 | $html_data = $curl->sendPostData("http://www.foo.com/login.php", $post_data); 35 | 36 | //You can also fetch data from somewhere using get method! 37 | //Fetch html from url 38 | $html_data = $curl->fetchUrl("http://www.foo.com/foobar.php?login=pera&password=joe&other_foo_field=foo_value"); 39 | 40 | //if you have more than one IP on your server, 41 | //you can also bind to specific IP address like ... 42 | //$bind_ip = "192.168.0.1"; 43 | //$curl->fetchUrl("http://www.foo.com/login.php", $bind_ip); 44 | //$html_data = $curl->sendPostData("http://www.foo.com/login.php", $post_data, $bind_ip); 45 | 46 | //and there are many other things you can do like 47 | 48 | //use proxy 49 | //$curl->setProxy('http://www.proxyurl.com'); 50 | 51 | //use proxy auth 52 | //$curl->setProxyAuth('user:pass'); 53 | 54 | //get http response code for last request 55 | //$http_code = $curl->getHttpResponseCode(); 56 | 57 | //get last http request duration in sec 58 | //$duration = $curl->getRequestDuration(); 59 | 60 | 61 | ?> 62 | -------------------------------------------------------------------------------- /src/CurlHttpClient.php: -------------------------------------------------------------------------------- 1 | 9 | * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License 10 | */ 11 | 12 | /** 13 | * Curl based HTTP Client 14 | * Simple but effective OOP wrapper around Curl php lib. 15 | * 16 | * Contains common methods needed for getting data from url, setting referrer, credentials, 17 | * sending post data, managing cookies, etc. 18 | * 19 | * Samle usage: 20 | * $curl = new /Dinke/CurlHttpClient; 21 | * $useragent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:19.0)"; 22 | * $curl->setUserAgent($useragent); 23 | * $curl->storeCookies("/tmp/cookies.txt"); 24 | * $post_data = ['login' => 'pera', 'password' => 'joe']; 25 | * $html_data = $curl->sendPostData(http://www.foo.com/login.php, $post_data); 26 | */ 27 | 28 | class CurlHttpClient 29 | { 30 | /** 31 | * Curl handle 32 | * 33 | * @access protected 34 | * @var resource 35 | */ 36 | protected $ch; 37 | 38 | 39 | /** 40 | * Default curl options 41 | * (more details about each option: http://www.php.net/manual/en/function.curl-setopt-array.php) 42 | * @var array 43 | * @access protected 44 | */ 45 | protected $config = [ 46 | CURLOPT_FAILONERROR => false, //whether to fail if http response code is >=400 47 | CURLOPT_FOLLOWLOCATION => true, //whether to follow any 'Location:..' header from response 48 | CURLOPT_AUTOREFERER => true, //whether to automatically set referer for http redirections 49 | CURLOPT_ENCODING => 'gzip, deflate', //The contents of the Accept-Encoding header in curl request 50 | CURLOPT_SSL_VERIFYPEER => false, //whether to verify ssl peer's certificate 51 | CURLOPT_HEADER => false, //whether to add response headers to the output 52 | CURLOPT_USERAGENT => 'CurlHttpClient/v2.0', //default user agent if none is set 53 | CURLOPT_SSLVERSION => 1, //force cURL to use TLSv1 (prevent it from using SSLv3 ever) 54 | ]; 55 | 56 | /** 57 | * CurlHttpClient constructor 58 | * 59 | * @access public 60 | */ 61 | public function __construct () 62 | { 63 | $this->init(); 64 | } 65 | 66 | /** 67 | * Init new Curl handle 68 | * 69 | * @access public 70 | */ 71 | public function init () 72 | { 73 | //create new curl handle 74 | $this->ch = curl_init(); 75 | 76 | //set options 77 | curl_setopt_array($this->ch, $this->config); 78 | } 79 | 80 | /** 81 | * Set custom curl option 82 | * (usually not needed to call this directly, advanced users only) 83 | * 84 | * @param int $opt 85 | * @param int $value 86 | * 87 | * @access public 88 | */ 89 | public function setCustomOption ($opt, $value) 90 | { 91 | curl_setopt($this->ch, $opt, $value); 92 | } 93 | 94 | /** 95 | * Set client's useragent 96 | * 97 | * @param string $useragent 98 | * 99 | * @access public 100 | */ 101 | public function setUserAgent ($useragent) 102 | { 103 | curl_setopt($this->ch, CURLOPT_USERAGENT, $useragent); 104 | } 105 | 106 | /** 107 | * Set custom referer 108 | * 109 | * @param string $referer_url 110 | * 111 | * @access public 112 | */ 113 | public function setReferer ($referer_url) 114 | { 115 | curl_setopt($this->ch, CURLOPT_REFERER, $referer_url); 116 | } 117 | 118 | /** 119 | * Whether to include response headers in results 120 | * 121 | * @param boolean true to include response headers, false to suppress them 122 | * 123 | * @access public 124 | */ 125 | public function includeResponseHeaders ($value) 126 | { 127 | curl_setopt($this->ch, CURLOPT_HEADER, $value); 128 | } 129 | 130 | /** 131 | * Set username/pass for basic http auth 132 | * 133 | * @param string $username 134 | * @param string $password 135 | * 136 | * @access public 137 | */ 138 | public function setCredentials ($username, $password) 139 | { 140 | curl_setopt($this->ch, CURLOPT_USERPWD, "$username:$password"); 141 | } 142 | 143 | /** 144 | * Set proxy to use for each curl request 145 | * 146 | * @param string $proxy_url 147 | * @param boolean $socks5 whether to use socks5 [=false] 148 | * 149 | * @access public 150 | */ 151 | public function setProxy ($proxy_url, $socks5 = false) 152 | { 153 | curl_setopt($this->ch, CURLOPT_PROXY, $proxy_url); 154 | 155 | //set proxy to use socks5 156 | if ($socks5) 157 | { 158 | curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); 159 | } 160 | } 161 | 162 | /** 163 | * Set proxy auth 164 | * 165 | * @param $proxy_auth 166 | */ 167 | public function setProxyAuth ($proxy_auth) 168 | { 169 | curl_setopt($this->ch, CURLOPT_PROXYUSERPWD, $proxy_auth); 170 | } 171 | 172 | /** 173 | * Fetch data from passed URL (by using http get method) 174 | * 175 | * @param string $url 176 | * @param string $ip address to bind (default null) 177 | * @param int $timeout in sec for complete curl operation [=10] 178 | * 179 | * @return mixed string data returned from url or boolean false if error occured 180 | * @access public 181 | */ 182 | public function fetchUrl ($url, $ip = null, $timeout = 10) 183 | { 184 | //set various curl options first 185 | 186 | // set url to post to 187 | curl_setopt($this->ch, CURLOPT_URL, $url); 188 | 189 | //set method to get 190 | curl_setopt($this->ch, CURLOPT_HTTPGET, true); 191 | 192 | // return into a variable rather than displaying it 193 | curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); 194 | 195 | //bind to specific ip address if it is sent trough arguments 196 | if ($ip) 197 | { 198 | curl_setopt($this->ch, CURLOPT_INTERFACE, $ip); 199 | } 200 | 201 | //set curl function timeout to $timeout 202 | curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout); 203 | 204 | //and finally send curl request 205 | $result = curl_exec($this->ch); 206 | 207 | if ($this->hasError()) 208 | { 209 | return false; 210 | } 211 | 212 | return $result; 213 | } 214 | 215 | 216 | /** 217 | * Send post request to target URL 218 | * 219 | * @param string $url 220 | * @param mixed $postdata (assoc array ie. $foo['post_var_name'] = $value or as string like var=val1&var2=val2) 221 | * @param string $ip IP address to bind to [=null] 222 | * @param int $timeout Timeout in sec for complete curl operation [=10] 223 | * 224 | * @return mixed string with data returned from url or boolean false if error occured 225 | * @access public 226 | */ 227 | public function sendPostData ($url, $postdata, $ip = null, $timeout = 10) 228 | { 229 | //set various curl options first 230 | 231 | // set url to post to 232 | curl_setopt($this->ch, CURLOPT_URL, $url); 233 | 234 | // return into a variable rather than displaying it 235 | curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); 236 | 237 | //bind to specific ip address if it is sent trough arguments 238 | if ($ip) 239 | { 240 | curl_setopt($this->ch, CURLOPT_INTERFACE, $ip); 241 | } 242 | 243 | //set curl function timeout to $timeout 244 | curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout); 245 | 246 | //set method to post 247 | curl_setopt($this->ch, CURLOPT_POST, true); 248 | 249 | $post_string = is_array($postdata) ? http_build_query($postdata) : $postdata; 250 | 251 | // set post string 252 | curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post_string); 253 | 254 | //and finally send curl request 255 | $result = curl_exec($this->ch); 256 | 257 | if ($this->hasError()) 258 | { 259 | return false; 260 | } 261 | 262 | return $result; 263 | } 264 | 265 | /** 266 | * Fetch data from target URL and store it directly into file 267 | * 268 | * @param string $url 269 | * @param resource $fp stream resource(ie. fopen) 270 | * @param string $ip address to bind (default null) 271 | * @param int $timeout in sec for complete curl operation (default 5) 272 | * 273 | * @return boolean true on success false othervise 274 | * @access public 275 | */ 276 | public function fetchIntoFile ($url, $fp, $ip = null, $timeout = 5) 277 | { 278 | // set url to post to 279 | curl_setopt($this->ch, CURLOPT_URL, $url); 280 | 281 | //set method to get 282 | curl_setopt($this->ch, CURLOPT_HTTPGET, true); 283 | 284 | // store data into file rather than displaying it 285 | curl_setopt($this->ch, CURLOPT_FILE, $fp); 286 | 287 | //bind to specific ip address if it is sent trough arguments 288 | if ($ip) 289 | { 290 | curl_setopt($this->ch, CURLOPT_INTERFACE, $ip); 291 | } 292 | 293 | //set curl function timeout to $timeout 294 | curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout); 295 | 296 | //and finally send curl request 297 | curl_exec($this->ch); 298 | 299 | if ($this->hasError()) 300 | { 301 | return false; 302 | } 303 | 304 | return true; 305 | } 306 | 307 | /** 308 | * Set file location where cookie data will be stored on curl handle close 309 | * and then parsed and send along on new requests 310 | * 311 | * @param string $cookie_file absolute path to cookie file (must be in writable dir) 312 | * 313 | * @access public 314 | */ 315 | public function storeCookies ($cookie_file) 316 | { 317 | //make sure all cookies are stored to $cookie_file when curl handle is closed 318 | curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookie_file); 319 | 320 | //The name of the file containing the cookie data 321 | curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_file); 322 | } 323 | 324 | 325 | /** 326 | * Set custom cookie 327 | * 328 | * @param mixed string or array with key=>value (i.e. array('foo'=>'value')) 329 | * 330 | * @access public 331 | */ 332 | public function setCookie ($cookie) 333 | { 334 | //if cookie is sent as key=>value array 335 | if (is_array($cookie)) 336 | { 337 | $cookies_data = []; 338 | foreach ($cookie as $key => $value) 339 | { 340 | $cookies_data[] = "{$key}={$value}"; 341 | } 342 | 343 | //and implode (useful if more than one cookie is sent along so we separate them by ;) 344 | $cookie = implode('; ', $cookies_data); 345 | } 346 | 347 | curl_setopt($this->ch, CURLOPT_COOKIE, $cookie); 348 | } 349 | 350 | /** 351 | * Wraper around curl's getinfo method 352 | * http://www.php.net/manual/en/function.curl-getinfo.php 353 | * 354 | * @param int $opt 355 | * 356 | * @access public 357 | * @return mixed array with all data or sting (depends of $opt param) 358 | */ 359 | public function getInfo ($opt = 0) 360 | { 361 | return curl_getinfo($this->ch, $opt); 362 | } 363 | 364 | /** 365 | * Get last URL info 366 | * (usefull when original url was redirected to other location) 367 | * 368 | * @access public 369 | * @return string url 370 | */ 371 | public function getEffectiveUrl () 372 | { 373 | return $this->getInfo(CURLINFO_EFFECTIVE_URL); 374 | } 375 | 376 | /** 377 | * Get http response code 378 | * 379 | * @access public 380 | * @return int 381 | */ 382 | public function getHttpResponseCode () 383 | { 384 | return $this->getInfo(CURLINFO_HTTP_CODE); 385 | } 386 | 387 | /** 388 | * Get http reqeust headers generated with last request 389 | * 390 | * @access public 391 | * @return string 392 | */ 393 | public function getRequestHeaders () 394 | { 395 | return $this->getInfo(CURLINFO_HEADER_OUT); 396 | } 397 | 398 | /** 399 | * Total reqeust time in seconds for last transfer 400 | * 401 | * @access public 402 | * @return int 403 | */ 404 | public function getRequestDuration () 405 | { 406 | return $this->getInfo(CURLINFO_TOTAL_TIME); 407 | } 408 | 409 | /** 410 | * Return nice formatted last curl error message and error number 411 | * 412 | * @return string error msg 413 | * @access public 414 | */ 415 | public function getErrorMsg () 416 | { 417 | return "Curl error #" . curl_errno($this->ch) . ": " . curl_error($this->ch); 418 | } 419 | 420 | /** 421 | * Return true if we had an error during last curl request 422 | * 423 | * @access public 424 | * @return boolean 425 | */ 426 | public function hasError () 427 | { 428 | return (curl_errno($this->ch) != 0) ? true : false; 429 | } 430 | 431 | /** 432 | * Close curl session and free resource 433 | * Usually no need to call this function directly but in case you do (i.e. to free resources), 434 | * you'll have to call $this->init() in order to recreate curl handle 435 | * 436 | * @access public 437 | */ 438 | public function close () 439 | { 440 | //close curl session and free up resources 441 | curl_close($this->ch); 442 | } 443 | 444 | /** 445 | * Curl_HTTP_Client destructor 446 | * 447 | * @access public 448 | */ 449 | public function __destruct () 450 | { 451 | $this->close(); 452 | } 453 | } 454 | --------------------------------------------------------------------------------