What is cURL and how to download via curl
PHP/cURL is a library that helps you to make HTTP requests in PHP using libcurl.
PHP supports libcurl, a library created by Daniel Stenberg.
In order to use this functions you need to install the libcurl
To understand we will have to know about following terms
cURL is a command line tool for receiving or sending files using URL syntax
libcurl is a library that allows us to connect and communicate to different types of servers with different types of protocols.
It is supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols.sit i also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies.
It helps the PHP programms to make use of libcurl to make HTTP request .
Now you have an idea of the different terms used.
Basically the following steps are need to follow to use in php.
- Initialize a session
- Set options for the session
- Execute the action or get/send data from/to server
- Close the session
Example: To fetch the abc.com homepage
- Initialize a curl session
$ch = curl_init(“http://www.abc.com/”);
- setting options
$fp = fopen(“abc.txt”, “w”); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0);
curl_close($ch); fclose($fp); ?>
We can do more task with curl like curl_error() , curl_setopt(), curl_getinfo(), curl_version() etc.
- curl_error(): to check the errors based on the return value(bool) of the curl_exec().
- curl_setopt() : Set an option for a transfer.
- curl_getinfo() : Get information regarding a specific transfer
- curl_version() : Gets version information
There are lots of options are available. You can take a look at the manual for more and detailed information.
Thank you for reading this article,
If any thing is wrong in the article please do let us know in the comment section.
We will be very happy to hear that.