--- title: PHP Curl Class Documentation language_tabs: - php toc_footers: - PHP Curl Class on Github --- # Introduction PHP Curl Class is an object-oriented wrapper of the PHP cURL extension. # Install ## Composer Install using Composer. ```shell $ composer require php-curl-class/php-curl-class ``` ## Include Include PHP Curl Class. ```php get('https://www.example.com/', array( 'q' => 'keyword', )); ``` ## POST ```php post('https://www.example.com/login/', array( 'username' => 'myusername', 'password' => 'mypassword', )); ``` ## PUT ```php put('https://api.example.com/user/', array( 'first_name' => 'Zach', 'last_name' => 'Borboa', )); ``` ## PATCH ```php patch('https://api.example.com/profile/', array( 'image' => '@path/to/file.jpg', )); ``` ```php patch('https://api.example.com/profile/', array( 'image' => new CURLFile('path/to/file.jpg'), )); ``` ## DELETE ```php delete('https://api.example.com/user/', array( 'id' => '1234', )); ``` ## HEAD ```php head('https://www.example.com/song.mp3'); ``` ## OPTIONS ```php options('https://www.example.com/'); ``` ## Curl Options ```php setOpt(CURLOPT_AUTOREFERER, true); $curl->get('https://www.example.com/302'); ``` ```php setOpt(CURLOPT_ENCODING , 'gzip'); $curl->get('https://www.example.com/image.png'); ``` ## Authentication ```php setBasicAuthentication('username', 'password'); ``` ## User Agent ```php setUserAgent(''); ``` ## Referrer ```php setReferrer(''); ``` ## Cookies ```php setCookie('key', 'value'); ``` ## Request header ```php setHeader('X-Requested-With', 'XMLHttpRequest'); $curl->get('https://www.example.com/'); // Case-insensitive access to headers. echo $curl->request_headers['X-Requested-With'] . "\n"; // XMLHttpRequest echo $curl->request_headers['x-ReQUeStED-wiTH'] . "\n"; // XMLHttpRequest ``` ## Response header ```php get('https://www.example.com/image.png'); // Case-insensitive access to headers. echo $curl->response_headers['Content-Type'] . "\n"; // image/png echo $curl->response_headers['CoNTeNT-TyPE'] . "\n"; // image/png ``` ## Response body ```php get('https://www.example.com/'); echo $curl->response; ``` ## Curl object > Example access to curl object. ```php setOpt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1'); // or curl_set_opt($curl->curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1'); ``` ```php close(); // or curl_close($curl->curl); ``` ## Errors ```php get('https://www.example.com/404'); if ($curl->error) { echo 'Error code: ' . $curl->error_code . "\n"; // 404 echo 'Error message: ' . $curl->error_message . "\n"; // HTTP/1.1 404 Not Found } else { echo $curl->response; } ``` ## Parallel Requests ```php success(function($instance) { echo 'call was successful. response was' . "\n"; echo $instance->response . "\n"; }); $curl->error(function($instance) { echo 'call was unsuccessful.' . "\n"; echo 'error code:' . $instance->error_code . "\n"; echo 'error message:' . $instance->error_message . "\n"; }); $curl->complete(function($instance) { echo 'call completed' . "\n"; }); $curl->get(array( 'https://duckduckgo.com/', 'https://search.yahoo.com/search', 'https://www.bing.com/search', 'http://www.dogpile.com/search/web', 'https://www.google.com/search', 'https://www.wolframalpha.com/input/', ), array( 'q' => 'hello world', )); ```