PHP cURL Tutorial with Examples – POFTUT

PHP cURL Tutorial with Examples

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.

PHP cURL Library

PHP cURL Library

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.

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.

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() function.

LEARN MORE  OpenPGP Mail Encryption and Related Tools For Linux, Windows

  • 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.

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.

echo '
'

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.

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.

LEARN MORE  How To Install Tar.Gz or Tar.Bz2 Application Source Code?

Get URL with cURL

Get URL with cURL

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.

Return Page Contents To A Variable

Return Page Contents To A Variable

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 PofWeb.

Follow Redirects

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 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.

LEARN MORE  Linux pv Command Tutorial With Examples To Monitor Progress Of Copy/Backup/Compress

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.

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_VERIFYHOST and CURLOPT_SSL_VERIFYPEER options to false.

Leave a Reply

Your email address will not be published. Required fields are marked *