PHP cURL

by Vincy. Last modified on May 7th, 2022.

cURL – Client URL library is used to communicate with different types of servers with protocols FTP, HTTP, telnet, gopher and more.

In PHP, we have to install the libcurl library for working with the cURL script. If your PHP setup is not enabled with the cURL library, find the steps to enable this library in the PHP cURL post article.

If we used any one of the AMP bundles like XAMPP, to create a PHP environment, then the cURL library will come up with this bundle.

After cURL installation, we should enable this extension in the PHP configuration file.

//Remove semi colon (;) to enable
;extension=php_curl.dll

cURL Supported Operations

For handling remote file access, the cURL as an intermediary is used for,

  • Form submitting
  • Authentication
  • File upload
  • File transfer

Without cURL, we can read file contents in PHP using file_get_contents() function. For example, file_get_contents() can read remote data via URL.

It requires the allow_url_fopen configuration directive must be set. But is not advisable because of security reasons.

PHP cURL Delimiters

In a PHP program, the cURL portion should be enclosed within these two pairs of functions.

$ch = curl_init(URL Syntax);
...
curl_close($ch);

The curl_init() function returns the cURL session handle with respect to the given file URL. And then, the curl_close() function will be an end delimiter to close the cURL session with respect to its handle.

cURL Options

PHP curl_setopt() function is used to set options. It receives the cURL handle, option’s name and value as its parameters.

We can use another cURL function curl_setopt_array() to set an array of multiple options at a push.

curl_setopt ($ch, CURLOPT_HEADER, 1);

cURL Option Constants

  • CURLOPT_FILE – target file to write cURL result.
  • CURLOPT_URL – URL from where we need to get data. If this URL is specified with curl_init(), then no need to set this option
  • CURLOPT_RETURNTRANSFER – to return the result in string format into a variable instead of printing it to the browser.
  • CURLOPT_HTTPHEADER – to set header fields as an array.
  • CURLOPT_POST – set to TRUE to perform HTTP POST.
  • CURLOPT_USERPWD – to set username/password if required to connect to a remote server.

PHP cURL Request

Like PHP, the GET and POST methods are used for sending cURL requests where GET is the default. The CURLOPT_POST constant is used for sending a POST request. And the parameter CURLOPT_POSTFIELDS is for sending the parameter array of the field: value pairs.

cURL Execution

After setting request methods and parameters, the curl_exec() function is used for executing the cURL request.

$result = curl_exec($ch);

This function returns either boolean values or string data based on the value of option CURLOPT_RETURNTRANSFER.

PHP cURL Example

This PHP example is to get the body content from the given remote URL.

<?php
$url = "http://php.net/";
$content = curlRequest($url);
print $content;

function curlRequest($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
?>

Leave a Reply

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

↑ Back to Top