Wednesday 13 June 2012

PHP/CURL Tutorial Part-2


Learning PHP with libcurl...


Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up a major parts of its functionality in just four functions.

A typical PHP cURL usage follows the following sequence of steps.
  • curl_initInitializes the session and returns a cURL handle which can be passed to other cURL functions.
  • curl_optThis is the main workhorse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.
  • curl_execExecutes a cURL session.
  • curl_closeCloses the current cURL session.
let's have a small example and understand it better...
<?php
// Example for Simple Get Webpage
$url = "http://www.google.co.in";  // URL to get webpage contents.
$ch = curl_init();  // Initialize a CURL session.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return Page contents.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free up system resources.
echo $result; // Print page contents.
?>

The simple example above simply gets the contents of www.google.co.in and saves it in the variable $result.

Here’s a brief explanation...


$ch = curl_init ();
| Initialize  new curl session and return curl handle.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
| Tell php curl that we want the data returned to us instead of being displayed
curl_setopt ($ch, CURLOPT_URL, $url);
| Tell php curl that this is the url you want to get from internet.
$result = curl_exec ($ch);
| Execute curl and put the output in $result.
$curl_close ($ch);
| close curl resource  and  free system resource





Here is detailed explanation..

$ch = curl_init();
curl_init() — Initialize a cURL session.This is going to initialize the CURL library in your PHP script and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions.You can see we initialize the library and set it to what's called a "handle".  This is where this specific instance is initialized to be stored so that we can work with it. (here $ch is a handle for curl operation). curl_init() function Returns a cURL handle on success, FALSE on errors.
As you can see, curl_setopt is the pivot around which the main cURL functionality revolves. cURL functioning is controlled by way of passing predefined options and values to this function.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
The curl_setopt() function is the way we make our settings in our CURL object. In this case we're setting an option in the handle we created/initialized using curl_init() function, $ch.The option we're setting is CURLOPT_RETURNTRANSFER, what this tells the curl object/handle is to return the transfer to a variable, in this case $result  variable, and not the screen. We want curl function to return webpage content to a variable so that we can do something with output like store it in database or file, regex to parse the data or whatever we want. If we didn't put this option then whole content will be displayed on screen and nothing will be stored in variable. The final parameter pass as TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. If we were to just put false here it'd be the same as not having it at all.

curl_setopt ($ch, CURLOPT_URL, $url);
Use it 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.This option set value to The URL to fetch. This can also be set when initializing a session with curl_init().


$result = curl_exec($ch);
The one line meaning for this function is "run all the stuff we've set" in the lines preceding this and return the data scraped to the variable $result. Hope you will argu like we can do whole this stuff using this one line code. $result = file_get_contents("http://www.google.co.in"); Yes you’re absolutely right. In this example it doesn't make a lot of sense but this is going to allow us to do some things more that we can't do with just file_get_contents() function.

$curl_close ($ch);
Closes a cURL session and frees all resources. The cURL handle, $ch, is also deleted. and it will be no more available in program.
To know more feature of curl just read the next tutorial.

No comments:

Post a Comment