what is curl and why we use it



Benefits of using curl

CURL is a tool for transferring files and data with URL syntax,
supporting many protocols including HTTP, FTP, TELNET and more.
Initially, cURL was designed to be a command line tool. Lucky for us,
the cURL library is also supported by PHP. In this article, we will look at
some of the advanced features of cURL,
and how we can use them in our PHP scripts.
curl example
$content = file_get_contents(“http://www.websolutionsdone.com”);
// or
$lines = file(“http://www.websolutionsdone.com”);
// or
basic strucute for curl
Before we move on to more complicated examples, let’s review the basic structure of a cURL request in PHP. There are four main steps:
Set Options
Execute and Fetch Result
Free up the cURL handle
curl example:
/ 1. initialize
$ch = curl_init();
// 2. set the options, including the url
curl_setopt($ch, CURLOPT_URL, “http://www.nettuts.com”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// 3. execute and fetch the resulting HTML output
$output = curl_exec($ch);
// 4. free up the curl handle
Getting Information
Another optional step is to get information about the cURL request, after it has been executed.
// …
$info = curl_getinfo($ch);
echo ‘Took ‘ . $info[‘total_time’] . ‘ seconds for url ‘ . $info[‘url’];
Following information is included in the returned array:
Multi cURL
One of the more advanced features of cURL is the ability to create a “multi” cURL handle. This allows you to open connections to multiple URLs simultaneously and asynchronously.
On a regular cURL request, the script execution stops and waits for the URL request to finish before it can continue. If you intend to hit multiple URLs, this can take a long time, as you can only request one URL at a time. We can overcome this limitation by using the multi handle.
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, “http://lxr.php.net/”);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, “http://www.php.net/”);
curl_setopt($ch2, CURLOPT_HEADER, 0);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the two handles
$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
The idea is that you can open multiple cURL handles and assign them to a single multi handle. Then you can wait for them to finish executing while in a loop.
There are two main loops in this example. The first do-while loop repeatedly calls curl_multi_exec(). This function is non-blocking. It executes as little as possible and returns a status value. As long as the returned value is the constant ‘CURLM_CALL_MULTI_PERFORM’, it means that there is still more immediate work to do (for example, sending http headers to the URLs.) That’s why we keep calling it until the return value is something else.
In the following while loop, we continue as long as the $active variable is ‘true’. This was passed as the second argument to the curl_multi_exec() call. It is set to ‘true’ as long as there are active connections withing the multi handle. Next thing we do is to call curl_multi_select(). This function is ‘blocking’ until there is any connection activity, such as receiving a response. When that happens, we go into yet another do-while loop to continue executing.

Leave A Reply

Your email address will not be published.