PHP programming language standard library supports the cURL library named
libcurl. libcurl is a library implementation of the cURL tool which is used to communicate with servers and clients by using popular protocols like HTTP, FTP, etc. cURL can be used for HTTP POST, HTTP PUT, FTP Upload requests.
PHP cURL Library
PHP cURL library generally does not installed by default for most of the Linux distributions. In Windows, all libraries including PHP cURL is installed with the PHP. So we do not need to install an extra library for Windows operating systems. We can install PHP cURL library for deb based distributions like Ubuntu, Debian, Mint, Kali like below.
$ sudo apt install php-curl
PHP cURL Usage Steps
In order to use cURL in PHP, we have to follow the following steps that are required to initialize, configure, execute and close a cURL session. Of course, we will provide some parameters and arguments to these functions in order to set the cURL behavior.
curl_init(); // initializes a cURL session curl_setopt(); // changes the cURL session behavior with options curl_exec(); // executes the started cURL session curl_close(); // closes the cURL session and deletes the variable made by curl_init();
Initialize cURL Sessions
The first is to use or create PHP cURL sessions is using the
curl_init() function which is used to initialize the cURL session. curl_init() function can be called without a parameter or with a URL parameter which will set the remote URL we can to use and communicate.
//Only create a cURL session and set to $ch variable $ch = curl_init(); //Create a sessions variable named $ch //and initialize it for the "http://www.poftut.com" URL $ch = curl_init("http://www.poftut.com/");
Set cURL Options
Setting cURL options is not mandatory where if no option is set the default option configuration will be used. In order to set cURL sessions options, we will provide the cURL session variable we have already initialized and the option we want to set. There are a lot of options to set. We can set these options one by one executing the
curl_setopt(SESSION_VARIABLE, OPTION, OPTION_VALUE);
- `SESSION_VARIABLE` is the variable that is initialized as a PHP cURL session.
- `OPTION` is the option we want the set or change.
- `OPTION_VALUE` is the value for the specified option.
$ch = curl_init(); //Set URL as "http://www.poftut.com" curl_setopt($ch, CURLOPT_URL, "http://www.poftut.com/"); //Set HTTP Header curl_setopt($ch, CURLOPT_HEADER, Array("Content-Type: text/xml")); //Return Page Contents curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Execute cURL Request
The third step is executing the cURL request or session with the
curl_exec() function where we will provide the cURL session variable as a parameter.
$ch = curl_init(); //Set URL as "http://www.poftut.com" curl_setopt($ch, CURLOPT_URL, "http://www.poftut.com/"); //Execute curl with given options curl_exec($ch);
Close cURL Session
The last step for a PHP cURL request is closing the session and releasing the resources.
curl_close() is used to close and release all resources like memory, network, etc.
//Initialize cURL session $ch = curl_init(); //Set URL as "http://www.poftut.com" curl_setopt($ch, CURLOPT_URL, "http://www.poftut.com/"); //Execute curl with given options curl_exec($ch); //Close the cURL session curl_close($ch);
Get URL with cURL
Now we have learned basics of the PHP cURL usage. We will start examining the different types of examples. First, we will make an HTTP GET request to the google.com and print the request-response to the standard output or command line.
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.google.com'); curl_exec($ch); curl_close($ch);
Return Page Contents To A Variable
Alternatively, we can put the returned response from the cURL request into a variable. We will use the
CURLOPT_RETURNTRANSFER option and set this option value is 1.
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.google.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $contents = curl_exec($ch); echo $contents;
Set User-Agent HTTP Header
We can also set the User-Agent header of the HTTP request by using
CURLOPT_HEADER option with the
"User-Agent: PofWeb" value. In this case, the user agent or browser name expressed as
$ch = curl_init(); //Set URL as "http://www.poftut.com" curl_setopt($ch, CURLOPT_URL, "http://www.poftut.com/"); //Set HTTP Header curl_setopt($ch, CURLOPT_HEADER, Array("User-Agent: PofWeb")); //Return Page Contents curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Close cURL session curl_close($ch);
Redirects are used to redirect the user to from requested URL into another URL. Redirects can be useful if the current URL does not provide historical resources. We can also make the PHP cURL to follow redirects by using
CURLOPT_FOLLOWLOCATION options with the
// Follow redirects, if any curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
Fail cURL If Response Is 400, 404
The HTTP protocol provides return or HTTP status codes in order to represent the request status. Codes like 4XX, 400, 404 are used to express different error about the processing of the request. If the current PHP cURL request is returned an error status code we can make the cURL fail and return some error code. We will use
<span class="pln">CURLOPT_FAILONERROR option.
// Fail the cURL request if response code = 400 (like 404 errors) curl_setopt($curl, CURLOPT_FAILONERROR, true);
Wait For Specified Time For Connect
While connecting to the remote server there may be some delay which can be related to internet speed server speed etc. We can set some timeout in order to prevent waiting indefinitely. We will use
CURLOPT_TIMEOUT option and provide the seconds for timeout which is 10 in this example. After 10 seconds the connection request will be stopped.
// Execute the cURL request for a maximum of 50 seconds curl_setopt($curl, CURLOPT_TIMEOUT, 10);
Skip SSL Certificate Check
Today’s HTTP connections are very secure with the usage of the TCL/SSL certificates. TLS/SSL certificates are used to encrypt the connection and approved from certificate authorities for the usage of the different parties. The default behavior of PHP cURL is stopping request if the remote TLS/SSL certificate is not valid. We can prevent this and continue even the remote HTTP server TLS/SSL certificates are not valid. We will just set
CURLOPT_SSL_VERIFYPEER options to
// Do not check the SSL certificates curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);