CURL stands for Client URL library functions . Using CURL we can fetch information from other websites.

PHP provides a library called ‘libcurl’ that allows us to connect and communicate to many different types of servers with many different types of protocols.

Currently libcurl supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user+password authentication.

In order to use PHP’s CURL functions we need to install the libcurl package.

To fetch information, CURL initialises a session (using the function curl_init()) with the website with which the information has to be fetched . We can set all the options needed for the information transfer (using the function curl_setopt()). After that we can execute the session (using the function curl_exec()) . The function curl_close() is used to close the curl session.

The following example uses CURL to fetch information from the website to a file.

<?php
$ch
= curl_init("http://www.dictionary.com/");
$fp = fopen("example.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

In the above example, the file ‘example.txt’ will contain the information fetched from the given website.

The constant ‘CURLOPT_FILE’ refers to the file with which the information will be fetched. Also the constant ‘CURLOPT_HEADER’ will be set to true if the header is to be included in the output.