cURL in PHP

cURL is one of the most powerful PHP extensions. It stands for Client URL, and allows a user to communicate with other servers using a wide range of protocols.

The libcurl is the library behind the PHP cURL extension that currently supports a wide range of protocols, including HTTP, HTTPS, FTP, TELNET, FILE, LDAP, DICT, GOPHER and HTTPS, as well as HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user: password authentication.

There are alternatives for some of these, such as streams, sockets or the FTP extension, but cURL is the master of them all – they are less flexible.

Using cURL a user can:

  • Implement payment gateways’ payment notification scripts.
  • Download and upload files from remote servers.
  • Login to other websites and access members only sections.

PHP cURL library is definitely the odd man out. Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up a major part of its functionality in just four functions.

A typical PHP cURL usage follows the following sequence of steps:

  • curl_init – It initializes the session and returns a cURL handle which can be passed to other cURL functions.
  • curl_opt – It is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.
  • curl_exec – It executes a cURL session.
  • curl_close – It closes the current cURL session.

Example

Here, we will explain an example how to download file or web page using PHP cURL This PHP code uses cURL to download Google’s RSS feed.

<notextile>
/**
* Initialize the cURL session
*/
$ch = curl_init();</p>
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL, ‘http://news.google.com/news?hl=en&topic=t& output=rss’);

/**
* Ask cURL to return the contents in a variable instead of simply echoing them to the browser.
*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

/**
* Execute the cURL session
*/
$contents = curl_exec ($ch);

/**
* Close cURL session
*/
curl_close ($ch);
<p></notextile>

In the above code you have seen two different options as been used. Let’s see a brief explanation of them:

  • CURLOPT_URL: This is used to specify the URL which you want to process. This could be the URL of the file you want to download or it could be the URL of the script to which you want to post some data.
  • CURLOPT_RETURNTRANSFER: This option is used to set 1 which will cause the curl_exec function to return the contents instead of echoing them to the browser.

Example

This code returns the curl output as a string.

<?php
// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, “example.com”);

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);
?>

There are number of cURL Functions present in PHP:

    • curl_close – This function is used to close a cURL session.

 

    • curl_copy_handle – This function is used to copy a cURL handle along with all of its preferences.

 

    • curl_errno – This function is used to return the last error number.

 

    • curl_error – This function is used to return a string containing the last error for the current session.

 

    • curl_escape – This function uses URL to encode the given string.

 

    • curl_exec – This function is used to perform a cURL session.

 

    • curl_file_create – This function is used to create a CURLFile object.

 

    • curl_getinfo – This function is used to get information regarding a specific transfer.

 

    • curl_init – This function is used to initialize a cURL session.

 

    • curl_multi_add_handle – This function is used to add a normal cURL handle to a cURL multi handle.

 

    • curl_multi_close – This function is used to close a set of cURL handles.

 

    • curl_multi_exec – This function is used to run the sub-connections of the current cURL handle.

 

    • curl_multi_getcontent – This function is used to return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set.

 

    • curl_multi_info_read – This function is used to get information about the current transfers.

 

    • curl_multi_init – This function is used to returns a new cURL multi handle.

 

    • curl_multi_remove_handle – This function is used to remove a multi handle from a set of cURL handles.

 

    • curl_multi_select – This function is used to wait for activity on any curl_multi connection.

 

    • curl_multi_setopt – This function is used to set an option for the cURL multi handle.

 

    • curl_multi_strerror – This function is used to return string describing error code.

 

    • curl_pause – This function is used to pause and unpause a connection.

 

    • curl_reset – This function is used to reset all options of a libcurl session handle.

 

    • curl_setopt_array – This function is used to set multiple options for a cURL transfer.

 

    • curl_setopt – This function is used to set an option for a cURL transfer.

 

    • curl_share_close – This function is used to Close a cURL share handle.

 

    • curl_share_init – This function is used to initialize a cURL share handle.

 

    • curl_share_setopt – This function is used to set an option for a cURL share handle.

 

    • curl_strerror – This function is used to return string describing the given error code.

 

    • curl_unescape – This function is used to decode the given URL encoded string.

 

  • curl_version – This function is used to get cURL version information.