CURLOPT_XXX to be set - see PHP documentation for the list of options and acceptable values
value
The value to be set on the cURL handle for the given option
curl_exec
-- Perform a cURL session
ch
The cURL handle (return value from curl_init())
curl_close
-- Close a cURL session
ch
The cURL handle (return value from curl_init())
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.
// a little script check is the cURL extension loaded or not
if(!extension_loaded("curl")) {
die("cURL extension not loaded! Quit Now.");
}
// Actual script start
// create a new cURL resource
// $curl is the handle of the resource
$curl = curl_init();
// set the URL and other options
curl_setopt($curl, CURLOPT_URL, "http://www.example.com");
// execute and pass the result to browser
curl_exec($curl);
// close the cURL resource
curl_close($curl);
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.
If you want to mimic HTML form POST action, you can use cURL.
// POST data in array
$post = [
'a' => 'apple',
'b' => 'banana'
];
// Create a new cURL resource with URL to POST
$ch = curl_init('http://www.example.com');
// We set parameter CURLOPT_RETURNTRANSFER to read output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Let's pass POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// We execute our request, and get output in a $response variable
$response = curl_exec($ch);
// Close the connection
curl_close($ch);
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:
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:
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:
curl_setopt($ch, CURLOPT_COOKIEFILE, "");
In cases where you are required to keep cookies after the cURL handle is destroyed, you can specify the file to store them in:
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.
<?php
# create a cURL handle
$ch = curl_init();
# set the URL (this could also be passed to curl_init() if desired)
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/login.php");
# set the HTTP method to POST
curl_setopt($ch, CURLOPT_POST, true);
# setting this option to an empty string enables cookie handling
# but does not load cookies from a file
curl_setopt($ch, CURLOPT_COOKIEFILE, "");
# set the values to be sent
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"username"=>"joe_bloggs",
"password"=>"$up3r_$3cr3t",
));
# return the response body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# send the request
$result = curl_exec($ch);
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.
# we are not calling curl_init()
# simply change the URL
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/show_me_the_foo.php");
# change the method back to GET
curl_setopt($ch, CURLOPT_HTTPGET, true);
# send the request
$result = curl_exec($ch);
# finished with cURL
curl_close($ch);
# do stuff with $result...
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:
//array of data to POST
$request_contents = array();
//array of URLs
$urls = array();
//array of cURL handles
$chs = array();
//first POST content
$request_contents[] = [
'a' => 'apple',
'b' => 'banana'
];
//second POST content
$request_contents[] = [
'a' => 'fish',
'b' => 'shrimp'
];
//set the urls
$urls[] = 'http://www.example.com';
$urls[] = 'http://www.example2.com';
//create the array of cURL handles and add to a multi_curl
$mh = curl_multi_init();
foreach ($urls as $key => $url) {
$chs[$key] = curl_init($url);
curl_setopt($chs[$key], CURLOPT_RETURNTRANSFER, true);
curl_setopt($chs[$key], CURLOPT_POST, true);
curl_setopt($chs[$key], CURLOPT_POSTFIELDS, $request_contents[$key]);
curl_multi_add_handle($mh, $chs[$key]);
}
Then, we use curl_multi_exec to send the requests
//running the requests
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
//getting the responses
foreach(array_keys($chs) as $key){
$error = curl_error($chs[$key]);
$last_effective_URL = curl_getinfo($chs[$key], CURLINFO_EFFECTIVE_URL);
$time = curl_getinfo($chs[$key], CURLINFO_TOTAL_TIME);
$response = curl_multi_getcontent($chs[$key]); // get results
if (!empty($error)) {
echo "The request $key return a error: $error" . "\n";
}
else {
echo "The request to '$last_effective_URL' returned '$response' in $time seconds." . "\n";
}
curl_multi_remove_handle($mh, $chs[$key]);
}
// close current handler
curl_multi_close($mh);