├── .github └── FUNDING.yml ├── .gitignore ├── 01_Basics ├── 01_Request_Methods │ ├── 01_Get │ │ ├── console.sh │ │ ├── curl-ext.php │ │ ├── guzzle-lib.php │ │ └── http-request.http │ ├── 02_Post_Raw_Data │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php │ ├── 03_Post_Form_Data │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php │ ├── 04_Put │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php │ ├── 05_Patch │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php │ └── 06_Delete │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php ├── 02_Headers │ ├── 01_Send_Request_Headers │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php │ └── 02_Get_Response_Headers │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php ├── 03_Request_Stats │ ├── console.sh │ ├── curl-ext.php │ └── guzzle-lib.php ├── 04_Debug_Request │ ├── 01_Output_Debug_Info_To_Stderr │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php │ └── 02_Output_Debug_Info_To_File │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php ├── 05_Error_Catching │ ├── console.sh │ ├── curl-ext.php │ └── guzzle-lib.php ├── 06_Follow_Redirects │ ├── console.sh │ ├── curl-ext.php │ └── guzzle-lib.php ├── 07_Timeouts │ ├── console.sh │ ├── curl-ext.php │ └── guzzle-lib.php ├── 08_Set_Http_Version │ ├── console.sh │ ├── curl-ext.php │ └── guzzle-lib.php ├── 09_Get_Curl_Version │ ├── console.sh │ └── curl-ext.php ├── 10_Set_User_Agent │ ├── console.sh │ ├── curl-ext.php │ └── guzzle-lib.php ├── 12_Redirect_Location_History │ ├── console.sh │ ├── curl-ext.php │ └── guzzle-lib.php └── 13_Set_Http_Referer │ ├── console.sh │ ├── curl-ext.php │ └── guzzle-lib.php ├── 02_Advanced ├── 01_Files │ ├── 01_Upload │ │ ├── console.sh │ │ ├── curl-ext.php │ │ ├── guzzle-lib.php │ │ └── resource │ │ │ └── file.txt │ ├── 02_Upload_Multiple │ │ ├── console.sh │ │ ├── curl-ext.php │ │ ├── guzzle-lib.php │ │ └── resource │ │ │ ├── file.txt │ │ │ └── github-icon.png │ ├── 03_Upload_Array_Of_Files │ │ ├── console.sh │ │ ├── curl-ext.php │ │ ├── guzzle-lib.php │ │ └── resource │ │ │ ├── file-1.txt │ │ │ ├── file-2.txt │ │ │ └── github-icon.png │ └── 04_Download │ │ ├── console.sh │ │ ├── curl-ext.php │ │ ├── guzzle-lib.php │ │ └── resource │ │ └── image.jpeg ├── 02_Auth │ ├── 01_Basic_Auth │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php │ ├── 02_Digest_Auth │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php │ └── 03_Bearer_Auth │ │ ├── console.sh │ │ ├── curl-ext.php │ │ └── guzzle-lib.php └── 03_Cookies │ ├── 01_Send_Cookies_From_String │ ├── console.sh │ ├── curl-ext.php │ └── guzzle-lib.php │ ├── 02_Set_Cookie_Options │ └── guzzle-lib.php │ ├── 03_Send_Cookies_From_File │ ├── console.sh │ ├── curl-ext.php │ ├── guzzle-lib.php │ └── resource │ │ ├── cookie-jar.txt │ │ └── guzzle-cookie-jar.json │ └── 04_Save_Response_Cookies_To_File │ ├── console.sh │ ├── curl-ext.php │ ├── guzzle-lib.php │ └── resource │ └── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── console ├── docker-compose.yml ├── docker └── php-fpm │ └── Dockerfile └── run /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=LYNYHKQJXGD36&source=url 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/01_Get/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request GET "https://postman-echo.com/get?foo=bar" -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/01_Get/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/get?foo=bar', 7 | CURLOPT_RETURNTRANSFER => true, 8 | ]); 9 | 10 | $response = curl_exec($curlHandler); 11 | 12 | curl_close($curlHandler); 13 | 14 | echo($response); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/01_Get/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 11 | 'https://postman-echo.com/get', 12 | [ 13 | RequestOptions::QUERY => [ 14 | 'foo' => 'bar', 15 | ], 16 | ] 17 | ); 18 | 19 | echo( 20 | $response->getBody()->getContents() 21 | ); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/01_Get/http-request.http: -------------------------------------------------------------------------------- 1 | GET https://postman-echo.com/get?foo=bar -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/02_Post_Raw_Data/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request POST "https://postman-echo.com/post" --data "POST raw request content" -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/02_Post_Raw_Data/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/post', 7 | CURLOPT_RETURNTRANSFER => true, 8 | 9 | /** 10 | * Specify POST method 11 | */ 12 | CURLOPT_POST => true, 13 | 14 | /** 15 | * Specify request content 16 | */ 17 | CURLOPT_POSTFIELDS => 'POST raw request content', 18 | ]); 19 | 20 | $response = curl_exec($curlHandler); 21 | 22 | curl_close($curlHandler); 23 | 24 | echo($response); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/02_Post_Raw_Data/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | post( 11 | 'https://postman-echo.com/post', 12 | [ 13 | RequestOptions::BODY => 'POST raw request content', 14 | RequestOptions::HEADERS => [ 15 | 'Content-Type' => 'application/x-www-form-urlencoded', 16 | ], 17 | ] 18 | ); 19 | 20 | echo($response->getBody()->getContents()); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/03_Post_Form_Data/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request POST "https://postman-echo.com/post" --data "foo=bar&baz=biz" -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/03_Post_Form_Data/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/post', 7 | CURLOPT_RETURNTRANSFER => true, 8 | 9 | /** 10 | * Specify POST method 11 | */ 12 | CURLOPT_POST => true, 13 | 14 | /** 15 | * Specify array of form fields 16 | */ 17 | CURLOPT_POSTFIELDS => [ 18 | 'foo' => 'bar', 19 | 'baz' => 'biz', 20 | ], 21 | ]); 22 | 23 | $response = curl_exec($curlHandler); 24 | 25 | curl_close($curlHandler); 26 | 27 | echo($response); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/03_Post_Form_Data/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | post( 11 | 'https://postman-echo.com/post', 12 | [ 13 | RequestOptions::FORM_PARAMS => [ 14 | 'foo' => 'bar', 15 | 'baz' => 'biz', 16 | ], 17 | ] 18 | ); 19 | 20 | echo( 21 | $response->getBody()->getContents() 22 | ); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/04_Put/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request PUT "https://postman-echo.com/put" --data "foo=bar&baz=biz" -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/04_Put/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/put', 7 | CURLOPT_RETURNTRANSFER => true, 8 | 9 | /** 10 | * Specify custom HTTP request method 11 | */ 12 | CURLOPT_CUSTOMREQUEST => 'PUT', 13 | 14 | /** 15 | * Specify request body (can be array or string) 16 | */ 17 | CURLOPT_POSTFIELDS => [ 18 | 'foo' => 'bar', 19 | 'baz' => 'biz', 20 | ] 21 | ]); 22 | 23 | $pageContent = curl_exec($curlHandler); 24 | 25 | curl_close($curlHandler); 26 | 27 | echo($pageContent); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/04_Put/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | put( 11 | 'https://postman-echo.com/put', 12 | [ 13 | RequestOptions::FORM_PARAMS => [ 14 | 'foo' => 'bar', 15 | 'baz' => 'biz', 16 | ], 17 | ] 18 | ); 19 | 20 | echo( 21 | $response->getBody()->getContents() 22 | ); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/05_Patch/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request PATCH "https://postman-echo.com/patch" --data "foo=bar&baz=biz" -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/05_Patch/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/patch', 7 | CURLOPT_RETURNTRANSFER => true, 8 | 9 | /** 10 | * Specify custom HTTP request method 11 | */ 12 | CURLOPT_CUSTOMREQUEST => 'PATCH', 13 | 14 | /** 15 | * Specify request body (can be array or string) 16 | */ 17 | CURLOPT_POSTFIELDS => [ 18 | 'foo' => 'bar', 19 | 'baz' => 'biz', 20 | ] 21 | ]); 22 | 23 | $pageContent = curl_exec($curlHandler); 24 | 25 | curl_close($curlHandler); 26 | 27 | echo($pageContent); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/05_Patch/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | patch( 11 | 'https://postman-echo.com/patch', 12 | [ 13 | RequestOptions::FORM_PARAMS => [ 14 | 'foo' => 'bar', 15 | 'baz' => 'biz', 16 | ], 17 | ] 18 | ); 19 | 20 | echo( 21 | $response->getBody()->getContents() 22 | ); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/06_Delete/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request DELETE "https://postman-echo.com/delete" --data "foo=bar&baz=biz" -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/06_Delete/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/delete', 7 | CURLOPT_RETURNTRANSFER => true, 8 | 9 | /** 10 | * Specify custom HTTP request method 11 | */ 12 | CURLOPT_CUSTOMREQUEST => 'DELETE', 13 | 14 | /** 15 | * Specify request body (can be array or string) 16 | */ 17 | CURLOPT_POSTFIELDS => [ 18 | 'foo' => 'bar', 19 | 'baz' => 'biz', 20 | ] 21 | ]); 22 | 23 | $pageContent = curl_exec($curlHandler); 24 | 25 | curl_close($curlHandler); 26 | 27 | echo($pageContent); -------------------------------------------------------------------------------- /01_Basics/01_Request_Methods/06_Delete/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | delete( 11 | 'https://postman-echo.com/delete', 12 | [ 13 | RequestOptions::FORM_PARAMS => [ 14 | 'foo' => 'bar', 15 | 'baz' => 'biz', 16 | ], 17 | ] 18 | ); 19 | 20 | echo( 21 | $response->getBody()->getContents() 22 | ); -------------------------------------------------------------------------------- /01_Basics/02_Headers/01_Send_Request_Headers/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request GET "https://postman-echo.com/headers" --header "foo: bar" --header "baz: biz" -------------------------------------------------------------------------------- /01_Basics/02_Headers/01_Send_Request_Headers/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/headers', 7 | CURLOPT_RETURNTRANSFER => true, 8 | 9 | /** 10 | * Specify request headers 11 | */ 12 | CURLOPT_HTTPHEADER => [ 13 | 'foo: bar', 14 | 'baz: biz', 15 | ] 16 | ]); 17 | 18 | $pageContent = curl_exec($curlHandler); 19 | 20 | curl_close($curlHandler); 21 | 22 | echo($pageContent); -------------------------------------------------------------------------------- /01_Basics/02_Headers/01_Send_Request_Headers/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 11 | 'https://postman-echo.com/headers', 12 | [ 13 | RequestOptions::HEADERS => [ 14 | 'foo' => 'bar', 15 | 'baz' => 'biz', 16 | ], 17 | ] 18 | ); 19 | 20 | echo($response->getBody()->getContents()); -------------------------------------------------------------------------------- /01_Basics/02_Headers/02_Get_Response_Headers/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request GET "https://postman-echo.com/response-headers?Content-Type=text/html&foo=bar" -------------------------------------------------------------------------------- /01_Basics/02_Headers/02_Get_Response_Headers/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/response-headers?foo=bar', 9 | 10 | /** 11 | * Exclude the body from the output 12 | */ 13 | CURLOPT_NOBODY => true, 14 | CURLOPT_RETURNTRANSFER => false, 15 | 16 | /** 17 | * Include the header in the output 18 | */ 19 | CURLOPT_HEADER => false, 20 | 21 | /** 22 | * Collect server response headers 23 | */ 24 | CURLOPT_HEADERFUNCTION => function ($curlInfo, $header) use (&$headers) { 25 | $headers[] = trim($header); 26 | 27 | return mb_strlen($header); 28 | }, 29 | ]); 30 | 31 | curl_exec($curlHandler); 32 | 33 | curl_close($curlHandler); 34 | 35 | print_r(array_filter($headers)); -------------------------------------------------------------------------------- /01_Basics/02_Headers/02_Get_Response_Headers/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get('https://postman-echo.com/response-headers?foo=bar'); 10 | 11 | print_r($response->getHeaders()); -------------------------------------------------------------------------------- /01_Basics/03_Request_Stats/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # See full list of options here https://ec.haxx.se/usingcurl-writeout.html 4 | 5 | curl --request GET "https://postman-echo.com/get?foo=bar" -w "size_download: %{size_download}" -o /dev/null -s -------------------------------------------------------------------------------- /01_Basics/03_Request_Stats/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/get?foo=bar', 7 | CURLOPT_RETURNTRANSFER => true, 8 | ]); 9 | 10 | curl_exec($curlHandler); 11 | 12 | $curlInfo = curl_getinfo($curlHandler); 13 | 14 | curl_close($curlHandler); 15 | 16 | print_r($curlInfo); -------------------------------------------------------------------------------- /01_Basics/03_Request_Stats/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 12 | 'https://postman-echo.com/get', 13 | [ 14 | RequestOptions::ON_STATS => function (TransferStats $stats) { 15 | print_r($stats->getHandlerStats()); 16 | } 17 | ] 18 | ); -------------------------------------------------------------------------------- /01_Basics/04_Debug_Request/01_Output_Debug_Info_To_Stderr/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --verbose --request GET "https://postman-echo.com/get?foo=bar" -------------------------------------------------------------------------------- /01_Basics/04_Debug_Request/01_Output_Debug_Info_To_Stderr/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/get?foo=bar', 7 | CURLOPT_RETURNTRANSFER => true, 8 | 9 | /** 10 | * Specify debug option 11 | */ 12 | CURLOPT_VERBOSE => true, 13 | ]); 14 | 15 | curl_exec($curlHandler); 16 | 17 | curl_close($curlHandler); -------------------------------------------------------------------------------- /01_Basics/04_Debug_Request/01_Output_Debug_Info_To_Stderr/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 11 | 'https://postman-echo.com/get?foo=bar', 12 | [ 13 | RequestOptions::DEBUG => true, 14 | ] 15 | ); -------------------------------------------------------------------------------- /01_Basics/04_Debug_Request/02_Output_Debug_Info_To_File/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request GET "https://postman-echo.com/get?foo=bar" --verbose --silent > debug.log 2>&1 -------------------------------------------------------------------------------- /01_Basics/04_Debug_Request/02_Output_Debug_Info_To_File/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/get?foo=bar', 7 | CURLOPT_RETURNTRANSFER => true, 8 | 9 | /** 10 | * Specify debug option. 11 | */ 12 | CURLOPT_VERBOSE => true, 13 | 14 | /** 15 | * Specify log file. 16 | * Make sure that the folder is writable. 17 | */ 18 | CURLOPT_STDERR => fopen('./curl.log', 'w+'), 19 | ]); 20 | 21 | curl_exec($curlHandler); 22 | 23 | curl_close($curlHandler); -------------------------------------------------------------------------------- /01_Basics/04_Debug_Request/02_Output_Debug_Info_To_File/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 11 | 'https://postman-echo.com/get?foo=bar', 12 | [ 13 | RequestOptions::DEBUG => fopen('./guzzle.log', 'w+'), 14 | ] 15 | ); -------------------------------------------------------------------------------- /01_Basics/05_Error_Catching/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --verbose --request GET "https://httpbin.org/delay/5" --max-time 3 --connect-timeout 2 -------------------------------------------------------------------------------- /01_Basics/05_Error_Catching/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/delay/5', 7 | CURLOPT_RETURNTRANSFER => true, 8 | CURLOPT_CONNECTTIMEOUT => 2, 9 | CURLOPT_TIMEOUT => 3, 10 | ]); 11 | 12 | $response = curl_exec($curlHandler); 13 | $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); 14 | 15 | if ($statusCode > 300) { 16 | print_r('Redirection or Error response with status: ' . $statusCode); 17 | } 18 | 19 | if (curl_errno($curlHandler) !== CURLE_OK) { 20 | print_r([ 21 | 'error_code' => curl_errno($curlHandler), 22 | 'error_message' => curl_error($curlHandler), 23 | ]); 24 | } 25 | 26 | curl_close($curlHandler); -------------------------------------------------------------------------------- /01_Basics/05_Error_Catching/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 13 | 'https://httpbin.org/dedlay/5', 14 | [ 15 | RequestOptions::CONNECT_TIMEOUT => 2, 16 | RequestOptions::TIMEOUT => 3, 17 | ] 18 | ); 19 | 20 | print_r($response->getBody()->getContents()); 21 | } catch (GuzzleException $exception) { 22 | print_r([ 23 | 'code' => $exception->getCode(), 24 | 'message' => $exception->getMessage(), 25 | ]); 26 | } -------------------------------------------------------------------------------- /01_Basics/06_Follow_Redirects/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --location --max-redirs 5 -X GET "https://httpbin.org/absolute-redirect/3" -------------------------------------------------------------------------------- /01_Basics/06_Follow_Redirects/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/absolute-redirect/3', 7 | CURLOPT_RETURNTRANSFER => true, 8 | CURLOPT_FOLLOWLOCATION => true, 9 | CURLOPT_MAXREDIRS => 5, 10 | ]); 11 | 12 | $content = curl_exec($curlHandler); 13 | $curlInfo = curl_getinfo($curlHandler); 14 | 15 | curl_close($curlHandler); 16 | 17 | print_r($curlInfo); -------------------------------------------------------------------------------- /01_Basics/06_Follow_Redirects/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 11 | 'https://httpbin.org/absolute-redirect/3', 12 | [ 13 | RequestOptions::ALLOW_REDIRECTS => [ 14 | 'max' => 5, 15 | ], 16 | ] 17 | ); 18 | 19 | echo $response->getStatusCode(); -------------------------------------------------------------------------------- /01_Basics/07_Timeouts/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request GET "https://httpbin.org/delay/5" --max-time 20 --connect-timeout 10 -------------------------------------------------------------------------------- /01_Basics/07_Timeouts/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/delay/5', 7 | CURLOPT_RETURNTRANSFER => true, 8 | CURLOPT_CONNECTTIMEOUT => 10, 9 | CURLOPT_TIMEOUT => 20, 10 | ]); 11 | 12 | $response = curl_exec($curlHandler); 13 | 14 | curl_close($curlHandler); 15 | 16 | print_r($response); -------------------------------------------------------------------------------- /01_Basics/07_Timeouts/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 11 | 'https://httpbin.org/delay/5', 12 | [ 13 | RequestOptions::CONNECT_TIMEOUT => 10, 14 | RequestOptions::TIMEOUT => 20, 15 | ] 16 | ); 17 | 18 | print_r($response->getBody()->getContents()); -------------------------------------------------------------------------------- /01_Basics/08_Set_Http_Version/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # See https://ec.haxx.se/http-versions.html 4 | 5 | curl --request GET "https://httpbin.org/get" --http2 -w "http_version: %{http_version}" -o /dev/null -s -------------------------------------------------------------------------------- /01_Basics/08_Set_Http_Version/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/get', 7 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0, 8 | ]); 9 | 10 | curl_exec($curlHandler); 11 | $info = curl_getinfo($curlHandler); 12 | curl_close($curlHandler); 13 | 14 | print_r($info); -------------------------------------------------------------------------------- /01_Basics/08_Set_Http_Version/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 12 | 'https://httpbin.org/get', 13 | [ 14 | RequestOptions::VERSION => 2.0, 15 | RequestOptions::ON_STATS => function (TransferStats $stats) { 16 | print_r($stats->getHandlerStats()); 17 | } 18 | ] 19 | ); -------------------------------------------------------------------------------- /01_Basics/09_Get_Curl_Version/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --version -------------------------------------------------------------------------------- /01_Basics/09_Get_Curl_Version/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/get', 7 | CURLOPT_USERAGENT => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3', 8 | 9 | /* OR set header 10 | CURLOPT_HTTPHEADER => [ 11 | 'User-Agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3', 12 | ]*/ 13 | ]); 14 | 15 | $response = curl_exec($curlHandler); 16 | 17 | curl_close($curlHandler); 18 | 19 | print_r($response); -------------------------------------------------------------------------------- /01_Basics/10_Set_User_Agent/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 11 | 'https://httpbin.org/get', 12 | [ 13 | RequestOptions::HEADERS => [ 14 | 'User-Agent' => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3', 15 | ] 16 | ] 17 | ); 18 | 19 | print_r($response->getBody()->getContents()); -------------------------------------------------------------------------------- /01_Basics/12_Redirect_Location_History/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --verbose --location --request GET "https://httpbin.org/absolute-redirect/5" 2>&1 | grep "Location:" -------------------------------------------------------------------------------- /01_Basics/12_Redirect_Location_History/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/absolute-redirect/5', 11 | CURLOPT_NOBODY => true, 12 | CURLOPT_FOLLOWLOCATION => true, 13 | CURLOPT_HEADERFUNCTION => function ($curlInfo, $header) use (&$locations) { 14 | preg_match('#Location:\s(?[\S]+)#iu', $header, $location); 15 | 16 | if (!empty($location['url'])) { 17 | $locations[] = trim($location['url']); 18 | } 19 | 20 | return mb_strlen($header); 21 | }, 22 | ] 23 | ); 24 | 25 | curl_exec($curlHandler); 26 | curl_close($curlHandler); 27 | 28 | print_r($locations); -------------------------------------------------------------------------------- /01_Basics/12_Redirect_Location_History/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 12 | 'https://httpbin.org/absolute-redirect/5', 13 | [ 14 | RequestOptions::ALLOW_REDIRECTS => [ 15 | 'max' => 5, 16 | 'track_redirects' => true, 17 | ], 18 | ] 19 | ); 20 | 21 | print_r($response->getHeader(RedirectMiddleware::HISTORY_HEADER)); -------------------------------------------------------------------------------- /01_Basics/13_Set_Http_Referer/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --request GET "https://httpbin.org/get" --referer "https://github.com" -------------------------------------------------------------------------------- /01_Basics/13_Set_Http_Referer/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/get', 7 | CURLOPT_RETURNTRANSFER => true, 8 | CURLOPT_REFERER => 'https://github.com', 9 | ]); 10 | 11 | $response = curl_exec($curlHandler); 12 | 13 | curl_close($curlHandler); 14 | 15 | print_r($response); -------------------------------------------------------------------------------- /01_Basics/13_Set_Http_Referer/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 11 | 'https://httpbin.org/get', 12 | [ 13 | RequestOptions::HEADERS => [ 14 | 'Referer' => 'https://github.com', 15 | ], 16 | ] 17 | ); 18 | 19 | print_r($response->getBody()->getContents()); -------------------------------------------------------------------------------- /02_Advanced/01_Files/01_Upload/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # format: curl --form '[request_field_name]=@[absolute_path_to_file]' [upload_url] 4 | curl --form 'file=@/home/serge/curl-examples/02_Advanced/01_Files/01_Upload/resource/file.txt' https://postman-echo.com/post -------------------------------------------------------------------------------- /02_Advanced/01_Files/01_Upload/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/post', 25 | CURLOPT_RETURNTRANSFER => true, 26 | 27 | /** 28 | * Specify POST method 29 | */ 30 | CURLOPT_POST => true, 31 | 32 | /** 33 | * Specify array of form fields 34 | */ 35 | CURLOPT_POSTFIELDS => [ 36 | $uploadFilePostKey => $uploadFile, 37 | ], 38 | ]); 39 | 40 | $response = curl_exec($curlHandler); 41 | 42 | curl_close($curlHandler); 43 | 44 | echo($response); -------------------------------------------------------------------------------- /02_Advanced/01_Files/01_Upload/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | post( 11 | 'https://postman-echo.com/post', 12 | [ 13 | RequestOptions::MULTIPART => [ 14 | [ 15 | 'name' => 'file', 16 | 'contents' => new SplFileObject(__DIR__ . '/resource/file.txt', 'r'), 17 | 'filename' => 'file.txt', 18 | ] 19 | ], 20 | ] 21 | ); 22 | 23 | echo($response->getBody()->getContents()); -------------------------------------------------------------------------------- /02_Advanced/01_Files/01_Upload/resource/file.txt: -------------------------------------------------------------------------------- 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque consequat ipsum sit amet tellus scelerisque, at ultrices orci maximus. Sed et nibh id orci maximus aliquet. Phasellus mattis, risus id commodo consectetur, enim tortor ullamcorper nulla, a auctor turpis est nec lectus. Suspendisse venenatis scelerisque ligula, vitae lobortis elit posuere id. Aenean lobortis eu massa nec auctor. Aliquam dictum luctus justo et malesuada. Aliquam erat volutpat. 2 | 3 | Nunc sit amet orci est. Nulla congue mauris et quam lacinia, in finibus dui ullamcorper. Maecenas quis velit sapien. Quisque eu quam odio. Ut aliquam mi a sem malesuada rutrum. Mauris semper, nibh sagittis maximus blandit, lectus mauris vulputate eros, sit amet varius ligula turpis ut turpis. Proin aliquam egestas tellus, id ultrices quam sodales at. Nulla a condimentum velit. Ut sagittis ac augue id dictum. Aenean sem metus, tincidunt non molestie in, efficitur sit amet erat. Morbi auctor nibh vitae sapien mollis, a laoreet diam porttitor. Cras ut nibh neque. Mauris consectetur vestibulum felis, nec maximus libero ultricies sed. Quisque non sodales arcu, ac scelerisque ipsum. Duis dignissim a dui vel placerat. Cras consequat pulvinar risus vitae ornare. 4 | 5 | Aenean viverra purus erat, sed condimentum quam congue ut. Morbi ut metus ac orci dapibus facilisis. Maecenas vel facilisis est. Maecenas ut ante non nisl vestibulum vehicula a nec purus. In ante urna, maximus quis dictum et, ultricies eu metus. Suspendisse eu pharetra justo. Aenean posuere vitae diam id facilisis. Proin a tincidunt urna. Donec ornare commodo velit. Aliquam condimentum, libero ac tincidunt viverra, eros arcu fringilla nisl, vel porttitor quam velit vel nunc. Nam eget congue massa, vitae lobortis dui. Etiam gravida, elit suscipit rhoncus placerat, ex nisi congue libero, nec volutpat nisl mauris nec felis. Quisque gravida quam non pellentesque convallis. Fusce blandit eget quam ac elementum. Fusce ut viverra mi, pulvinar blandit est. 6 | 7 | Praesent ut quam luctus, condimentum elit sed, tempor metus. Ut gravida ante nisi, vel accumsan augue congue vel. Aliquam eu commodo lorem. Vestibulum eros sapien, pellentesque vel pellentesque et, rutrum ut neque. Ut sodales convallis metus sit amet accumsan. Donec feugiat accumsan metus, ac pulvinar turpis dignissim sed. Mauris dictum felis orci, vel cursus enim efficitur non. In placerat eget diam ac gravida. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer dapibus tempor tortor eu cursus. Integer at pharetra augue. 8 | 9 | Sed ac ex auctor sem tristique fringilla. Nam vestibulum malesuada dolor, id aliquet enim finibus at. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec laoreet a lectus ut luctus. Sed quis egestas lectus, vel eleifend dui. Fusce sem tellus, finibus ac enim at, commodo suscipit ipsum. Mauris condimentum, tortor et varius pharetra, ex libero rhoncus dolor, vitae porttitor diam ligula non orci. Suspendisse eu arcu tincidunt, sollicitudin arcu ut, tincidunt justo. Nulla eleifend accumsan elit. Proin lobortis non eros eu dictum. Mauris fermentum purus vel arcu vehicula dictum. Curabitur vel nisi at sem porttitor accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. -------------------------------------------------------------------------------- /02_Advanced/01_Files/02_Upload_Multiple/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --form 'text_file=@/home/serge/curl-examples/02_Advanced/01_Files/02_Upload_Multiple/resource/file.txt' \ 4 | --form 'image_file=@/home/serge/curl-examples/02_Advanced/01_Files/02_Upload_Multiple/resource/github-icon.png' \ 5 | https://postman-echo.com/post -------------------------------------------------------------------------------- /02_Advanced/01_Files/02_Upload_Multiple/curl-ext.php: -------------------------------------------------------------------------------- 1 | __DIR__ . '/resource/file.txt', 5 | 'image_file' => __DIR__ . '/resource/github-icon.png', 6 | ]; 7 | 8 | $uploadFiles = []; 9 | 10 | foreach ($localFiles as $filePostKey => $uploadFilePath) { 11 | if (!file_exists($uploadFilePath)) { 12 | throw new Exception('File not found: ' . $uploadFilePath); 13 | } 14 | 15 | $uploadFileMimeType = mime_content_type($uploadFilePath); 16 | 17 | $uploadFiles[$filePostKey] = new CURLFile($uploadFilePath, $uploadFileMimeType, $filePostKey); 18 | } 19 | 20 | $curlHandler = curl_init(); 21 | 22 | curl_setopt_array($curlHandler, [ 23 | CURLOPT_URL => 'https://postman-echo.com/post', 24 | CURLOPT_RETURNTRANSFER => true, 25 | 26 | /** 27 | * Specify POST method 28 | */ 29 | CURLOPT_POST => true, 30 | 31 | /** 32 | * Specify array of form fields 33 | */ 34 | CURLOPT_POSTFIELDS => $uploadFiles, 35 | ]); 36 | 37 | $response = curl_exec($curlHandler); 38 | 39 | curl_close($curlHandler); 40 | 41 | echo($response); -------------------------------------------------------------------------------- /02_Advanced/01_Files/02_Upload_Multiple/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | __DIR__ . '/resource/file.txt', 10 | 'image_file' => __DIR__ . '/resource/github-icon.png', 11 | ]; 12 | 13 | $uploadFiles = []; 14 | 15 | foreach ($localFiles as $filePostKey => $uploadFilePath) { 16 | if (!file_exists($uploadFilePath)) { 17 | throw new Exception('File not found: ' . $uploadFilePath); 18 | } 19 | 20 | array_push($uploadFiles, [ 21 | 'name' => $filePostKey, 22 | 'contents' => new SplFileObject($uploadFilePath, 'r'), 23 | 'filename' => $filePostKey, 24 | ]); 25 | } 26 | 27 | $httpClient = new Client(); 28 | 29 | $response = $httpClient->post( 30 | 'https://postman-echo.com/post', 31 | [ 32 | RequestOptions::MULTIPART => $uploadFiles, 33 | ] 34 | ); 35 | 36 | echo($response->getBody()->getContents()); -------------------------------------------------------------------------------- /02_Advanced/01_Files/02_Upload_Multiple/resource/file.txt: -------------------------------------------------------------------------------- 1 | File content 2 | -------------------------------------------------------------------------------- /02_Advanced/01_Files/02_Upload_Multiple/resource/github-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriichuk/php-curl-cookbook/1bdbea06b090bb6711034968ef2259362046d51e/02_Advanced/01_Files/02_Upload_Multiple/resource/github-icon.png -------------------------------------------------------------------------------- /02_Advanced/01_Files/03_Upload_Array_Of_Files/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --form 'documents[]=@/home/serge/projects/php-curl-cookbook/02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/file-1.txt' \ 4 | --form 'documents[]=@/home/serge/projects/php-curl-cookbook/02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/file-2.txt' \ 5 | --form 'images[icon]=@/home/serge/projects/php-curl-cookbook/02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/github-icon.png' \ 6 | --form 'images[octocat]=@/home/serge/projects/php-curl-cookbook/02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/github-octocat.jpg' \ 7 | https://postman-echo.com/post 8 | -------------------------------------------------------------------------------- /02_Advanced/01_Files/03_Upload_Array_Of_Files/curl-ext.php: -------------------------------------------------------------------------------- 1 | [ 6 | __DIR__ . '/resource/file-1.txt', 7 | __DIR__ . '/resource/file-2.txt', 8 | ], 9 | 10 | // associated 11 | 'images' => [ 12 | 'icon' => __DIR__ . '/resource/github-icon.png', 13 | 'octocat' => __DIR__ . '/resource/github-octocat.jpg', 14 | ], 15 | ]; 16 | 17 | $uploadFiles = []; 18 | 19 | foreach ($localFiles as $fileType => $files) { 20 | foreach ($files as $filePostKey => $uploadFilePath) { 21 | if (!file_exists($uploadFilePath)) { 22 | throw new Exception('File not found: ' . $uploadFilePath); 23 | } 24 | 25 | $uploadFileMimeType = mime_content_type($uploadFilePath); 26 | $keyName = sprintf('%s[%s]', $fileType, $filePostKey); 27 | 28 | $uploadFiles[$keyName] = new CURLFile($uploadFilePath, $uploadFileMimeType, $filePostKey); 29 | } 30 | } 31 | 32 | $curlHandler = curl_init(); 33 | 34 | curl_setopt_array($curlHandler, [ 35 | CURLOPT_URL => 'https://postman-echo.com/post', 36 | CURLOPT_RETURNTRANSFER => true, 37 | 38 | /** 39 | * Specify POST method 40 | */ 41 | CURLOPT_POST => true, 42 | 43 | /** 44 | * Specify array of form fields 45 | */ 46 | CURLOPT_POSTFIELDS => $uploadFiles, 47 | ]); 48 | 49 | $response = curl_exec($curlHandler); 50 | 51 | curl_close($curlHandler); 52 | 53 | echo($response); 54 | -------------------------------------------------------------------------------- /02_Advanced/01_Files/03_Upload_Array_Of_Files/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | [ 10 | __DIR__ . '/resource/file-1.txt', 11 | __DIR__ . '/resource/file-2.txt', 12 | ], 13 | 'images' => [ 14 | 'icon' => __DIR__ . '/resource/github-icon.png', 15 | 'octocat' => __DIR__ . '/resource/github-octocat.jpg', 16 | ], 17 | ]; 18 | 19 | $uploadFiles = []; 20 | 21 | foreach ($localFiles as $fileType => $files) { 22 | foreach ($files as $filePostKey => $uploadFilePath) { 23 | if (!file_exists($uploadFilePath)) { 24 | throw new Exception('File not found: ' . $uploadFilePath); 25 | } 26 | 27 | $uploadFileMimeType = mime_content_type($uploadFilePath); 28 | $keyName = sprintf('%s[%s]', $fileType, $filePostKey); 29 | 30 | array_push($uploadFiles, [ 31 | 'name' => $keyName, 32 | 'contents' => new SplFileObject($uploadFilePath, 'r'), 33 | 'filename' => $keyName, 34 | ]); 35 | } 36 | } 37 | 38 | $httpClient = new Client(); 39 | 40 | $response = $httpClient->post( 41 | 'https://postman-echo.com/post', 42 | [ 43 | RequestOptions::MULTIPART => $uploadFiles, 44 | ] 45 | ); 46 | 47 | echo($response->getBody()->getContents()); 48 | -------------------------------------------------------------------------------- /02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/file-1.txt: -------------------------------------------------------------------------------- 1 | First File Content 2 | -------------------------------------------------------------------------------- /02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/file-2.txt: -------------------------------------------------------------------------------- 1 | Second File Content 2 | -------------------------------------------------------------------------------- /02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/github-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriichuk/php-curl-cookbook/1bdbea06b090bb6711034968ef2259362046d51e/02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/github-icon.png -------------------------------------------------------------------------------- /02_Advanced/01_Files/04_Download/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl https://httpbin.org/image/jpeg --output /home/serge/curl-examples/02_Advanced/01_Files/03_Download/resource/image.jpeg -------------------------------------------------------------------------------- /02_Advanced/01_Files/04_Download/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/image/jpeg', 9 | CURLOPT_FILE => fopen($imageFilePath, 'w+') 10 | ]); 11 | 12 | curl_exec($curlHandler); 13 | 14 | if (curl_errno($curlHandler) === CURLE_OK) { 15 | echo 'The image has been successfully downloaded: ' . $imageFilePath; 16 | } 17 | 18 | curl_close($curlHandler); 19 | -------------------------------------------------------------------------------- /02_Advanced/01_Files/04_Download/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 13 | 'https://httpbin.org/image/jpeg', 14 | [ 15 | RequestOptions::SINK => $imageFileResource, 16 | ] 17 | ); 18 | 19 | if ($response->getStatusCode() === 200) { 20 | echo 'The image has been successfully downloaded: ' . $imageFilePath; 21 | } 22 | -------------------------------------------------------------------------------- /02_Advanced/01_Files/04_Download/resource/image.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriichuk/php-curl-cookbook/1bdbea06b090bb6711034968ef2259362046d51e/02_Advanced/01_Files/04_Download/resource/image.jpeg -------------------------------------------------------------------------------- /02_Advanced/02_Auth/01_Basic_Auth/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --user postman:password --request GET "https://postman-echo.com/basic-auth" -------------------------------------------------------------------------------- /02_Advanced/02_Auth/01_Basic_Auth/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/basic-auth', 10 | CURLOPT_RETURNTRANSFER => true, 11 | 12 | CURLOPT_HTTPAUTH => CURLAUTH_BASIC, 13 | CURLOPT_USERPWD => $userName . ':' . $password, 14 | ]); 15 | 16 | $response = curl_exec($curlHandler); 17 | curl_close($curlHandler); 18 | 19 | print_r('First example response: ' . $response . PHP_EOL); 20 | 21 | /** 22 | * Or specify credentials in Authorization header 23 | */ 24 | 25 | $curlSecondHandler = curl_init(); 26 | 27 | curl_setopt_array($curlSecondHandler, [ 28 | CURLOPT_URL => 'https://postman-echo.com/basic-auth', 29 | CURLOPT_RETURNTRANSFER => true, 30 | 31 | CURLOPT_HTTPHEADER => [ 32 | 'Authorization: Basic ' . base64_encode($userName . ':' . $password) 33 | ], 34 | ]); 35 | 36 | $response = curl_exec($curlSecondHandler); 37 | curl_close($curlSecondHandler); 38 | 39 | print_r('Second example response: ' . $response . PHP_EOL); -------------------------------------------------------------------------------- /02_Advanced/02_Auth/01_Basic_Auth/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 14 | 'https://postman-echo.com/basic-auth', 15 | [ 16 | RequestOptions::AUTH => [$userName, $password] 17 | ] 18 | ); 19 | 20 | print_r($response->getBody()->getContents()); -------------------------------------------------------------------------------- /02_Advanced/02_Auth/02_Digest_Auth/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --digest --user postman:password --request GET "https://postman-echo.com/digest-auth" -------------------------------------------------------------------------------- /02_Advanced/02_Auth/02_Digest_Auth/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://postman-echo.com/digest-auth', 10 | CURLOPT_RETURNTRANSFER => true, 11 | 12 | CURLOPT_HTTPAUTH => CURLAUTH_DIGEST, 13 | CURLOPT_USERPWD => $userName . ':' . $password, 14 | ]); 15 | 16 | $response = curl_exec($curlHandler); 17 | curl_close($curlHandler); 18 | 19 | print_r($response); -------------------------------------------------------------------------------- /02_Advanced/02_Auth/02_Digest_Auth/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 14 | 'https://postman-echo.com/digest-auth', 15 | [ 16 | RequestOptions::AUTH => [$userName, $password, 'digest'] 17 | ] 18 | ); 19 | 20 | print_r($response->getBody()->getContents()); -------------------------------------------------------------------------------- /02_Advanced/02_Auth/03_Bearer_Auth/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl -X GET "https://httpbin.org/bearer" -H "Accept: application/json" -H "Authorization: Bearer your_token" -------------------------------------------------------------------------------- /02_Advanced/02_Auth/03_Bearer_Auth/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/bearer', 9 | CURLOPT_RETURNTRANSFER => true, 10 | CURLOPT_HTTPHEADER => [ 11 | 'Accept: application/json', 12 | 'Authorization: Bearer ' . $token 13 | ], 14 | ]); 15 | 16 | $response = curl_exec($curlHandler); 17 | curl_close($curlHandler); 18 | 19 | print_r($response); -------------------------------------------------------------------------------- /02_Advanced/02_Auth/03_Bearer_Auth/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 13 | 'https://httpbin.org/bearer', 14 | [ 15 | RequestOptions::HEADERS => [ 16 | 'Accept' => 'application/json', 17 | 'Authorization' => 'Bearer ' . $token, 18 | ] 19 | ] 20 | ); 21 | 22 | print_r($response->getBody()->getContents()); -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/01_Send_Cookies_From_String/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --cookie "foo=bar;baz=foo" --request GET "https://httpbin.org/cookies" -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/01_Send_Cookies_From_String/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/cookies', 7 | CURLOPT_RETURNTRANSFER => true, 8 | 9 | CURLOPT_COOKIE => 'foo=bar;baz=foo', 10 | 11 | /** 12 | * Or set header 13 | * CURLOPT_HTTPHEADER => [ 14 | 'Cookie: foo=bar;baz=foo', 15 | ] 16 | */ 17 | ]); 18 | 19 | $response = curl_exec($curlHandler); 20 | curl_close($curlHandler); 21 | 22 | echo $response; 23 | -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/01_Send_Cookies_From_String/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | 'bar', 17 | 'baz' => 'foo', 18 | ], 19 | $urlHost 20 | ); 21 | 22 | $response = $httpClient->get( 23 | $url, 24 | [ 25 | RequestOptions::COOKIES => $cookieJar, 26 | ] 27 | ); 28 | 29 | echo($response->getBody()->getContents()); -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/02_Set_Cookie_Options/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | 'foo', 18 | 'Value' => 'bar', 19 | 'Domain' => $urlHost, 20 | 'Path' => '/', 21 | 'Max-Age' => 100, 22 | 'Secure' => false, 23 | 'Discard' => false, 24 | 'HttpOnly' => false, 25 | ]), 26 | new SetCookie([ 27 | 'Name' => 'baz', 28 | 'Value' => 'foo', 29 | 'Domain' => $urlHost, 30 | 'Path' => '/', 31 | 'Max-Age' => 100, 32 | 'Secure' => false, 33 | 'Discard' => false, 34 | 'HttpOnly' => false, 35 | ]), 36 | ]; 37 | 38 | $cookieJar = new CookieJar(true, $cookies); 39 | 40 | $response = $httpClient->get( 41 | $url, 42 | [ 43 | RequestOptions::COOKIES => $cookieJar, 44 | ] 45 | ); 46 | 47 | echo($response->getBody()->getContents()); -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/03_Send_Cookies_From_File/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --cookie "02_Advanced/03_Cookies/03_Send_Cookies_From_File/resource/cookie-jar.txt" --request GET "https://httpbin.org/cookies" -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/03_Send_Cookies_From_File/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/cookies', 9 | CURLOPT_RETURNTRANSFER => true, 10 | 11 | CURLOPT_COOKIEFILE => $cookieFile, 12 | ]); 13 | 14 | $response = curl_exec($curlHandler); 15 | curl_close($curlHandler); 16 | 17 | echo $response; 18 | -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/03_Send_Cookies_From_File/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 17 | 'https://httpbin.org/cookies', 18 | [ 19 | RequestOptions::COOKIES => $cookieJarFile, 20 | ] 21 | ); 22 | 23 | echo $response->getBody()->getContents(); -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/03_Send_Cookies_From_File/resource/cookie-jar.txt: -------------------------------------------------------------------------------- 1 | # Domain Flag Path Secure Expiration Name Value 2 | httpbin.org FALSE / FALSE 0 foo bar 3 | httpbin.org FALSE / FALSE 0 baz foo 4 | -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/03_Send_Cookies_From_File/resource/guzzle-cookie-jar.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "Name": "foo", 4 | "Value": "bar", 5 | "Domain": "httpbin.org", 6 | "Path": "\/", 7 | "Max-Age": 100, 8 | "Expires": 1567343671, 9 | "Secure": false, 10 | "Discard": false, 11 | "HttpOnly": false 12 | }, 13 | { 14 | "Name": "baz", 15 | "Value": "foo", 16 | "Domain": "httpbin.org", 17 | "Path": "\/", 18 | "Max-Age": 100, 19 | "Expires": 1567343671, 20 | "Secure": false, 21 | "Discard": false, 22 | "HttpOnly": false 23 | } 24 | ] -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/console.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | curl --location --cookie-jar "02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/resource/cookie-jar.txt" --request GET "https://httpbin.org/cookies/set/foo/bar" -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/curl-ext.php: -------------------------------------------------------------------------------- 1 | 'https://httpbin.org/cookies/set/foo/bar', 9 | CURLOPT_RETURNTRANSFER => true, 10 | 11 | CURLOPT_COOKIEJAR => $cookieFile, 12 | CURLOPT_FOLLOWLOCATION => true, 13 | ]); 14 | 15 | $response = curl_exec($curlHandler); 16 | curl_close($curlHandler); 17 | 18 | echo $response; 19 | -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/guzzle-lib.php: -------------------------------------------------------------------------------- 1 | get( 17 | 'https://httpbin.org/cookies/set/foo/bar', 18 | [ 19 | RequestOptions::COOKIES => $cookieJarFile, 20 | ] 21 | ); 22 | 23 | echo $response->getBody()->getContents(); -------------------------------------------------------------------------------- /02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/resource/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at andriichuk29@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Serge Andriichuk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP CURL Cookbook 2 | 3 | [![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://vshymanskyy.github.io/StandWithUkraine/) 4 | 5 | List of commonly used cases with PHP cURL extension. 6 | 7 | CURL library [Home Page](https://curl.haxx.se/) and [Wiki Page](https://ec.haxx.se/). 8 | 9 | PHP [Extension Page](http://docs.php.net/manual/en/book.curl.php) and [List Of Options](http://docs.php.net/manual/en/function.curl-setopt.php). 10 | 11 | PHP [Guzzle](http://docs.guzzlephp.org/en/stable/overview.html) library - wrapper over PHP CURL extension. 12 | 13 | For testing requests we will use the excellent services [httpbin.org](https://httpbin.org/) and [Postman Echo](https://docs.postman-echo.com). 14 | 15 | # Table of Contents 16 | 17 | * [Requirements](#requirements) 18 | * [Installation](#installation) 19 | * [Basics](#basics) 20 | * [HTTP Request methods](#http-request-methods) 21 | * [GET method](#get-method) 22 | * [POST raw request](#post-raw-request) 23 | * [POST form data](#post-form-data) 24 | * [PUT method](#put-method) 25 | * [PATCH method](#patch-method) 26 | * [DELETE method](#delete-method) 27 | * [Headers](#headers) 28 | * [Send custom request headers](#send-custom-request-headers) 29 | * [Get response headers](#get-response-headers) 30 | * [Request stats](#request-stats) 31 | * [Debug request](#debug-request) 32 | * [Output debug info to STDERR](#output-debug-info-to-stderr) 33 | * [Output debug info to file](#output-debug-info-to-file) 34 | * [Error catching](#error-catching) 35 | * [Follow redirects](#follow-redirects) 36 | * [Timeouts](#timeouts) 37 | * [Set HTTP version](#set-http-version) 38 | * [Get cURL version](#get-curl-version) 39 | * [Set User agent](#set-user-agent) 40 | * [Redirect Location History](#redirect-location-history) 41 | * [Set HTTP Referer](#set-http-referer) 42 | * [Advanced](#advanced) 43 | * [Files](#files) 44 | * [Upload file](#upload-file) 45 | * [Upload multiple files](#upload-multiple-files) 46 | * [Upload multiple files as an array](#upload-multiple-files-as-an-array) 47 | * [Download file](#download-file) 48 | * [Auth](#auth) 49 | * [Basic Auth](#basic-auth) 50 | * [Digest Auth](#digest-auth) 51 | * [Bearer Auth](#bearer-auth) 52 | * [Cookies](#cookies) 53 | * [Send cookies from string](#send-cookies-from-string) 54 | * [Set cookie options](#set-cookie-options) 55 | * [Send cookies from file](#send-cookies-from-file) 56 | * [Save Response cookies to file](#save-response-cookies-to-file) 57 | * [Todo](#todo) 58 | 59 | ## Requirements 60 | 61 | * PHP >= 5.5 62 | * cURL PHP Extension 63 | * Mbstring PHP Extension 64 | * Fileinfo PHP Extension 65 | 66 | ## Installation 67 | 68 | Download repository 69 | 70 | ```bash 71 | git clone https://github.com/andriichuk/php-curl-cookbook.git 72 | ``` 73 | 74 | Go to the directory 75 | 76 | ```bash 77 | cd ./php-curl-cookbook 78 | ``` 79 | 80 | Install composer dependencies 81 | 82 | ```bash 83 | bash run composer install 84 | ``` 85 | 86 | Run BASH example 87 | 88 | ```bash 89 | bash run bash 01_Basics/01_Request_Methods/01_Get/console.sh 90 | ``` 91 | Run PHP example 92 | 93 | ```bash 94 | bash run php 01_Basics/01_Request_Methods/01_Get/curl-ext.php 95 | ``` 96 | 97 | For multiple usage 98 | 99 | ```bash 100 | bash console 101 | 102 | bash 01_Basics/01_Request_Methods/01_Get/console.sh 103 | php 01_Basics/01_Request_Methods/01_Get/curl-ext.php 104 | ``` 105 | 106 | # Basics 107 | 108 | ## HTTP Request methods 109 | 110 | ### GET method 111 | 112 | #### Bash 113 | 114 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/01_Get/console.sh)] 115 | 116 | ```bash 117 | curl --request GET "https://postman-echo.com/get?foo=bar" 118 | ``` 119 | 120 | #### PHP CURL extension 121 | 122 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/01_Get/curl-ext.php)] 123 | 124 | The default method is GET. If you want to specify it directly than add `CURLOPT_CUSTOMREQUEST` option. 125 | 126 | ```php 127 | $curlHandler = curl_init(); 128 | 129 | curl_setopt_array($curlHandler, [ 130 | CURLOPT_URL => 'https://postman-echo.com/get?foo=bar', 131 | CURLOPT_RETURNTRANSFER => true, 132 | 133 | /** 134 | * Or specify directly 135 | * CURLOPT_CUSTOMREQUEST => 'GET' 136 | */ 137 | ]); 138 | 139 | $response = curl_exec($curlHandler); 140 | curl_close($curlHandler); 141 | 142 | echo($response); 143 | ``` 144 | 145 | #### PHP Guzzle library 146 | 147 | Call `get` method of Guzzle Client instance. 148 | 149 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/01_Get/guzzle-lib.php)] 150 | 151 | ```php 152 | use GuzzleHttp\Client; 153 | use GuzzleHttp\RequestOptions; 154 | 155 | $httpClient = new Client(); 156 | 157 | $response = $httpClient->get( 158 | 'https://postman-echo.com/get', 159 | [ 160 | RequestOptions::QUERY => [ 161 | 'foo' => 'bar', 162 | ], 163 | ] 164 | ); 165 | 166 | echo($response->getBody()->getContents()); 167 | ``` 168 | 169 |
Response 170 |

171 | 172 | ```json 173 | {"args":{"foo":"bar"},"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","user-agent":"GuzzleHttp/6.3.3 curl/7.64.0 PHP/7.3.5-1+ubuntu19.04.1+deb.sury.org+1","x-forwarded-port":"443"},"url":"https://postman-echo.com/get?foo=bar"} 174 | ``` 175 | 176 |

177 |
178 | 179 | ### POST raw request 180 | 181 | #### Bash 182 | 183 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/02_Post_Raw_Data/console.sh)] 184 | 185 | ```bash 186 | curl --request POST "https://postman-echo.com/post" --data "POST raw request content" 187 | ``` 188 | 189 | #### PHP CURL extension 190 | 191 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/02_Post_Raw_Data/curl-ext.php)] 192 | 193 | ```php 194 | $curlHandler = curl_init(); 195 | 196 | curl_setopt_array($curlHandler, [ 197 | CURLOPT_URL => 'https://postman-echo.com/post', 198 | CURLOPT_RETURNTRANSFER => true, 199 | 200 | /** 201 | * Specify POST method 202 | */ 203 | CURLOPT_POST => true, 204 | 205 | /** 206 | * Specify request content 207 | */ 208 | CURLOPT_POSTFIELDS => 'POST raw request content', 209 | ]); 210 | 211 | $response = curl_exec($curlHandler); 212 | 213 | curl_close($curlHandler); 214 | 215 | echo($response); 216 | ``` 217 | 218 | #### PHP Guzzle library 219 | 220 | Call `post` method of Guzzle Client instance. 221 | 222 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/02_Post_Raw_Data/guzzle-lib.php)] 223 | 224 | ```php 225 | 226 | use GuzzleHttp\Client; 227 | use GuzzleHttp\RequestOptions; 228 | 229 | $httpClient = new Client(); 230 | 231 | $response = $httpClient->post( 232 | 'https://postman-echo.com/post', 233 | [ 234 | RequestOptions::BODY => 'POST raw request content', 235 | RequestOptions::HEADERS => [ 236 | 'Content-Type' => 'application/x-www-form-urlencoded', 237 | ], 238 | ] 239 | ); 240 | 241 | echo($response->getBody()->getContents()); 242 | ``` 243 | 244 |
Response 245 |

246 | 247 | ```json 248 | {"args":{},"data":"","files":{},"form":{"POST raw request content":""},"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content-length":"24","content-type":"application/x-www-form-urlencoded","user-agent":"GuzzleHttp/6.3.3 curl/7.64.0 PHP/7.3.5-1+ubuntu19.04.1+deb.sury.org+1","x-forwarded-port":"443"},"json":{"POST raw request content":""},"url":"https://postman-echo.com/post"} 249 | ``` 250 | 251 |

252 |
253 | 254 | ### POST form data 255 | 256 | #### Bash 257 | 258 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/03_Post_Form_Data/console.sh)] 259 | 260 | ```bash 261 | curl --request POST "https://postman-echo.com/post" --data "foo=bar&baz=biz" 262 | ``` 263 | 264 | #### PHP CURL extension 265 | 266 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/03_Post_Form_Data/curl-ext.php)] 267 | 268 | ```php 269 | $curlHandler = curl_init(); 270 | 271 | curl_setopt_array($curlHandler, [ 272 | CURLOPT_URL => 'https://postman-echo.com/post', 273 | CURLOPT_RETURNTRANSFER => true, 274 | 275 | /** 276 | * Specify POST method 277 | */ 278 | CURLOPT_POST => true, 279 | 280 | /** 281 | * Specify array of form fields 282 | */ 283 | CURLOPT_POSTFIELDS => [ 284 | 'foo' => 'bar', 285 | 'baz' => 'biz', 286 | ], 287 | ]); 288 | 289 | $response = curl_exec($curlHandler); 290 | 291 | curl_close($curlHandler); 292 | 293 | echo($response); 294 | ``` 295 | 296 | #### PHP Guzzle library 297 | 298 | Call `post` method of Guzzle Client instance. 299 | 300 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/03_Post_Form_Data/guzzle-lib.php)] 301 | 302 | ```php 303 | use GuzzleHttp\Client; 304 | use GuzzleHttp\RequestOptions; 305 | 306 | $httpClient = new Client(); 307 | 308 | $response = $httpClient->post( 309 | 'https://postman-echo.com/post', 310 | [ 311 | RequestOptions::FORM_PARAMS => [ 312 | 'foo' => 'bar', 313 | 'baz' => 'biz', 314 | ], 315 | ] 316 | ); 317 | 318 | echo($response->getBody()->getContents()); 319 | ``` 320 | 321 |
Response 322 |

323 | 324 | ```json 325 | {"args":{},"data":"","files":{},"form":{"foo":"bar","baz":"biz"},"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content-length":"15","content-type":"application/x-www-form-urlencoded","user-agent":"GuzzleHttp/6.3.3 curl/7.64.0 PHP/7.3.5-1+ubuntu19.04.1+deb.sury.org+1","x-forwarded-port":"443"},"json":{"foo":"bar","baz":"biz"},"url":"https://postman-echo.com/post"} 326 | ``` 327 | 328 |

329 |
330 | 331 | ### PUT method 332 | 333 | #### Bash 334 | 335 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/04_Put/console.sh)] 336 | 337 | ```bash 338 | curl --request PUT "https://postman-echo.com/put" --data "foo=bar&baz=biz" 339 | ``` 340 | 341 | #### PHP CURL extension 342 | 343 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/04_Put/curl-ext.php)] 344 | 345 | ```php 346 | $curlHandler = curl_init(); 347 | 348 | curl_setopt_array($curlHandler, [ 349 | CURLOPT_URL => 'https://postman-echo.com/put', 350 | CURLOPT_RETURNTRANSFER => true, 351 | 352 | /** 353 | * Specify custom HTTP request method 354 | */ 355 | CURLOPT_CUSTOMREQUEST => 'PUT', 356 | 357 | /** 358 | * Specify request body (can be array or string) 359 | */ 360 | CURLOPT_POSTFIELDS => [ 361 | 'foo' => 'bar', 362 | 'baz' => 'biz', 363 | ] 364 | ]); 365 | 366 | $pageContent = curl_exec($curlHandler); 367 | 368 | curl_close($curlHandler); 369 | 370 | echo($pageContent); 371 | ``` 372 | 373 | #### PHP Guzzle library 374 | 375 | Call `put` method of Guzzle Client instance. 376 | 377 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/01_Request_Methods/04_Put/guzzle-lib.php)] 378 | 379 | ```php 380 | use GuzzleHttp\Client; 381 | use GuzzleHttp\RequestOptions; 382 | 383 | $httpClient = new Client(); 384 | 385 | $response = $httpClient->put( 386 | 'https://postman-echo.com/put', 387 | [ 388 | RequestOptions::FORM_PARAMS => [ 389 | 'foo' => 'bar', 390 | 'baz' => 'biz', 391 | ], 392 | ] 393 | ); 394 | 395 | echo($response->getBody()->getContents()); 396 | ``` 397 | 398 |
Response 399 |

400 | 401 | ```json 402 | {"args":{},"data":"","files":{},"form":{"foo":"bar","baz":"biz"},"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content-length":"15","content-type":"application/x-www-form-urlencoded","user-agent":"GuzzleHttp/6.3.3 curl/7.64.0 PHP/7.3.5-1+ubuntu19.04.1+deb.sury.org+1","x-forwarded-port":"443"},"json":{"foo":"bar","baz":"biz"},"url":"https://postman-echo.com/put"} 403 | ``` 404 | 405 |

406 |
407 | 408 | ### PATCH method 409 | 410 | #### Bash 411 | 412 | ```bash 413 | curl --request PATCH "https://postman-echo.com/patch" --data "foo=bar&baz=biz" 414 | ``` 415 | 416 | #### PHP CURL extension 417 | 418 | ```php 419 | $curlHandler = curl_init(); 420 | 421 | curl_setopt_array($curlHandler, [ 422 | CURLOPT_URL => 'https://postman-echo.com/patch', 423 | CURLOPT_RETURNTRANSFER => true, 424 | 425 | /** 426 | * Specify custom HTTP request method 427 | */ 428 | CURLOPT_CUSTOMREQUEST => 'PATCH', 429 | 430 | /** 431 | * Specify request body (can be array or string) 432 | */ 433 | CURLOPT_POSTFIELDS => [ 434 | 'foo' => 'bar', 435 | 'baz' => 'biz', 436 | ] 437 | ]); 438 | 439 | $pageContent = curl_exec($curlHandler); 440 | 441 | curl_close($curlHandler); 442 | 443 | echo($pageContent); 444 | ``` 445 | 446 | #### PHP Guzzle library 447 | 448 | Call `patch` method of Guzzle Client instance. 449 | 450 | ```php 451 | use GuzzleHttp\Client; 452 | use GuzzleHttp\RequestOptions; 453 | 454 | $httpClient = new Client(); 455 | 456 | $response = $httpClient->patch( 457 | 'https://postman-echo.com/patch', 458 | [ 459 | RequestOptions::FORM_PARAMS => [ 460 | 'foo' => 'bar', 461 | 'baz' => 'biz', 462 | ], 463 | ] 464 | ); 465 | 466 | echo($response->getBody()->getContents()); 467 | ``` 468 | 469 |
Response 470 |

471 | 472 | ```json 473 | {"args":{},"data":"","files":{},"form":{"foo":"bar","baz":"biz"},"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content-length":"15","accept":"*/*","content-type":"application/x-www-form-urlencoded","user-agent":"curl/7.64.0","x-forwarded-port":"443"},"json":{"foo":"bar","baz":"biz"},"url":"https://postman-echo.com/patch"} 474 | ``` 475 | 476 |

477 |
478 | 479 | ### DELETE method 480 | 481 | #### Bash 482 | 483 | ```bash 484 | curl --request DELETE "https://postman-echo.com/delete" --data "foo=bar&baz=biz" --max-time 10 485 | ``` 486 | 487 | #### PHP CURL extension 488 | 489 | ```php 490 | $curlHandler = curl_init(); 491 | 492 | curl_setopt_array($curlHandler, [ 493 | CURLOPT_URL => 'https://postman-echo.com/delete', 494 | CURLOPT_RETURNTRANSFER => true, 495 | 496 | /** 497 | * Specify custom HTTP request method 498 | */ 499 | CURLOPT_CUSTOMREQUEST => 'DELETE', 500 | 501 | /** 502 | * Specify request body (can be array or string) 503 | */ 504 | CURLOPT_POSTFIELDS => [ 505 | 'foo' => 'bar', 506 | 'baz' => 'biz', 507 | ] 508 | ]); 509 | 510 | $pageContent = curl_exec($curlHandler); 511 | 512 | curl_close($curlHandler); 513 | 514 | echo($pageContent); 515 | ``` 516 | 517 | #### PHP Guzzle library 518 | 519 | Call `delete` method of Guzzle Client instance. 520 | 521 | ```php 522 | use GuzzleHttp\Client; 523 | use GuzzleHttp\RequestOptions; 524 | 525 | $httpClient = new Client(); 526 | 527 | $response = $httpClient->delete( 528 | 'https://postman-echo.com/delete', 529 | [ 530 | RequestOptions::FORM_PARAMS => [ 531 | 'foo' => 'bar', 532 | 'baz' => 'biz', 533 | ], 534 | ] 535 | ); 536 | 537 | echo($response->getBody()->getContents()); 538 | ``` 539 | 540 |
Response 541 |

542 | 543 | ```json 544 | {"args":{},"data":"","files":{},"form":{"foo":"bar","baz":"biz"},"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content-length":"15","content-type":"application/x-www-form-urlencoded","user-agent":"GuzzleHttp/6.3.3 curl/7.64.0 PHP/7.3.5-1+ubuntu19.04.1+deb.sury.org+1","x-forwarded-port":"443"},"json":{"foo":"bar","baz":"biz"},"url":"https://postman-echo.com/delete"} 545 | ``` 546 | 547 |

548 |
549 | 550 | ## Headers 551 | 552 | ### Send custom request headers 553 | 554 | #### Bash 555 | 556 | ```bash 557 | curl --request GET "https://postman-echo.com/headers" --header "foo: bar" --header "baz: biz" 558 | ``` 559 | 560 | #### PHP CURL extension 561 | 562 | ```php 563 | $curlHandler = curl_init(); 564 | 565 | curl_setopt_array($curlHandler, [ 566 | CURLOPT_URL => 'https://postman-echo.com/headers', 567 | CURLOPT_RETURNTRANSFER => true, 568 | 569 | /** 570 | * Specify request headers 571 | */ 572 | CURLOPT_HTTPHEADER => [ 573 | 'foo: bar', 574 | 'baz: biz', 575 | ] 576 | ]); 577 | 578 | $pageContent = curl_exec($curlHandler); 579 | 580 | curl_close($curlHandler); 581 | 582 | echo($pageContent); 583 | ``` 584 | 585 | #### PHP Guzzle library 586 | 587 | ```php 588 | use GuzzleHttp\Client; 589 | use GuzzleHttp\RequestOptions; 590 | 591 | $httpClient = new Client(); 592 | 593 | $response = $httpClient->get( 594 | 'https://postman-echo.com/headers', 595 | [ 596 | RequestOptions::HEADERS => [ 597 | 'foo' => 'bar', 598 | 'baz' => 'biz', 599 | ], 600 | ] 601 | ); 602 | 603 | print_r( 604 | $response->getBody()->getContents() 605 | ); 606 | ``` 607 | 608 |
Response 609 |

610 | 611 | ```json 612 | {"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","baz":"biz","foo":"bar","user-agent":"GuzzleHttp/6.3.3 curl/7.64.0 PHP/7.3.5-1+ubuntu19.04.1+deb.sury.org+1","x-forwarded-port":"443"}} 613 | ``` 614 | 615 |

616 |
617 | 618 | ### Get response headers 619 | 620 | #### Bash 621 | 622 | ```bash 623 | curl --request GET "https://postman-echo.com/response-headers?Content-Type=text/html&foo=bar" 624 | ``` 625 | 626 | #### PHP CURL extension 627 | 628 | ```php 629 | $headers = []; 630 | 631 | $curlHandler = curl_init(); 632 | 633 | curl_setopt_array($curlHandler, [ 634 | CURLOPT_URL => 'https://postman-echo.com/response-headers?foo=bar', 635 | 636 | /** 637 | * Exclude the body from the output 638 | */ 639 | CURLOPT_NOBODY => true, 640 | CURLOPT_RETURNTRANSFER => false, 641 | 642 | /** 643 | * Include the header in the output 644 | */ 645 | CURLOPT_HEADER => false, 646 | 647 | /** 648 | * Collect server response headers 649 | */ 650 | CURLOPT_HEADERFUNCTION => function ($curlInfo, $header) use (&$headers) { 651 | array_push($headers, trim($header)); 652 | 653 | return mb_strlen($header); 654 | }, 655 | ]); 656 | 657 | curl_exec($curlHandler); 658 | 659 | curl_close($curlHandler); 660 | 661 | print_r( 662 | array_filter($headers) 663 | ); 664 | ``` 665 | 666 | #### PHP Guzzle library 667 | 668 | ```php 669 | use GuzzleHttp\Client; 670 | 671 | $httpClient = new Client(); 672 | 673 | $response = $httpClient->get('https://postman-echo.com/response-headers?foo=bar'); 674 | 675 | print_r( 676 | $response->getHeaders() 677 | ); 678 | ``` 679 | 680 |
Response 681 |

682 | 683 | ```plain 684 | Array 685 | ( 686 | [Content-Type] => Array 687 | ( 688 | [0] => application/json; charset=utf-8 689 | ) 690 | 691 | [Date] => Array 692 | ( 693 | [0] => Sun, 19 May 2019 14:16:36 GMT 694 | ) 695 | // ... 696 | ``` 697 | 698 |

699 |
700 | 701 | ## Request stats 702 | 703 | #### Bash 704 | 705 | See full list of options [here](https://ec.haxx.se/usingcurl-writeout.html) 706 | 707 | ```bash 708 | curl --request GET "https://postman-echo.com/get?foo=bar" -w "size_download: %{size_download}" -o /dev/null -s 709 | ``` 710 | 711 | #### PHP CURL extension 712 | 713 | ```php 714 | $curlHandler = curl_init(); 715 | 716 | curl_setopt_array($curlHandler, [ 717 | CURLOPT_URL => 'https://postman-echo.com/get?foo=bar', 718 | CURLOPT_RETURNTRANSFER => true, 719 | ]); 720 | 721 | curl_exec($curlHandler); 722 | 723 | $curlInfo = curl_getinfo($curlHandler); 724 | 725 | curl_close($curlHandler); 726 | 727 | print_r($curlInfo); 728 | ``` 729 | 730 | #### PHP Guzzle library 731 | 732 | ```php 733 | use GuzzleHttp\Client; 734 | use GuzzleHttp\RequestOptions; 735 | use GuzzleHttp\TransferStats; 736 | 737 | $httpClient = new Client(); 738 | 739 | $httpClient->get( 740 | 'https://postman-echo.com/get', 741 | [ 742 | RequestOptions::ON_STATS => function (TransferStats $stats) { 743 | print_r($stats->getHandlerStats()); 744 | } 745 | ] 746 | ); 747 | ``` 748 | 749 |
Response 750 |

751 | 752 | ```plain 753 | Array 754 | ( 755 | [url] => https://postman-echo.com/get 756 | [content_type] => application/json; charset=utf-8 757 | [http_code] => 200 758 | [header_size] => 354 759 | [request_size] => 128 760 | [filetime] => -1 761 | [ssl_verify_result] => 0 762 | [redirect_count] => 0 763 | // ... 764 | ``` 765 | 766 |

767 |
768 | 769 | ## Debug request 770 | 771 | ### Output debug info to STDERR 772 | 773 | #### Bash 774 | 775 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/04_Debug_Request/01_Output_Debug_Info_To_Stderr/console.sh)] 776 | 777 | ```bash 778 | curl --verbose --request GET "https://postman-echo.com/get?foo=bar" 779 | ``` 780 | 781 | #### PHP CURL extension 782 | 783 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/04_Debug_Request/01_Output_Debug_Info_To_Stderr/curl-ext.php)] 784 | 785 | ```php 786 | $curlHandler = curl_init(); 787 | 788 | curl_setopt_array($curlHandler, [ 789 | CURLOPT_URL => 'https://postman-echo.com/get?foo=bar', 790 | CURLOPT_RETURNTRANSFER => true, 791 | 792 | /** 793 | * Specify debug option 794 | */ 795 | CURLOPT_VERBOSE => true, 796 | ]); 797 | 798 | curl_exec($curlHandler); 799 | 800 | curl_close($curlHandler); 801 | ``` 802 | 803 | #### PHP Guzzle library 804 | 805 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/04_Debug_Request/01_Output_Debug_Info_To_Stderr/guzzle-lib.php)] 806 | 807 | ```php 808 | use GuzzleHttp\Client; 809 | use GuzzleHttp\RequestOptions; 810 | 811 | $httpClient = new Client(); 812 | 813 | $httpClient->get( 814 | 'https://postman-echo.com/get?foo=bar', 815 | [ 816 | RequestOptions::DEBUG => true, 817 | ] 818 | ); 819 | ``` 820 | 821 |
Response 822 |

823 | 824 | ```plain 825 | * Trying 35.153.115.14... 826 | * TCP_NODELAY set 827 | * Expire in 149999 ms for 3 (transfer 0x55b754f97120) 828 | * Expire in 200 ms for 4 (transfer 0x55b754f97120) 829 | * Connected to postman-echo.com (35.153.115.14) port 443 (#0) 830 | // ... 831 | ``` 832 | 833 |

834 |
835 | 836 | ### Output debug info to file 837 | 838 | #### Bash 839 | 840 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/04_Debug_Request/02_Output_Debug_Info_To_File/console.sh)] 841 | 842 | ```bash 843 | curl --request GET "https://postman-echo.com/get?foo=bar" --verbose --silent > debug.log 2>&1 844 | ``` 845 | 846 | #### PHP CURL extension 847 | 848 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/04_Debug_Request/02_Output_Debug_Info_To_File/curl-ext.php)] 849 | 850 | ```php 851 | $curlHandler = curl_init(); 852 | 853 | curl_setopt_array($curlHandler, [ 854 | CURLOPT_URL => 'https://postman-echo.com/get?foo=bar', 855 | CURLOPT_RETURNTRANSFER => true, 856 | 857 | /** 858 | * Specify debug option. 859 | */ 860 | CURLOPT_VERBOSE => true, 861 | 862 | /** 863 | * Specify log file. 864 | * Make sure that the folder is writable. 865 | */ 866 | CURLOPT_STDERR => fopen('./curl.log', 'w+'), 867 | ]); 868 | 869 | curl_exec($curlHandler); 870 | 871 | curl_close($curlHandler); 872 | ``` 873 | 874 | #### PHP Guzzle library 875 | 876 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/04_Debug_Request/02_Output_Debug_Info_To_File/guzzle-lib.php)] 877 | 878 | ```php 879 | use GuzzleHttp\Client; 880 | use GuzzleHttp\RequestOptions; 881 | 882 | $httpClient = new Client(); 883 | 884 | $httpClient->get( 885 | 'https://postman-echo.com/get?foo=bar', 886 | [ 887 | RequestOptions::DEBUG => fopen('./guzzle.log', 'w+'), 888 | ] 889 | ); 890 | ``` 891 | 892 | #### Log file content 893 | 894 | ```plain 895 | * Trying 35.153.115.14... 896 | * TCP_NODELAY set 897 | * Expire in 149999 ms for 3 (transfer 0x55b754f97120) 898 | * Expire in 200 ms for 4 (transfer 0x55b754f97120) 899 | * Connected to postman-echo.com (35.153.115.14) port 443 (#0) 900 | // ... 901 | ``` 902 | 903 | ## Error catching 904 | 905 | #### Bash 906 | 907 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/05_Error_Catching/console.sh)] 908 | 909 | ```bash 910 | curl --verbose --request GET "https://httpbin.org/delay/5" --max-time 3 --connect-timeout 2 911 | ``` 912 | 913 | #### PHP CURL extension 914 | 915 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/05_Error_Catching/curl-ext.php)] 916 | 917 | ```php 918 | $curlHandler = curl_init(); 919 | 920 | curl_setopt_array($curlHandler, [ 921 | CURLOPT_URL => 'https://httpbin.org/delay/5', 922 | CURLOPT_RETURNTRANSFER => true, 923 | CURLOPT_CONNECTTIMEOUT => 2, 924 | CURLOPT_TIMEOUT => 3, 925 | ]); 926 | 927 | $response = curl_exec($curlHandler); 928 | $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); 929 | 930 | if ($statusCode > 300) { 931 | print_r('Redirection or Error response with status: ' . $statusCode); 932 | } 933 | 934 | if (curl_errno($curlHandler) !== CURLE_OK) { 935 | print_r([ 936 | 'error_code' => curl_errno($curlHandler), 937 | 'error_message' => curl_error($curlHandler), 938 | ]); 939 | } 940 | 941 | curl_close($curlHandler); 942 | ``` 943 | 944 | #### PHP Guzzle library 945 | 946 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/05_Error_Catching/guzzle-lib.php)] 947 | 948 | ```php 949 | use GuzzleHttp\Client; 950 | use GuzzleHttp\RequestOptions; 951 | use GuzzleHttp\Exception\GuzzleException; 952 | 953 | $httpClient = new Client(); 954 | 955 | try { 956 | $response = $httpClient->get( 957 | 'https://httpbin.org/dedlay/5', 958 | [ 959 | RequestOptions::CONNECT_TIMEOUT => 2, 960 | RequestOptions::TIMEOUT => 3, 961 | ] 962 | ); 963 | 964 | print_r($response->getBody()->getContents()); 965 | } catch (GuzzleException $exception) { 966 | print_r([ 967 | 'code' => $exception->getCode(), 968 | 'message' => $exception->getMessage(), 969 | ]); 970 | } 971 | ``` 972 | 973 |
Response 974 |

975 | 976 | ```plain 977 | Array 978 | ( 979 | [error_code] => 28 980 | [error_message] => Operation timed out after 3001 milliseconds with 0 bytes received 981 | ) 982 | ``` 983 | 984 |

985 |
986 | 987 | ## Follow redirects 988 | 989 | #### Bash 990 | 991 | ```bash 992 | curl --location --max-redirs 5 -X GET "https://httpbin.org/absolute-redirect/3" 993 | ``` 994 | 995 | #### PHP CURL extension 996 | 997 | ```php 998 | $curlHandler = curl_init(); 999 | 1000 | curl_setopt_array($curlHandler, [ 1001 | CURLOPT_URL => 'https://httpbin.org/absolute-redirect/3', 1002 | CURLOPT_RETURNTRANSFER => true, 1003 | CURLOPT_FOLLOWLOCATION => true, 1004 | CURLOPT_MAXREDIRS => 5, 1005 | ]); 1006 | 1007 | $content = curl_exec($curlHandler); 1008 | $curlInfo = curl_getinfo($curlHandler); 1009 | 1010 | curl_close($curlHandler); 1011 | 1012 | print_r($curlInfo); 1013 | ``` 1014 | 1015 | #### PHP Guzzle library 1016 | 1017 | ```php 1018 | use GuzzleHttp\Client; 1019 | use GuzzleHttp\RequestOptions; 1020 | use GuzzleHttp\TransferStats; 1021 | 1022 | $httpClient = new Client(); 1023 | 1024 | $response = $httpClient->get( 1025 | 'https://httpbin.org/absolute-redirect/3', 1026 | [ 1027 | RequestOptions::ALLOW_REDIRECTS => [ 1028 | 'max' => 5, 1029 | ], 1030 | ] 1031 | ); 1032 | 1033 | echo $response->getStatusCode(); 1034 | ``` 1035 | 1036 |
Response 1037 |

1038 | 1039 | ```json 1040 | { 1041 | "args": {}, 1042 | "headers": { 1043 | "Accept": "*/*", 1044 | "Host": "httpbin.org", 1045 | "User-Agent": "curl/7.64.0" 1046 | }, 1047 | "url": "https://httpbin.org/get" 1048 | } 1049 | ``` 1050 | 1051 |

1052 |
1053 | 1054 | ## Timeouts 1055 | 1056 | #### Bash 1057 | 1058 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/06_Timeouts/console.sh)] 1059 | 1060 | ```bash 1061 | curl --request GET "https://httpbin.org/delay/5" --max-time 20 --connect-timeout 10 1062 | ``` 1063 | 1064 | #### PHP CURL extension 1065 | 1066 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/06_Timeouts/curl-ext.php)] 1067 | 1068 | ```php 1069 | $curlHandler = curl_init(); 1070 | 1071 | curl_setopt_array($curlHandler, [ 1072 | CURLOPT_URL => 'https://httpbin.org/delay/5', 1073 | CURLOPT_RETURNTRANSFER => true, 1074 | CURLOPT_CONNECTTIMEOUT => 10, 1075 | CURLOPT_TIMEOUT => 20, 1076 | ]); 1077 | 1078 | $response = curl_exec($curlHandler); 1079 | 1080 | curl_close($curlHandler); 1081 | 1082 | print_r($response); 1083 | ``` 1084 | 1085 | #### PHP Guzzle library 1086 | 1087 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/06_Timeouts/guzzle-lib.php)] 1088 | 1089 | ```php 1090 | use GuzzleHttp\Client; 1091 | use GuzzleHttp\RequestOptions; 1092 | 1093 | $httpClient = new Client(); 1094 | 1095 | $response = $httpClient->get( 1096 | 'https://httpbin.org/delay/5', 1097 | [ 1098 | RequestOptions::CONNECT_TIMEOUT => 10, 1099 | RequestOptions::TIMEOUT => 20, 1100 | ] 1101 | ); 1102 | 1103 | print_r($response->getBody()->getContents()); 1104 | ``` 1105 | 1106 | ### Set HTTP version 1107 | 1108 | #### Bash 1109 | 1110 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/08_Set_Http_Version/console.sh)] 1111 | 1112 | ```bash 1113 | # See https://ec.haxx.se/http-versions.html 1114 | 1115 | curl --request GET "https://httpbin.org/get" --http2 1116 | ``` 1117 | 1118 | #### PHP CURL extension 1119 | 1120 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/08_Set_Http_Version/curl-ext.php)] 1121 | 1122 | ```php 1123 | $curlHandler = curl_init(); 1124 | 1125 | curl_setopt_array($curlHandler, [ 1126 | CURLOPT_URL => 'https://httpbin.org/get', 1127 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0, 1128 | ]); 1129 | 1130 | curl_exec($curlHandler); 1131 | $info = curl_getinfo($curlHandler); 1132 | curl_close($curlHandler); 1133 | 1134 | print_r($info); 1135 | ``` 1136 | 1137 | #### PHP Guzzle library 1138 | 1139 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/08_Set_Http_Version/guzzle-lib.php)] 1140 | 1141 | ```php 1142 | use GuzzleHttp\Client; 1143 | use GuzzleHttp\RequestOptions; 1144 | use GuzzleHttp\TransferStats; 1145 | 1146 | $httpClient = new Client(); 1147 | 1148 | $response = $httpClient->get( 1149 | 'https://httpbin.org/get', 1150 | [ 1151 | RequestOptions::VERSION => 2.0, 1152 | RequestOptions::ON_STATS => function (TransferStats $stats) { 1153 | print_r($stats->getHandlerStats()); 1154 | } 1155 | ] 1156 | ); 1157 | ``` 1158 | 1159 |
Response 1160 |

1161 | 1162 | ```plain 1163 | Array 1164 | ( 1165 | [url] => https://httpbin.org/get 1166 | ... 1167 | [http_version] => 2 1168 | [protocol] => 2 1169 | ... 1170 | ) 1171 | ``` 1172 | 1173 |

1174 |
1175 | 1176 | ### Get cURL version 1177 | 1178 | #### Bash 1179 | 1180 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/09_Get_Curl_Version/console.sh)] 1181 | 1182 | ```bash 1183 | curl --version 1184 | ``` 1185 | 1186 | #### PHP CURL extension and Guzzle library 1187 | 1188 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/09_Get_Curl_Version/curl-ext.php)] 1189 | 1190 | ```php 1191 | print_r(curl_version()); 1192 | ``` 1193 | 1194 |
Response 1195 |

1196 | 1197 | ```plain 1198 | curl 7.64.0 (x86_64-pc-linux-gnu) libcurl/7.64.0 OpenSSL/1.1.1c zlib/1.2.11 libidn2/2.0.5 libpsl/0.20.2 (+libidn2/2.0.5) libssh/0.8.6/openssl/zlib nghttp2/1.36.0 librtmp/2.3 1199 | Release-Date: 2019-02-06 1200 | Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp 1201 | Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL 1202 | ``` 1203 | 1204 |

1205 |
1206 | 1207 | ### Set User agent 1208 | 1209 | #### Bash 1210 | 1211 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/10_Set_User_Agent/console.sh)] 1212 | 1213 | ```bash 1214 | curl --request GET "https://httpbin.org/get" --user-agent 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3' 1215 | ``` 1216 | 1217 | #### PHP CURL extension 1218 | 1219 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/10_Set_User_Agent/curl-ext.php)] 1220 | 1221 | ```php 1222 | $curlHandler = curl_init(); 1223 | 1224 | curl_setopt_array($curlHandler, [ 1225 | CURLOPT_URL => 'https://postman-echo.com/get', 1226 | CURLOPT_USERAGENT => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3', 1227 | 1228 | /* OR set header 1229 | CURLOPT_HTTPHEADER => [ 1230 | 'User-Agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3', 1231 | ]*/ 1232 | ]); 1233 | 1234 | $response = curl_exec($curlHandler); 1235 | 1236 | curl_close($curlHandler); 1237 | 1238 | print_r($response); 1239 | ``` 1240 | 1241 | #### PHP Guzzle library 1242 | 1243 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/10_Set_User_Agent/guzzle-lib.php)] 1244 | 1245 | ```php 1246 | use GuzzleHttp\Client; 1247 | use GuzzleHttp\RequestOptions; 1248 | 1249 | $httpClient = new Client(); 1250 | 1251 | $response = $httpClient->get( 1252 | 'https://httpbin.org/get', 1253 | [ 1254 | RequestOptions::HEADERS => [ 1255 | 'User-Agent' => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3', 1256 | ] 1257 | ] 1258 | ); 1259 | 1260 | print_r($response->getBody()->getContents()); 1261 | ``` 1262 | 1263 | ### Redirect Location History 1264 | 1265 | #### Bash 1266 | 1267 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/12_Redirect_Location_History/console.sh)] 1268 | 1269 | ```bash 1270 | curl --verbose --location --request GET "https://httpbin.org/absolute-redirect/5" 2>&1 | grep "Location:" 1271 | ``` 1272 | 1273 |
Response 1274 |

1275 | 1276 | ```plain 1277 | < Location: http://httpbin.org/absolute-redirect/4 1278 | < Location: http://httpbin.org/absolute-redirect/3 1279 | < Location: http://httpbin.org/absolute-redirect/2 1280 | < Location: http://httpbin.org/absolute-redirect/1 1281 | < Location: http://httpbin.org/get 1282 | ``` 1283 | 1284 |

1285 |
1286 | 1287 | #### PHP CURL extension 1288 | 1289 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/12_Redirect_Location_History/curl-ext.php)] 1290 | 1291 | ```php 1292 | $curlHandler = curl_init(); 1293 | 1294 | $locations = []; 1295 | 1296 | curl_setopt_array( 1297 | $curlHandler, 1298 | [ 1299 | CURLOPT_URL => 'https://httpbin.org/absolute-redirect/5', 1300 | CURLOPT_NOBODY => true, 1301 | CURLOPT_FOLLOWLOCATION => true, 1302 | CURLOPT_HEADERFUNCTION => function ($curlInfo, $header) use (&$locations) { 1303 | preg_match('#Location:\s(?[\S]+)#iu', $header, $location); 1304 | 1305 | if (!empty($location['url'])) { 1306 | $locations[] = trim($location['url']); 1307 | } 1308 | 1309 | return mb_strlen($header); 1310 | }, 1311 | ] 1312 | ); 1313 | 1314 | curl_exec($curlHandler); 1315 | curl_close($curlHandler); 1316 | 1317 | print_r($locations); 1318 | ``` 1319 | 1320 |
Response 1321 |

1322 | 1323 | ```plain 1324 | Array 1325 | ( 1326 | [0] => http://httpbin.org/absolute-redirect/4 1327 | [1] => http://httpbin.org/absolute-redirect/3 1328 | [2] => http://httpbin.org/absolute-redirect/2 1329 | [3] => http://httpbin.org/absolute-redirect/1 1330 | [4] => http://httpbin.org/get 1331 | ) 1332 | ``` 1333 | 1334 |

1335 |
1336 | 1337 | #### PHP Guzzle library 1338 | 1339 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/12_Redirect_Location_History/guzzle-lib.php)] 1340 | 1341 | ```php 1342 | use GuzzleHttp\Client; 1343 | use GuzzleHttp\RequestOptions; 1344 | use GuzzleHttp\RedirectMiddleware; 1345 | 1346 | $httpClient = new Client(); 1347 | 1348 | $response = $httpClient->get( 1349 | 'https://httpbin.org/absolute-redirect/5', 1350 | [ 1351 | RequestOptions::ALLOW_REDIRECTS => [ 1352 | 'max' => 5, 1353 | 'track_redirects' => true, 1354 | ], 1355 | ] 1356 | ); 1357 | 1358 | print_r($response->getHeader(RedirectMiddleware::HISTORY_HEADER)); 1359 | ``` 1360 | 1361 |
Response 1362 |

1363 | 1364 | ```plain 1365 | Array 1366 | ( 1367 | [0] => http://httpbin.org/absolute-redirect/4 1368 | [1] => http://httpbin.org/absolute-redirect/3 1369 | [2] => http://httpbin.org/absolute-redirect/2 1370 | [3] => http://httpbin.org/absolute-redirect/1 1371 | [4] => http://httpbin.org/get 1372 | ) 1373 | ``` 1374 |

1375 |
1376 | 1377 | 1378 | ### Set HTTP Referer 1379 | 1380 | #### Bash 1381 | 1382 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/13_Set_Http_Referer/console.sh)] 1383 | 1384 | ```bash 1385 | curl --request GET "https://httpbin.org/get" --referer "https://github.com" 1386 | ``` 1387 | 1388 | #### PHP CURL extension 1389 | 1390 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/13_Set_Http_Referer/curl-ext.php)] 1391 | 1392 | ```php 1393 | $curlHandler = curl_init(); 1394 | 1395 | curl_setopt_array($curlHandler, [ 1396 | CURLOPT_URL => 'https://httpbin.org/get', 1397 | CURLOPT_RETURNTRANSFER => true, 1398 | CURLOPT_REFERER => 'https://github.com', 1399 | ]); 1400 | 1401 | $response = curl_exec($curlHandler); 1402 | 1403 | curl_close($curlHandler); 1404 | 1405 | print_r($response); 1406 | ``` 1407 | 1408 | #### PHP Guzzle library 1409 | 1410 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/01_Basics/13_Set_Http_Referer/curl-ext.php)] 1411 | 1412 | ```php 1413 | use GuzzleHttp\Client; 1414 | use GuzzleHttp\RequestOptions; 1415 | 1416 | $httpClient = new Client(); 1417 | 1418 | $response = $httpClient->get( 1419 | 'https://httpbin.org/get', 1420 | [ 1421 | RequestOptions::HEADERS => [ 1422 | 'Referer' => 'https://github.com', 1423 | ], 1424 | ] 1425 | ); 1426 | 1427 | print_r($response->getBody()->getContents()); 1428 | ``` 1429 | 1430 |
Response 1431 |

1432 | 1433 | ```plain 1434 | { 1435 | "args": {}, 1436 | "headers": { 1437 | "Accept": "*/*", 1438 | "Host": "httpbin.org", 1439 | "Referer": "https://github.com", 1440 | "User-Agent": "curl/7.64.0" 1441 | }, 1442 | "origin": "100.00.00.00", 1443 | "url": "https://httpbin.org/get" 1444 | } 1445 | ``` 1446 |

1447 |
1448 | 1449 | # Advanced 1450 | 1451 | ## Files 1452 | 1453 | ### Upload file 1454 | 1455 | #### Bash 1456 | 1457 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/01_Upload/console.sh)] 1458 | 1459 | ```bash 1460 | # format: curl --form '[request_field_name]=@[absolute_path_to_file]' [upload_url] 1461 | curl --form 'file=@/home/serge/curl-examples/02_Advanced/01_Files/01_Upload/resource/file.txt' https://postman-echo.com/post 1462 | ``` 1463 | 1464 | #### PHP CURL extension 1465 | 1466 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/01_Upload/curl-ext.php)] 1467 | 1468 | ```php 1469 | $uploadFilePath = __DIR__ . '/resource/file.txt'; 1470 | 1471 | if (!file_exists($uploadFilePath)) { 1472 | throw new Exception('File not found: ' . $uploadFilePath); 1473 | } 1474 | 1475 | $uploadFileMimeType = mime_content_type($uploadFilePath); 1476 | $uploadFilePostKey = 'file'; 1477 | 1478 | $uploadFile = new CURLFile( 1479 | $uploadFilePath, 1480 | $uploadFileMimeType, 1481 | $uploadFilePostKey 1482 | ); 1483 | 1484 | $curlHandler = curl_init(); 1485 | 1486 | curl_setopt_array($curlHandler, [ 1487 | CURLOPT_URL => 'https://postman-echo.com/post', 1488 | CURLOPT_RETURNTRANSFER => true, 1489 | 1490 | /** 1491 | * Specify POST method 1492 | */ 1493 | CURLOPT_POST => true, 1494 | 1495 | /** 1496 | * Specify array of form fields 1497 | */ 1498 | CURLOPT_POSTFIELDS => [ 1499 | $uploadFilePostKey => $uploadFile, 1500 | ], 1501 | ]); 1502 | 1503 | $response = curl_exec($curlHandler); 1504 | 1505 | curl_close($curlHandler); 1506 | 1507 | echo($response); 1508 | ``` 1509 | 1510 | #### PHP Guzzle library 1511 | 1512 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/01_Upload/guzzle-lib.php)] 1513 | 1514 | ```php 1515 | use GuzzleHttp\Client; 1516 | use GuzzleHttp\RequestOptions; 1517 | 1518 | $httpClient = new Client(); 1519 | 1520 | $response = $httpClient->post( 1521 | 'https://postman-echo.com/post', 1522 | [ 1523 | RequestOptions::MULTIPART => [ 1524 | [ 1525 | 'name' => 'file', 1526 | 'contents' => new SplFileObject(__DIR__ . '/resource/file.txt', 'r'), 1527 | 'filename' => 'file', 1528 | ] 1529 | ], 1530 | ] 1531 | ); 1532 | 1533 | echo($response->getBody()->getContents()); 1534 | ``` 1535 | 1536 |
Response 1537 |

1538 | 1539 | ```json 1540 | {"args":{},"data":{},"files":{"file":"data:application/octet-stream;base64,TG9yZW0gaXBzdW0gZG9sb3Igc2l0I ..."}} 1541 | ``` 1542 | 1543 |

1544 |
1545 | 1546 | ### Upload multiple files 1547 | 1548 | #### Bash 1549 | 1550 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/02_Upload_Multiple/console.sh)] 1551 | 1552 | ```bash 1553 | curl --form 'text_file=@/home/serge/curl-examples/02_Advanced/01_Files/02_Upload_Multiple/resource/file.txt' \ 1554 | --form 'image_file=@/home/serge/curl-examples/02_Advanced/01_Files/02_Upload_Multiple/resource/github-icon.png' \ 1555 | https://postman-echo.com/post 1556 | ``` 1557 | 1558 | #### PHP CURL extension 1559 | 1560 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/02_Upload_Multiple/curl-ext.php)] 1561 | 1562 | ```php 1563 | $localFiles = [ 1564 | 'text_file' => __DIR__ . '/resource/file.txt', 1565 | 'image_file' => __DIR__ . '/resource/github-icon.png', 1566 | ]; 1567 | 1568 | $uploadFiles = []; 1569 | 1570 | foreach ($localFiles as $filePostKey => $uploadFilePath) { 1571 | if (!file_exists($uploadFilePath)) { 1572 | throw new Exception('File not found: ' . $uploadFilePath); 1573 | } 1574 | 1575 | $uploadFileMimeType = mime_content_type($uploadFilePath); 1576 | 1577 | $uploadFiles[$filePostKey] = new CURLFile($uploadFilePath, $uploadFileMimeType, $filePostKey); 1578 | } 1579 | 1580 | $curlHandler = curl_init(); 1581 | 1582 | curl_setopt_array($curlHandler, [ 1583 | CURLOPT_URL => 'https://postman-echo.com/post', 1584 | CURLOPT_RETURNTRANSFER => true, 1585 | 1586 | /** 1587 | * Specify POST method 1588 | */ 1589 | CURLOPT_POST => true, 1590 | 1591 | /** 1592 | * Specify array of form fields 1593 | */ 1594 | CURLOPT_POSTFIELDS => $uploadFiles, 1595 | ]); 1596 | 1597 | $response = curl_exec($curlHandler); 1598 | 1599 | curl_close($curlHandler); 1600 | 1601 | echo($response); 1602 | ``` 1603 | 1604 | #### PHP Guzzle library 1605 | 1606 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/02_Upload_Multiple/guzzle-lib.php)] 1607 | 1608 | ```php 1609 | use GuzzleHttp\Client; 1610 | use GuzzleHttp\RequestOptions; 1611 | 1612 | $localFiles = [ 1613 | 'text_file' => __DIR__ . '/resource/file.txt', 1614 | 'image_file' => __DIR__ . '/resource/github-icon.png', 1615 | ]; 1616 | 1617 | $uploadFiles = []; 1618 | 1619 | foreach ($localFiles as $filePostKey => $uploadFilePath) { 1620 | if (!file_exists($uploadFilePath)) { 1621 | throw new Exception('File not found: ' . $uploadFilePath); 1622 | } 1623 | 1624 | array_push($uploadFiles, [ 1625 | 'name' => $filePostKey, 1626 | 'contents' => new SplFileObject($uploadFilePath, 'r'), 1627 | 'filename' => $filePostKey, 1628 | ]); 1629 | } 1630 | 1631 | $httpClient = new Client(); 1632 | 1633 | $response = $httpClient->post( 1634 | 'https://postman-echo.com/post', 1635 | [ 1636 | RequestOptions::MULTIPART => $uploadFiles, 1637 | ] 1638 | ); 1639 | 1640 | echo($response->getBody()->getContents()); 1641 | ``` 1642 | 1643 | ### Response example 1644 | 1645 | ```json 1646 | {"args":{},"data":{},"files":{"text_file":"data:application/octet-stream;base64,TG9yZW0gaXBzdW0gZG9sb3Ig ...", "image_file":"data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUgAAANAAAADQC ..."}} 1647 | ``` 1648 | 1649 | ### Upload multiple files as an array 1650 | 1651 | #### Bash 1652 | 1653 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/03_Upload_Array_Of_Files/console.sh)] 1654 | 1655 | ```bash 1656 | curl --form 'documents[]=@/home/serge/projects/php-curl-cookbook/02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/file-1.txt' \ 1657 | --form 'documents[]=@/home/serge/projects/php-curl-cookbook/02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/file-2.txt' \ 1658 | --form 'images[icon]=@/home/serge/projects/php-curl-cookbook/02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/github-icon.png' \ 1659 | --form 'images[octocat]=@/home/serge/projects/php-curl-cookbook/02_Advanced/01_Files/03_Upload_Array_Of_Files/resource/github-octocat.jpg' \ 1660 | https://postman-echo.com/post 1661 | ``` 1662 | 1663 | #### PHP CURL extension 1664 | 1665 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/03_Upload_Array_Of_Files/curl-ext.php)] 1666 | 1667 | ```php 1668 | $localFiles = [ 1669 | // indexed 1670 | 'documents' => [ 1671 | __DIR__ . '/resource/file-1.txt', 1672 | __DIR__ . '/resource/file-2.txt', 1673 | ], 1674 | 1675 | // associated 1676 | 'images' => [ 1677 | 'icon' => __DIR__ . '/resource/github-icon.png', 1678 | 'octocat' => __DIR__ . '/resource/github-octocat.jpg', 1679 | ], 1680 | ]; 1681 | 1682 | $uploadFiles = []; 1683 | 1684 | foreach ($localFiles as $fileType => $files) { 1685 | foreach ($files as $filePostKey => $uploadFilePath) { 1686 | if (!file_exists($uploadFilePath)) { 1687 | throw new Exception('File not found: ' . $uploadFilePath); 1688 | } 1689 | 1690 | $uploadFileMimeType = mime_content_type($uploadFilePath); 1691 | $keyName = sprintf('%s[%s]', $fileType, $filePostKey); 1692 | 1693 | $uploadFiles[$keyName] = new CURLFile($uploadFilePath, $uploadFileMimeType, $filePostKey); 1694 | } 1695 | } 1696 | 1697 | $curlHandler = curl_init(); 1698 | 1699 | curl_setopt_array($curlHandler, [ 1700 | CURLOPT_URL => 'https://postman-echo.com/post', 1701 | CURLOPT_RETURNTRANSFER => true, 1702 | 1703 | /** 1704 | * Specify POST method 1705 | */ 1706 | CURLOPT_POST => true, 1707 | 1708 | /** 1709 | * Specify array of form fields 1710 | */ 1711 | CURLOPT_POSTFIELDS => $uploadFiles, 1712 | ]); 1713 | 1714 | $response = curl_exec($curlHandler); 1715 | 1716 | curl_close($curlHandler); 1717 | 1718 | echo($response); 1719 | ``` 1720 | 1721 | #### PHP Guzzle library 1722 | 1723 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/03_Upload_Array_Of_Files/guzzle-lib.php)] 1724 | 1725 | ```php 1726 | use GuzzleHttp\Client; 1727 | use GuzzleHttp\RequestOptions; 1728 | 1729 | $localFiles = [ 1730 | 'documents' => [ 1731 | __DIR__ . '/resource/file-1.txt', 1732 | __DIR__ . '/resource/file-2.txt', 1733 | ], 1734 | 'images' => [ 1735 | 'icon' => __DIR__ . '/resource/github-icon.png', 1736 | 'octocat' => __DIR__ . '/resource/github-octocat.jpg', 1737 | ], 1738 | ]; 1739 | 1740 | $uploadFiles = []; 1741 | 1742 | foreach ($localFiles as $fileType => $files) { 1743 | foreach ($files as $filePostKey => $uploadFilePath) { 1744 | if (!file_exists($uploadFilePath)) { 1745 | throw new Exception('File not found: ' . $uploadFilePath); 1746 | } 1747 | 1748 | $uploadFileMimeType = mime_content_type($uploadFilePath); 1749 | $keyName = sprintf('%s[%s]', $fileType, $filePostKey); 1750 | 1751 | array_push($uploadFiles, [ 1752 | 'name' => $keyName, 1753 | 'contents' => new SplFileObject($uploadFilePath, 'r'), 1754 | 'filename' => $keyName, 1755 | ]); 1756 | } 1757 | } 1758 | 1759 | $httpClient = new Client(); 1760 | 1761 | $response = $httpClient->post( 1762 | 'https://postman-echo.com/post', 1763 | [ 1764 | RequestOptions::MULTIPART => $uploadFiles, 1765 | ] 1766 | ); 1767 | 1768 | echo($response->getBody()->getContents()); 1769 | ``` 1770 | 1771 | ### Response example 1772 | 1773 | ```json 1774 | {"args":{},"data":{},"files":{"file-1.txt":"data:application/octet-stream;base64,Rmlyc3QgRmlsZSBDb250ZW50Cg==","file-2.txt":"data:application/octet-stream;base64,U2V...=","github-icon.png":"data:application/octet-stream;base64,iVBORw...","github-octocat.jpg":"data:application/octet-stream;base64,/9j/4QAYRXhpZgAASUkqAAg..."},"form":{},"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content-length":"59650","accept":"*/*","content-type":"multipart/form-data; boundary=------------------------14a7692feb592877","user-agent":"curl/7.58.0","x-forwarded-port":"443"},"json":null,"url":"https://postman-echo.com/post"} 1775 | ``` 1776 | 1777 | ### Download file 1778 | 1779 | #### Bash 1780 | 1781 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/04_Download/console.sh)] 1782 | 1783 | ```bash 1784 | curl https://httpbin.org/image/jpeg --output /home/serge/curl-examples/02_Advanced/01_Files/04_Download/resource/image.jpeg 1785 | ``` 1786 | 1787 | #### PHP CURL extension 1788 | 1789 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/04_Download/curl-ext.php)] 1790 | 1791 | ```php 1792 | $imageFilePath = __DIR__ . '/resource/image.jpeg'; 1793 | 1794 | $curlHandler = curl_init(); 1795 | 1796 | curl_setopt_array($curlHandler, [ 1797 | CURLOPT_URL => 'https://httpbin.org/image/jpeg', 1798 | CURLOPT_FILE => fopen($imageFilePath, 'w+') 1799 | ]); 1800 | 1801 | curl_exec($curlHandler); 1802 | 1803 | if (curl_errno($curlHandler) === CURLE_OK) { 1804 | echo 'The image has been successfully downloaded: ' . $imageFilePath; 1805 | } 1806 | 1807 | curl_close($curlHandler); 1808 | ``` 1809 | 1810 | #### PHP Guzzle library 1811 | 1812 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/01_Files/04_Download/guzzle-lib.php)] 1813 | 1814 | ```php 1815 | use GuzzleHttp\Client; 1816 | use GuzzleHttp\RequestOptions; 1817 | 1818 | $imageFilePath = __DIR__ . '/resource/image.jpeg'; 1819 | $imageFileResource = fopen($imageFilePath, 'w+'); 1820 | 1821 | $httpClient = new Client(); 1822 | $response = $httpClient->get( 1823 | 'https://httpbin.org/image/jpeg', 1824 | [ 1825 | RequestOptions::SINK => $imageFileResource, 1826 | ] 1827 | ); 1828 | 1829 | if ($response->getStatusCode() === 200) { 1830 | echo 'The image has been successfully downloaded: ' . $imageFilePath; 1831 | } 1832 | ``` 1833 | 1834 |
Response 1835 |

1836 | 1837 | ```plain 1838 | The image has been successfully downloaded: /home/serge/curl-examples/02_Advanced/01_Files/04_Download/resource/image.jpeg 1839 | ``` 1840 | 1841 |

1842 |
1843 | 1844 | ## Auth 1845 | 1846 | ### Basic Auth 1847 | 1848 | #### Bash 1849 | 1850 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/01_Basic_Auth/console.sh)] 1851 | 1852 | ```bash 1853 | curl --user postman:password --request GET "https://postman-echo.com/basic-auth" 1854 | ``` 1855 | 1856 | #### PHP CURL extension 1857 | 1858 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/01_Basic_Auth/curl-ext.php)] 1859 | 1860 | ```php 1861 | $curlHandler = curl_init(); 1862 | 1863 | $userName = 'postman'; 1864 | $password = 'password'; 1865 | 1866 | curl_setopt_array($curlHandler, [ 1867 | CURLOPT_URL => 'https://postman-echo.com/basic-auth', 1868 | CURLOPT_RETURNTRANSFER => true, 1869 | 1870 | CURLOPT_HTTPAUTH => CURLAUTH_BASIC, 1871 | CURLOPT_USERPWD => $userName . ':' . $password, 1872 | ]); 1873 | 1874 | $response = curl_exec($curlHandler); 1875 | curl_close($curlHandler); 1876 | 1877 | print_r('First example response: ' . $response . PHP_EOL); 1878 | 1879 | /** 1880 | * Or specify credentials in Authorization header 1881 | */ 1882 | 1883 | $curlSecondHandler = curl_init(); 1884 | 1885 | curl_setopt_array($curlSecondHandler, [ 1886 | CURLOPT_URL => 'https://postman-echo.com/basic-auth', 1887 | CURLOPT_RETURNTRANSFER => true, 1888 | 1889 | CURLOPT_HTTPHEADER => [ 1890 | 'Authorization: Basic ' . base64_encode($userName . ':' . $password) 1891 | ], 1892 | ]); 1893 | 1894 | $response = curl_exec($curlSecondHandler); 1895 | curl_close($curlSecondHandler); 1896 | 1897 | print_r('Second example response: ' . $response . PHP_EOL); 1898 | ``` 1899 | 1900 | #### PHP Guzzle library 1901 | 1902 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/01_Basic_Auth/guzzle-lib.php)] 1903 | 1904 | ```php 1905 | use GuzzleHttp\Client; 1906 | use GuzzleHttp\RequestOptions; 1907 | 1908 | $userName = 'postman'; 1909 | $password = 'password'; 1910 | 1911 | $httpClient = new Client(); 1912 | 1913 | $response = $httpClient->get( 1914 | 'https://postman-echo.com/basic-auth', 1915 | [ 1916 | RequestOptions::AUTH => [$userName, $password] 1917 | ] 1918 | ); 1919 | 1920 | print_r($response->getBody()->getContents()); 1921 | ``` 1922 | 1923 |
Response 1924 |

1925 | 1926 | ```json 1927 | {"authenticated":true} 1928 | ``` 1929 | 1930 |

1931 |
1932 | 1933 | ### Digest Auth 1934 | 1935 | #### Bash 1936 | 1937 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/02_Digest_Auth/console.sh)] 1938 | 1939 | ```bash 1940 | curl --digest --user postman:password --request GET "https://postman-echo.com/digest-auth" 1941 | ``` 1942 | 1943 | #### PHP CURL extension 1944 | 1945 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/02_Digest_Auth/curl-ext.php)] 1946 | 1947 | ```php 1948 | $curlHandler = curl_init(); 1949 | 1950 | $userName = 'postman'; 1951 | $password = 'password'; 1952 | 1953 | curl_setopt_array($curlHandler, [ 1954 | CURLOPT_URL => 'https://postman-echo.com/digest-auth', 1955 | CURLOPT_RETURNTRANSFER => true, 1956 | 1957 | CURLOPT_HTTPAUTH => CURLAUTH_DIGEST, 1958 | CURLOPT_USERPWD => $userName . ':' . $password, 1959 | ]); 1960 | 1961 | $response = curl_exec($curlHandler); 1962 | curl_close($curlHandler); 1963 | 1964 | print_r($response); 1965 | ``` 1966 | 1967 | #### PHP Guzzle library 1968 | 1969 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/02_Digest_Auth/guzzle-lib.php)] 1970 | 1971 | ```php 1972 | use GuzzleHttp\Client; 1973 | use GuzzleHttp\RequestOptions; 1974 | 1975 | $userName = 'postman'; 1976 | $password = 'password'; 1977 | 1978 | $httpClient = new Client(); 1979 | 1980 | $response = $httpClient->get( 1981 | 'https://postman-echo.com/digest-auth', 1982 | [ 1983 | RequestOptions::AUTH => [$userName, $password, 'digest'] 1984 | ] 1985 | ); 1986 | 1987 | print_r($response->getBody()->getContents()); 1988 | ``` 1989 | 1990 |
Response 1991 |

1992 | 1993 | ```json 1994 | {"authenticated":true} 1995 | ``` 1996 | 1997 |

1998 |
1999 | 2000 | ### Bearer Auth 2001 | 2002 | #### Bash 2003 | 2004 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/03_Bearer_Auth/console.sh)] 2005 | 2006 | ```bash 2007 | curl -X GET "https://httpbin.org/bearer" -H "Accept: application/json" -H "Authorization: Bearer your_token" 2008 | ``` 2009 | 2010 | #### PHP CURL extension 2011 | 2012 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/03_Bearer_Auth/curl-ext.php)] 2013 | 2014 | ```php 2015 | $curlHandler = curl_init(); 2016 | 2017 | $token = 'your_token'; 2018 | 2019 | curl_setopt_array($curlHandler, [ 2020 | CURLOPT_URL => 'https://httpbin.org/bearer', 2021 | CURLOPT_RETURNTRANSFER => true, 2022 | CURLOPT_HTTPHEADER => [ 2023 | 'Accept: application/json', 2024 | 'Authorization: Bearer ' . $token 2025 | ], 2026 | ]); 2027 | 2028 | $response = curl_exec($curlHandler); 2029 | curl_close($curlHandler); 2030 | 2031 | print_r($response); 2032 | ``` 2033 | 2034 | #### PHP Guzzle library 2035 | 2036 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/03_Bearer_Auth/guzzle-lib.php)] 2037 | 2038 | ```php 2039 | use GuzzleHttp\Client; 2040 | use GuzzleHttp\RequestOptions; 2041 | 2042 | $token = 'your_token'; 2043 | 2044 | $httpClient = new Client(); 2045 | 2046 | $response = $httpClient->get( 2047 | 'https://httpbin.org/bearer', 2048 | [ 2049 | RequestOptions::HEADERS => [ 2050 | 'Accept' => 'application/json', 2051 | 'Authorization' => 'Bearer ' . $token, 2052 | ] 2053 | ] 2054 | ); 2055 | 2056 | print_r($response->getBody()->getContents()); 2057 | ``` 2058 | 2059 |
Response 2060 |

2061 | 2062 | ```json 2063 | { 2064 | "authenticated": true, 2065 | "token": "your_token" 2066 | } 2067 | ``` 2068 | 2069 |

2070 |
2071 | 2072 | ## Cookies 2073 | 2074 | ### Send cookies from string 2075 | 2076 | #### Bash 2077 | 2078 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/01_Send_Cookies_From_String/console.sh)] 2079 | 2080 | ```bash 2081 | curl --cookie "foo=bar;baz=foo" --request GET "https://httpbin.org/cookies" 2082 | ``` 2083 | 2084 | #### PHP CURL extension 2085 | 2086 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/01_Send_Cookies_From_String/curl-ext.php)] 2087 | 2088 | ```php 2089 | $curlHandler = curl_init(); 2090 | 2091 | curl_setopt_array($curlHandler, [ 2092 | CURLOPT_URL => 'https://httpbin.org/cookies', 2093 | CURLOPT_RETURNTRANSFER => true, 2094 | 2095 | CURLOPT_COOKIE => 'foo=bar;baz=foo', 2096 | 2097 | /** 2098 | * Or set header 2099 | * CURLOPT_HTTPHEADER => [ 2100 | 'Cookie: foo=bar;baz=foo', 2101 | ] 2102 | */ 2103 | ]); 2104 | 2105 | $response = curl_exec($curlHandler); 2106 | curl_close($curlHandler); 2107 | 2108 | echo $response; 2109 | ``` 2110 | 2111 | #### PHP Guzzle library 2112 | 2113 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/02_Auth/03_Bearer_Auth/guzzle-lib.php)] 2114 | 2115 | ```php 2116 | use GuzzleHttp\Client; 2117 | use GuzzleHttp\Cookie\CookieJar; 2118 | use GuzzleHttp\RequestOptions; 2119 | 2120 | $httpClient = new Client(); 2121 | 2122 | $url = 'https://httpbin.org/cookies'; 2123 | $urlHost = parse_url($url, PHP_URL_HOST); 2124 | 2125 | $cookieJar = CookieJar::fromArray( 2126 | [ 2127 | 'foo' => 'bar', 2128 | 'baz' => 'foo', 2129 | ], 2130 | $urlHost 2131 | ); 2132 | 2133 | $response = $httpClient->get( 2134 | $url, 2135 | [ 2136 | RequestOptions::COOKIES => $cookieJar, 2137 | ] 2138 | ); 2139 | 2140 | echo($response->getBody()->getContents()); 2141 | ``` 2142 | 2143 |
Response 2144 |

2145 | 2146 | ```json 2147 | { 2148 | "cookies": { 2149 | "baz": "foo", 2150 | "foo": "bar" 2151 | } 2152 | } 2153 | ``` 2154 | 2155 |

2156 |
2157 | 2158 | ### Set cookie options 2159 | 2160 | Cookies have attributes that can be sent from the server to the client, but not from the client to the server. 2161 | The client can only send the name and value of the cookie. 2162 | 2163 | #### PHP Guzzle library 2164 | 2165 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/02_Set_Cookie_Options/guzzle-lib.php)] 2166 | 2167 | ```php 2168 | use GuzzleHttp\Client; 2169 | use GuzzleHttp\Cookie\CookieJar; 2170 | use GuzzleHttp\Cookie\SetCookie; 2171 | use GuzzleHttp\RequestOptions; 2172 | 2173 | $httpClient = new Client(); 2174 | 2175 | $url = 'https://httpbin.org/cookies'; 2176 | $urlHost = parse_url($url, PHP_URL_HOST); 2177 | 2178 | $cookies = [ 2179 | new SetCookie([ 2180 | 'Name' => 'foo', 2181 | 'Value' => 'bar', 2182 | 2183 | // Other attributes will not be sent to the server, they are only needed for validation. 2184 | 'Domain' => $urlHost, 2185 | 'Path' => '/', 2186 | 'Max-Age' => 100, 2187 | 'Secure' => false, 2188 | 'Discard' => false, 2189 | 'HttpOnly' => false, 2190 | ]), 2191 | new SetCookie([ 2192 | 'Name' => 'baz', 2193 | 'Value' => 'foo', 2194 | 'Domain' => $urlHost, 2195 | 'Path' => '/', 2196 | 'Max-Age' => 100, 2197 | 'Secure' => false, 2198 | 'Discard' => false, 2199 | 'HttpOnly' => false, 2200 | ]), 2201 | ]; 2202 | 2203 | $cookieJar = new CookieJar(true, $cookies); 2204 | 2205 | $response = $httpClient->get( 2206 | $url, 2207 | [ 2208 | RequestOptions::COOKIES => $cookieJar, 2209 | ] 2210 | ); 2211 | 2212 | echo($response->getBody()->getContents()); 2213 | ``` 2214 | 2215 | #### Request headers 2216 | 2217 | ```plain 2218 | > GET /cookies HTTP/1.1 2219 | Host: httpbin.org 2220 | User-Agent: GuzzleHttp/6.3.3 curl/7.58.0 PHP/7.3.6-1+ubuntu18.04.1+deb.sury.org+1 2221 | Cookie: foo=bar; baz=foo 2222 | ``` 2223 | 2224 |
Response 2225 |

2226 | 2227 | ```json 2228 | { 2229 | "cookies": { 2230 | "baz": "foo", 2231 | "foo": "bar" 2232 | } 2233 | } 2234 | ``` 2235 |

2236 |
2237 | 2238 | ### Send cookies from file 2239 | 2240 | #### Bash 2241 | 2242 | Cookie JAR file structure: 2243 | 2244 | ```plain 2245 | # Domain Flag Path Secure Expiration Name Value 2246 | httpbin.org FALSE / FALSE 0 foo bar 2247 | httpbin.org FALSE / FALSE 0 baz foo 2248 | ``` 2249 | 2250 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/03_Send_Cookies_From_File/console.sh)] 2251 | 2252 | ```bash 2253 | curl --cookie "02_Advanced/03_Cookies/03_Send_Cookies_From_File/resource/cookie-jar.txt" --request GET "https://httpbin.org/cookies" 2254 | ``` 2255 | 2256 | #### PHP CURL extension 2257 | 2258 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/03_Send_Cookies_From_File/curl-ext.php)] 2259 | 2260 | ```php 2261 | $curlHandler = curl_init(); 2262 | 2263 | $cookieFile = __DIR__ . '/resource/cookie-jar.txt'; 2264 | 2265 | curl_setopt_array($curlHandler, [ 2266 | CURLOPT_URL => 'https://httpbin.org/cookies', 2267 | CURLOPT_RETURNTRANSFER => true, 2268 | 2269 | CURLOPT_COOKIEFILE => $cookieFile, 2270 | ]); 2271 | 2272 | $response = curl_exec($curlHandler); 2273 | curl_close($curlHandler); 2274 | 2275 | echo $response; 2276 | ``` 2277 | 2278 | #### PHP Guzzle library 2279 | 2280 | Guzzle cookie file structure: 2281 | 2282 | ```json 2283 | [ 2284 | { 2285 | "Name": "foo", 2286 | "Value": "bar", 2287 | "Domain": "httpbin.org", 2288 | "Path": "\/", 2289 | "Max-Age": 100, 2290 | "Expires": 1567343671, 2291 | "Secure": false, 2292 | "Discard": false, 2293 | "HttpOnly": false 2294 | }, 2295 | { 2296 | "Name": "baz", 2297 | "Value": "foo", 2298 | "Domain": "httpbin.org", 2299 | "Path": "\/", 2300 | "Max-Age": 100, 2301 | "Expires": 1567343671, 2302 | "Secure": false, 2303 | "Discard": false, 2304 | "HttpOnly": false 2305 | } 2306 | ] 2307 | ``` 2308 | 2309 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/03_Send_Cookies_From_File/guzzle-lib.php)] 2310 | 2311 | ```php 2312 | use GuzzleHttp\Client; 2313 | use GuzzleHttp\RequestOptions; 2314 | use GuzzleHttp\Cookie\FileCookieJar; 2315 | 2316 | $httpClient = new Client(); 2317 | 2318 | $cookieJarFile = new FileCookieJar( 2319 | __DIR__ . '/resource/guzzle-cookie-jar.json', 2320 | false 2321 | ); 2322 | 2323 | $response = $httpClient->get( 2324 | 'https://httpbin.org/cookies', 2325 | [ 2326 | RequestOptions::COOKIES => $cookieJarFile, 2327 | ] 2328 | ); 2329 | 2330 | echo $response->getBody()->getContents(); 2331 | ``` 2332 | 2333 |
Response 2334 |

2335 | 2336 | ```json 2337 | { 2338 | "cookies": { 2339 | "baz": "foo", 2340 | "foo": "bar" 2341 | } 2342 | } 2343 | ``` 2344 |

2345 |
2346 | 2347 | ### Save Response cookies to file 2348 | 2349 | #### Bash 2350 | 2351 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/console.sh)] 2352 | 2353 | ```bash 2354 | curl --location --cookie-jar "02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/resource/cookie-jar.txt" --request GET "https://httpbin.org/cookies/set/foo/bar" 2355 | ``` 2356 | 2357 | #### PHP CURL extension 2358 | 2359 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/curl-ext.php)] 2360 | 2361 | ```php 2362 | $curlHandler = curl_init(); 2363 | 2364 | $cookieFile = __DIR__ . '/resource/cookie-jar.txt'; 2365 | 2366 | curl_setopt_array($curlHandler, [ 2367 | CURLOPT_URL => 'https://httpbin.org/cookies/set/foo/bar', 2368 | CURLOPT_RETURNTRANSFER => true, 2369 | 2370 | CURLOPT_COOKIEJAR => $cookieFile, 2371 | CURLOPT_FOLLOWLOCATION => true, 2372 | ]); 2373 | 2374 | $response = curl_exec($curlHandler); 2375 | curl_close($curlHandler); 2376 | 2377 | echo $response; 2378 | ``` 2379 | 2380 | #### PHP Guzzle library 2381 | 2382 | [[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/guzzle-lib.php)] 2383 | 2384 | ```php 2385 | use GuzzleHttp\Client; 2386 | use GuzzleHttp\RequestOptions; 2387 | use GuzzleHttp\Cookie\FileCookieJar; 2388 | 2389 | $httpClient = new Client(); 2390 | 2391 | $cookieJarFile = new FileCookieJar( 2392 | __DIR__ . '/resource/guzzle-cookie-jar.json', 2393 | true 2394 | ); 2395 | 2396 | $response = $httpClient->get( 2397 | 'https://httpbin.org/cookies/set/foo/bar', 2398 | [ 2399 | RequestOptions::COOKIES => $cookieJarFile, 2400 | ] 2401 | ); 2402 | 2403 | echo $response->getBody()->getContents(); 2404 | ``` 2405 | 2406 |
Response 2407 |

2408 | 2409 | ```json 2410 | { 2411 | "cookies": { 2412 | "foo": "bar" 2413 | } 2414 | } 2415 | ``` 2416 |

2417 |
2418 | 2419 | ## Todo 2420 | 2421 | Project [link](https://github.com/andriichuk/php-curl-cookbook/projects/1#column-6364809) 2422 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "ext-curl": "*", 4 | "ext-mbstring": "*", 5 | "ext-fileinfo": "*", 6 | "ext-json": "*", 7 | "guzzlehttp/guzzle": "^6.3" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "7e7cb8c5114fd6cdde49ba4c7719c770", 8 | "packages": [ 9 | { 10 | "name": "guzzlehttp/guzzle", 11 | "version": "6.3.3", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/guzzle/guzzle.git", 15 | "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/guzzle/guzzle/zipball/407b0cb880ace85c9b63c5f9551db498cb2d50ba", 20 | "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "guzzlehttp/promises": "^1.0", 25 | "guzzlehttp/psr7": "^1.4", 26 | "php": ">=5.5" 27 | }, 28 | "require-dev": { 29 | "ext-curl": "*", 30 | "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0", 31 | "psr/log": "^1.0" 32 | }, 33 | "suggest": { 34 | "psr/log": "Required for using the Log middleware" 35 | }, 36 | "type": "library", 37 | "extra": { 38 | "branch-alias": { 39 | "dev-master": "6.3-dev" 40 | } 41 | }, 42 | "autoload": { 43 | "files": [ 44 | "src/functions_include.php" 45 | ], 46 | "psr-4": { 47 | "GuzzleHttp\\": "src/" 48 | } 49 | }, 50 | "notification-url": "https://packagist.org/downloads/", 51 | "license": [ 52 | "MIT" 53 | ], 54 | "authors": [ 55 | { 56 | "name": "Michael Dowling", 57 | "email": "mtdowling@gmail.com", 58 | "homepage": "https://github.com/mtdowling" 59 | } 60 | ], 61 | "description": "Guzzle is a PHP HTTP client library", 62 | "homepage": "http://guzzlephp.org/", 63 | "keywords": [ 64 | "client", 65 | "curl", 66 | "framework", 67 | "http", 68 | "http client", 69 | "rest", 70 | "web service" 71 | ], 72 | "time": "2018-04-22T15:46:56+00:00" 73 | }, 74 | { 75 | "name": "guzzlehttp/promises", 76 | "version": "v1.3.1", 77 | "source": { 78 | "type": "git", 79 | "url": "https://github.com/guzzle/promises.git", 80 | "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" 81 | }, 82 | "dist": { 83 | "type": "zip", 84 | "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", 85 | "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", 86 | "shasum": "" 87 | }, 88 | "require": { 89 | "php": ">=5.5.0" 90 | }, 91 | "require-dev": { 92 | "phpunit/phpunit": "^4.0" 93 | }, 94 | "type": "library", 95 | "extra": { 96 | "branch-alias": { 97 | "dev-master": "1.4-dev" 98 | } 99 | }, 100 | "autoload": { 101 | "psr-4": { 102 | "GuzzleHttp\\Promise\\": "src/" 103 | }, 104 | "files": [ 105 | "src/functions_include.php" 106 | ] 107 | }, 108 | "notification-url": "https://packagist.org/downloads/", 109 | "license": [ 110 | "MIT" 111 | ], 112 | "authors": [ 113 | { 114 | "name": "Michael Dowling", 115 | "email": "mtdowling@gmail.com", 116 | "homepage": "https://github.com/mtdowling" 117 | } 118 | ], 119 | "description": "Guzzle promises library", 120 | "keywords": [ 121 | "promise" 122 | ], 123 | "time": "2016-12-20T10:07:11+00:00" 124 | }, 125 | { 126 | "name": "guzzlehttp/psr7", 127 | "version": "1.8.5", 128 | "source": { 129 | "type": "git", 130 | "url": "https://github.com/guzzle/psr7.git", 131 | "reference": "337e3ad8e5716c15f9657bd214d16cc5e69df268" 132 | }, 133 | "dist": { 134 | "type": "zip", 135 | "url": "https://api.github.com/repos/guzzle/psr7/zipball/337e3ad8e5716c15f9657bd214d16cc5e69df268", 136 | "reference": "337e3ad8e5716c15f9657bd214d16cc5e69df268", 137 | "shasum": "" 138 | }, 139 | "require": { 140 | "php": ">=5.4.0", 141 | "psr/http-message": "~1.0", 142 | "ralouphie/getallheaders": "^2.0.5 || ^3.0.0" 143 | }, 144 | "provide": { 145 | "psr/http-message-implementation": "1.0" 146 | }, 147 | "require-dev": { 148 | "ext-zlib": "*", 149 | "phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.5.8 || ^9.3.10" 150 | }, 151 | "suggest": { 152 | "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" 153 | }, 154 | "type": "library", 155 | "extra": { 156 | "branch-alias": { 157 | "dev-master": "1.7-dev" 158 | } 159 | }, 160 | "autoload": { 161 | "files": [ 162 | "src/functions_include.php" 163 | ], 164 | "psr-4": { 165 | "GuzzleHttp\\Psr7\\": "src/" 166 | } 167 | }, 168 | "notification-url": "https://packagist.org/downloads/", 169 | "license": [ 170 | "MIT" 171 | ], 172 | "authors": [ 173 | { 174 | "name": "Graham Campbell", 175 | "email": "hello@gjcampbell.co.uk", 176 | "homepage": "https://github.com/GrahamCampbell" 177 | }, 178 | { 179 | "name": "Michael Dowling", 180 | "email": "mtdowling@gmail.com", 181 | "homepage": "https://github.com/mtdowling" 182 | }, 183 | { 184 | "name": "George Mponos", 185 | "email": "gmponos@gmail.com", 186 | "homepage": "https://github.com/gmponos" 187 | }, 188 | { 189 | "name": "Tobias Nyholm", 190 | "email": "tobias.nyholm@gmail.com", 191 | "homepage": "https://github.com/Nyholm" 192 | }, 193 | { 194 | "name": "Márk Sági-Kazár", 195 | "email": "mark.sagikazar@gmail.com", 196 | "homepage": "https://github.com/sagikazarmark" 197 | }, 198 | { 199 | "name": "Tobias Schultze", 200 | "email": "webmaster@tubo-world.de", 201 | "homepage": "https://github.com/Tobion" 202 | } 203 | ], 204 | "description": "PSR-7 message implementation that also provides common utility methods", 205 | "keywords": [ 206 | "http", 207 | "message", 208 | "psr-7", 209 | "request", 210 | "response", 211 | "stream", 212 | "uri", 213 | "url" 214 | ], 215 | "support": { 216 | "issues": "https://github.com/guzzle/psr7/issues", 217 | "source": "https://github.com/guzzle/psr7/tree/1.8.5" 218 | }, 219 | "funding": [ 220 | { 221 | "url": "https://github.com/GrahamCampbell", 222 | "type": "github" 223 | }, 224 | { 225 | "url": "https://github.com/Nyholm", 226 | "type": "github" 227 | }, 228 | { 229 | "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/psr7", 230 | "type": "tidelift" 231 | } 232 | ], 233 | "time": "2022-03-20T21:51:18+00:00" 234 | }, 235 | { 236 | "name": "psr/http-message", 237 | "version": "1.0.1", 238 | "source": { 239 | "type": "git", 240 | "url": "https://github.com/php-fig/http-message.git", 241 | "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" 242 | }, 243 | "dist": { 244 | "type": "zip", 245 | "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", 246 | "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", 247 | "shasum": "" 248 | }, 249 | "require": { 250 | "php": ">=5.3.0" 251 | }, 252 | "type": "library", 253 | "extra": { 254 | "branch-alias": { 255 | "dev-master": "1.0.x-dev" 256 | } 257 | }, 258 | "autoload": { 259 | "psr-4": { 260 | "Psr\\Http\\Message\\": "src/" 261 | } 262 | }, 263 | "notification-url": "https://packagist.org/downloads/", 264 | "license": [ 265 | "MIT" 266 | ], 267 | "authors": [ 268 | { 269 | "name": "PHP-FIG", 270 | "homepage": "http://www.php-fig.org/" 271 | } 272 | ], 273 | "description": "Common interface for HTTP messages", 274 | "homepage": "https://github.com/php-fig/http-message", 275 | "keywords": [ 276 | "http", 277 | "http-message", 278 | "psr", 279 | "psr-7", 280 | "request", 281 | "response" 282 | ], 283 | "support": { 284 | "source": "https://github.com/php-fig/http-message/tree/master" 285 | }, 286 | "time": "2016-08-06T14:39:51+00:00" 287 | }, 288 | { 289 | "name": "ralouphie/getallheaders", 290 | "version": "3.0.3", 291 | "source": { 292 | "type": "git", 293 | "url": "https://github.com/ralouphie/getallheaders.git", 294 | "reference": "120b605dfeb996808c31b6477290a714d356e822" 295 | }, 296 | "dist": { 297 | "type": "zip", 298 | "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", 299 | "reference": "120b605dfeb996808c31b6477290a714d356e822", 300 | "shasum": "" 301 | }, 302 | "require": { 303 | "php": ">=5.6" 304 | }, 305 | "require-dev": { 306 | "php-coveralls/php-coveralls": "^2.1", 307 | "phpunit/phpunit": "^5 || ^6.5" 308 | }, 309 | "type": "library", 310 | "autoload": { 311 | "files": [ 312 | "src/getallheaders.php" 313 | ] 314 | }, 315 | "notification-url": "https://packagist.org/downloads/", 316 | "license": [ 317 | "MIT" 318 | ], 319 | "authors": [ 320 | { 321 | "name": "Ralph Khattar", 322 | "email": "ralph.khattar@gmail.com" 323 | } 324 | ], 325 | "description": "A polyfill for getallheaders.", 326 | "support": { 327 | "issues": "https://github.com/ralouphie/getallheaders/issues", 328 | "source": "https://github.com/ralouphie/getallheaders/tree/develop" 329 | }, 330 | "time": "2019-03-08T08:55:37+00:00" 331 | } 332 | ], 333 | "packages-dev": [], 334 | "aliases": [], 335 | "minimum-stability": "stable", 336 | "stability-flags": [], 337 | "prefer-stable": false, 338 | "prefer-lowest": false, 339 | "platform": { 340 | "ext-curl": "*", 341 | "ext-mbstring": "*", 342 | "ext-fileinfo": "*", 343 | "ext-json": "*" 344 | }, 345 | "platform-dev": [], 346 | "plugin-api-version": "2.2.0" 347 | } 348 | -------------------------------------------------------------------------------- /console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | docker-compose run --rm php-fpm bash -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.1" 2 | 3 | services: 4 | 5 | php-fpm: 6 | build: docker/php-fpm 7 | container_name: php-curl-cookbook-php-fpm 8 | working_dir: /app 9 | volumes: 10 | - .:/app -------------------------------------------------------------------------------- /docker/php-fpm/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM phpdockerio/php73-fpm:latest 2 | 3 | WORKDIR "/app" 4 | 5 | # Fix debconf warnings upon build 6 | ARG DEBIAN_FRONTEND=noninteractive 7 | 8 | # Install selected extensions and other stuff 9 | RUN apt-get update \ 10 | && apt-get -y --no-install-recommends install php-xdebug \ 11 | && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* -------------------------------------------------------------------------------- /run: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ARGS=$@ 4 | 5 | docker-compose run --rm php-fpm bash -c "${ARGS}" --------------------------------------------------------------------------------