Using cURL in PHP (full)
https://phppedia.com/en/tutorial/701/using-curl-in-php#
Using cURL in PHP
Syntax
resource curl_init ([ string $url = NULL ] )
bool curl_setopt ( resource $ch , int $option , mixed $value )
bool curl_setopt_array ( resource $ch, array $options )
mixed curl_exec ( resource $ch )
void curl_close ( resource $ch )
Parameters
Basic Usage (GET Requests)
cURL is a tool for transferring data with URL syntax. It support HTTP, FTP, SCP and many others(curl >= 7.19.4). Remember, you need to install and enable the cURL extension to use it.
Creating and sending a request with a custom method
By default, PHP Curl supports GET
and POST
requests. It is possible to also send custom requests, such as DELETE
, PUT
or PATCH
(or even non-standard methods) using the CURLOPT_CUSTOMREQUEST
parameter.
Get and Set custom http headers in php
Sending The Request Header
Reading the custom header
OutPut :-
We can also send the header using below syntax :-
POST Requests
If you want to mimic HTML form POST action, you can use cURL.
Sending multi-dimensional data and multiple files with CurlFile in one request
Let's say we have a form like the one below. We want to send the data to our webserver via AJAX and from there to a script running on an external server.
So we have normal inputs, a multi-select field and a file dropzone where we can upload multiple files.
Assuming the AJAX POST request was successful we get the following data on PHP site:
and the files should look like this
So far, so good. Now we want to send this data and files to the external server using cURL with the CurlFile Class
Since cURL only accepts a simple but not a multi-dimensional array, we have to flatten the $_POST array first.
To do this, you could use this function for example which gives you the following:
The next step is to create CurlFile Objects for the uploaded files. This is done by the following loop:
curl_file_create is a helper function of the CurlFile Class and creates the CurlFile objects. We save each object in the $files array with keys named "upload[0]" and "upload[1]" for our two files.
We now have to combine the flattened post array and the files array and save it as $data like this:
The last step is to send the cURL request:
Since $data is now a simple (flat) array, cURL automatically sends this POST request with Content Type: multipart/form-data
In upload.php on the external server you can now get the post data and files with $_POST and $_FILES as you would normally do.
Using Cookies
cURL can keep cookies received in responses for use with subsequent requests. For simple session cookie handling in memory, this is achieved with a single line of code:
In cases where you are required to keep cookies after the cURL handle is destroyed, you can specify the file to store them in:
Then, when you want to use them again, pass them as the cookie file:
Remember, though, that these two steps are not necessary unless you need to carry cookies between different cURL handles. For most use cases, setting CURLOPT_COOKIEFILE
to the empty string is all you need.
Cookie handling can be used, for example, to retrieve resources from a web site that requires a login. This is typically a two-step procedure. First, POST to the login page.
The second step (after standard error checking is done) is usually a simple GET request. The important thing is to reuse the existing cURL handle for the second request. This ensures the cookies from the first response will be automatically included in the second request.
This is only intended as an example of cookie handling. In real life, things are usually more complicated. Often you must perform an initial GET of the login page to pull a login token that needs to be included in your POST. Other sites might block the cURL client based on its User-Agent string, requiring you to change it.
Using multi_curl to make multiple POST requests
Sometimes we need to make a lot of POST requests to one or many different endpoints. To deal with this scenario, we can use multi_curl
.
First of all, we create how many requests as needed exactly in the same way of the simple example and put them in an array.
We use the curl_multi_init and add each handle to it.
In this example, we are using 2 different endpoints:
Then, we use curl_multi_exec to send the requests
A possible return for this example could be:
The request to 'http://www.example.com' returned 'fruits' in 2 seconds.
The request to 'http://www.example2.com' returned 'seafood' in 5 seconds.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0This website is not affiliated with Stack OverflowEmail: tutorialpedia@outlook.com
Last updated