cURL: An Introduction, Installation and Use

cURL is a way to transfer data from any host via CLI. It supports various internal mechanisms of protocols like HTTP, HTTPS etc

cURL: An Introduction, Installation and Use

cURL: Canonical URL is a way to transfer data from any host via CLI

cURL supports following  protocols DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP.

cURL also supports various internal techniques of above mentioned protocols like HTTP GET, HTTP POST, HTTPS etc.

It is a command line tool to perform above mentioned task.

Examples


Open URL time2hack.com by cURL:

curl time2hack.com

Open URL time2hack.com by cURL and save the output to a file

curl -o index.html time2hack.com

Open URL time2hack.com by cURL and save the output to a file as the server has

curl -O index.html time2hack.com

But its main use does not rely on its CLI use. The main hack is the use of  libcurl in various programming languages to offer data transfer between server and extensively use it in applications.

Here I take the example of PHP to use the cURL. PHP supports some handy internal functions to support the cURLs provided the web server supports the libcurl.

Check

First of all check its support by following custom function. This function returns TRUE if it can find the cURL extension installed, and FALSE if it fails to find it.

// Checks for presence of the cURL extension.
function _iscurlinstalled() {  
  if (in_array ('curl', get_loaded_extensions())) {  
    return true;  
  } else {
    return false;
  }
}

Sample usage:

if (_iscurlinstalled())   
 echo "cURL is installed";   
else   
 echo "cURL is NOT installed";

Install

If it is not enabled; enable it by following ways

If working on Windows

  • Open php.ini and find the string curl.
  • And you will see a line like extension=php_curl.dll
  • Make sure you remove ; from the beginning of line.
  • Now restart your web server.

If working on Linux

Open up CLI and type

##For debian based linux distros like Ubuntu, Debian, Backtrack etc  
$ sudo apt-get install php5-curl

##For rpm based linux distros like Red Hat, Fedora, CentOS etc  
$ yum install php5-curl

Now restart your web server.


Use

Now you are ready to use the cURL in your scripts. Following PHP code makes use of cURL.

$url = "https://time2hack.com/";  
$ch = curl_init($url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
echo $curl_scraped_page = curl_exec($ch);  
curl_close($ch);

Make use of it in building interactive apps. many languages support the cURL. Few links to seek more help: