PHP cURL Tutorial

The cURL or Client for URLs is a popular tool and library used to download and upload files by using different protocols like FTP, HTTP, HTTPS, Telnet etc. PHP provides the cURL library libcurl natively as a built-in library.

cURL Functions

The PHP cURL library provides the following functions.

FunctionDescription
curl_init()This function is used to initialize a new session and return a cURL handle/object.
curl_exec()This function is used to run the cURL with the specified options.
curl_setop()This function is used to set options for the cURL handle/object.
curl_close()This function is used to close the existing cURL handle/object.

Check If cURL Is Enabled

The existence of the cURL library can be checked with the phpinfo() function. The phpinfo() function lists the current PHP configuration and provided libraries.

<?php

phpinfo();

?>

Run this PHP file with the following command. As the phinfo() creates a lot of output we can use the grep in order o filter the cURL like below.

$ php -f phpinfo.php  | grep cURL

If the cURL is provided via PHP the following output is displayed.

cURL => Sterling Hughes

Run cURL

In the following example, we use the cURL simply to download the specified web page. We download the https://www.wisetut.com/ . We use the curl_init() to create a cURL object and use curl_setopt() to set some options about the cURL operation. The curl_exec() is used to execute the cURL operation with the specified cURL object. The result is stored inside the $result variable which is HTML content as a string.

<?php

// URL we want to download with the cURL
$url = "https://www.wisetut.com/";

// Initialize a CURL.
$c = curl_init();

// Set Options for cURL
// CURLOPT_RETURNTRANSFER for return web page contents
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

// Set CURLOPT_URL from $url variable
curl_setopt($c, CURLOPT_URL, $url);

$result = curl_exec($c);

echo $result;

?>

Set Username and Password

Some protocols like HTTP, HTTPS, FTP, etc. may require a username and password for authentication. The cURL option is used to set the username and password. The CURLOPT_USERPWD option can be used to specify the username and password in USER:PASSWORD format. In the following example, we set the username as “ismail” and password as “MyS34cretPss”.

<?php

// URL we want to download with the cURL
$url = "https://www.wisetut.com/db.txt";

$infile = "/home/ismail/db.txt";

// Initialize a CURL.
$c = curl_init();

// Set Options for cURL
// CURLOPT_RETURNTRANSFER for return web page contents
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

// Set CURLOPT_URL from $url variable
curl_setopt($c, CURLOPT_URL, $url);

// Set CURLOPT_USERPWD for username and password
curl_setopt($c, CURLOPT_USERPWD, "ismail:MyS34cretPss");

// Set download file location
curl_setopt($c, CURLOPT_INFILE, $infile);

$result = curl_exec($c);

echo $result;

?>

Check cURL Error

Sometimes the cURL operation may not be completed successfully and create errors. Printing these errors to the screen is very useful to solve the error. After running the cURL with the curl_exec() error is stored inside the cURL object/handle which is initialized with the curl_init() .

<?php

// URL we want to download with the cURL
$url = "https://www.wisetut.com/";

// Initialize a CURL.
$c = curl_init();

// Set Options for cURL
// CURLOPT_RETURNTRANSFER for return web page contents
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

// Set CURLOPT_URL from $url variable
curl_setopt($c, CURLOPT_URL, $url);

$result = curl_exec($c);

$error_message = curl_errno($c) === CURLE_OK ? 'success' : 'failure';

echo $error_message;

?>

Leave a Comment