|
| 1 | +--- |
| 2 | +title: PHP Curl Class Documentation |
| 3 | + |
| 4 | +language_tabs: |
| 5 | + - php |
| 6 | + |
| 7 | +toc_footers: |
| 8 | + - <a href="https://github.com/php-curl-class/php-curl-class" target="_blank">PHP Curl Class on Github</a> |
| 9 | +--- |
| 10 | + |
| 11 | +# Introduction |
| 12 | + |
| 13 | +PHP Curl Class is an object-oriented wrapper of the PHP cURL extension. |
| 14 | + |
| 15 | +# Install |
| 16 | + |
| 17 | +## Composer |
| 18 | + |
| 19 | +Install using Composer. |
| 20 | + |
| 21 | +```shell |
| 22 | +$ composer require php-curl-class/php-curl-class |
| 23 | +``` |
| 24 | + |
| 25 | +## Include |
| 26 | + |
| 27 | +Include PHP Curl Class. |
| 28 | + |
| 29 | +```php |
| 30 | +<?php |
| 31 | +require 'Curl/Curl.php'; |
| 32 | + |
| 33 | +use \Curl\Curl; |
| 34 | +``` |
| 35 | + |
| 36 | +# Usage |
| 37 | + |
| 38 | +## GET |
| 39 | + |
| 40 | +```php |
| 41 | +<?php |
| 42 | +require 'Curl/Curl.php'; |
| 43 | + |
| 44 | +use \Curl\Curl; |
| 45 | + |
| 46 | +$curl = new Curl(); |
| 47 | +$curl->get('https://www.example.com/', array( |
| 48 | + 'q' => 'keyword', |
| 49 | +)); |
| 50 | +``` |
| 51 | + |
| 52 | +## POST |
| 53 | + |
| 54 | +```php |
| 55 | +<?php |
| 56 | +$curl = new Curl(); |
| 57 | +$curl->post('https://www.example.com/login/', array( |
| 58 | + 'username' => 'myusername', |
| 59 | + 'password' => 'mypassword', |
| 60 | +)); |
| 61 | +``` |
| 62 | + |
| 63 | +## PUT |
| 64 | + |
| 65 | +```php |
| 66 | +<?php |
| 67 | +$curl = new Curl(); |
| 68 | +$curl->put('https://api.example.com/user/', array( |
| 69 | + 'first_name' => 'Zach', |
| 70 | + 'last_name' => 'Borboa', |
| 71 | +)); |
| 72 | +``` |
| 73 | + |
| 74 | +## PATCH |
| 75 | + |
| 76 | +```php |
| 77 | +<?php |
| 78 | +$curl = new Curl(); |
| 79 | +$curl->patch('https://api.example.com/profile/', array( |
| 80 | + 'image' => '@path/to/file.jpg', |
| 81 | +)); |
| 82 | +``` |
| 83 | + |
| 84 | +```php |
| 85 | +<?php |
| 86 | +$curl = new Curl(); |
| 87 | +$curl->patch('https://api.example.com/profile/', array( |
| 88 | + 'image' => new CURLFile('path/to/file.jpg'), |
| 89 | +)); |
| 90 | +``` |
| 91 | + |
| 92 | +## DELETE |
| 93 | + |
| 94 | +```php |
| 95 | +<?php |
| 96 | +$curl = new Curl(); |
| 97 | +$curl->delete('https://api.example.com/user/', array( |
| 98 | + 'id' => '1234', |
| 99 | +)); |
| 100 | +``` |
| 101 | + |
| 102 | +## HEAD |
| 103 | + |
| 104 | +```php |
| 105 | +<?php |
| 106 | +$curl = new Curl(); |
| 107 | +$curl->head('https://www.example.com/song.mp3'); |
| 108 | +``` |
| 109 | + |
| 110 | +## OPTIONS |
| 111 | + |
| 112 | +```php |
| 113 | +<?php |
| 114 | +$curl = new Curl(); |
| 115 | +$curl->options('https://www.example.com/'); |
| 116 | +``` |
| 117 | + |
| 118 | +## Curl Options |
| 119 | + |
| 120 | +```php |
| 121 | +<?php |
| 122 | +$curl = new Curl(); |
| 123 | +$curl->setOpt(CURLOPT_AUTOREFERER, true); |
| 124 | +$curl->get('https://www.example.com/302'); |
| 125 | +``` |
| 126 | + |
| 127 | +```php |
| 128 | +<?php |
| 129 | +$curl = new Curl(); |
| 130 | +// Enable gzip compression. |
| 131 | +$curl->setOpt(CURLOPT_ENCODING , 'gzip'); |
| 132 | +$curl->get('https://www.example.com/image.png'); |
| 133 | +``` |
| 134 | + |
| 135 | +## Authentication |
| 136 | + |
| 137 | +```php |
| 138 | +<?php |
| 139 | +$curl = new Curl(); |
| 140 | +$curl->setBasicAuthentication('username', 'password'); |
| 141 | +``` |
| 142 | + |
| 143 | +## User Agent |
| 144 | + |
| 145 | +```php |
| 146 | +<?php |
| 147 | +$curl = new Curl(); |
| 148 | +$curl->setUserAgent(''); |
| 149 | +``` |
| 150 | + |
| 151 | +## Referrer |
| 152 | + |
| 153 | +```php |
| 154 | +<?php |
| 155 | +$curl = new Curl(); |
| 156 | +$curl->setReferrer(''); |
| 157 | +``` |
| 158 | + |
| 159 | +## Cookies |
| 160 | + |
| 161 | +```php |
| 162 | +<?php |
| 163 | +$curl = new Curl(); |
| 164 | +$curl->setCookie('key', 'value'); |
| 165 | +``` |
| 166 | + |
| 167 | +## Request header |
| 168 | + |
| 169 | +```php |
| 170 | +<?php |
| 171 | +$curl = new Curl(); |
| 172 | +$curl->setHeader('X-Requested-With', 'XMLHttpRequest'); |
| 173 | +$curl->get('https://www.example.com/'); |
| 174 | + |
| 175 | +// Case-insensitive access to headers. |
| 176 | +echo $curl->request_headers['X-Requested-With'] . "\n"; // XMLHttpRequest |
| 177 | +echo $curl->request_headers['x-ReQUeStED-wiTH'] . "\n"; // XMLHttpRequest |
| 178 | +``` |
| 179 | + |
| 180 | +## Response header |
| 181 | + |
| 182 | +```php |
| 183 | +<?php |
| 184 | +$curl = new Curl(); |
| 185 | +$curl->get('https://www.example.com/image.png'); |
| 186 | + |
| 187 | +// Case-insensitive access to headers. |
| 188 | +echo $curl->response_headers['Content-Type'] . "\n"; // image/png |
| 189 | +echo $curl->response_headers['CoNTeNT-TyPE'] . "\n"; // image/png |
| 190 | +``` |
| 191 | + |
| 192 | +## Response body |
| 193 | + |
| 194 | +```php |
| 195 | +<?php |
| 196 | +$curl = new Curl(); |
| 197 | +$curl->get('https://www.example.com/'); |
| 198 | + |
| 199 | +echo $curl->response; |
| 200 | +``` |
| 201 | + |
| 202 | +## Curl object |
| 203 | + |
| 204 | +> Example access to curl object. |
| 205 | +
|
| 206 | +```php |
| 207 | +<?php |
| 208 | +$curl->setOpt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1'); |
| 209 | +// or |
| 210 | +curl_set_opt($curl->curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1'); |
| 211 | +``` |
| 212 | + |
| 213 | +```php |
| 214 | +<?php |
| 215 | +$curl->close(); |
| 216 | +// or |
| 217 | +curl_close($curl->curl); |
| 218 | +``` |
| 219 | + |
| 220 | +## Errors |
| 221 | +```php |
| 222 | +<?php |
| 223 | +$curl = new Curl(); |
| 224 | +$curl->get('https://www.example.com/404'); |
| 225 | + |
| 226 | +if ($curl->error) { |
| 227 | + echo 'Error code: ' . $curl->error_code . "\n"; // 404 |
| 228 | + echo 'Error message: ' . $curl->error_message . "\n"; // HTTP/1.1 404 Not Found |
| 229 | +} |
| 230 | +else { |
| 231 | + echo $curl->response; |
| 232 | +} |
| 233 | +``` |
| 234 | + |
| 235 | +## Parallel Requests |
| 236 | + |
| 237 | +```php |
| 238 | +<?php |
| 239 | +// Requests in parallel with callback functions. |
| 240 | +$curl = new Curl(); |
| 241 | + |
| 242 | +$curl->success(function($instance) { |
| 243 | + echo 'call was successful. response was' . "\n"; |
| 244 | + echo $instance->response . "\n"; |
| 245 | +}); |
| 246 | +$curl->error(function($instance) { |
| 247 | + echo 'call was unsuccessful.' . "\n"; |
| 248 | + echo 'error code:' . $instance->error_code . "\n"; |
| 249 | + echo 'error message:' . $instance->error_message . "\n"; |
| 250 | +}); |
| 251 | +$curl->complete(function($instance) { |
| 252 | + echo 'call completed' . "\n"; |
| 253 | +}); |
| 254 | + |
| 255 | +$curl->get(array( |
| 256 | + 'https://duckduckgo.com/', |
| 257 | + 'https://search.yahoo.com/search', |
| 258 | + 'https://www.bing.com/search', |
| 259 | + 'http://www.dogpile.com/search/web', |
| 260 | + 'https://www.google.com/search', |
| 261 | + 'https://www.wolframalpha.com/input/', |
| 262 | +), array( |
| 263 | + 'q' => 'hello world', |
| 264 | +)); |
| 265 | +``` |
0 commit comments