PHP login/download file - CURL libraries

October 18, 2007

PHP is a great language though for small developers. It offers a lot in the easiest way possible. Is not hard to handle forms cookies or whatever. You can easily submit a form to a server via POT, being able to build scripts that can be able to login to protected areas. That’s where the CURL library comes.

CURL means “Client for URL”. There are two different libraries cURL and libcurl. The cURL libraries are used for transferring files to/from web server.

The cURL library make a request to the web server, receive data, send data and then receive the result information. Look at the below example:

$url = 'http://www.assistprogramming.com'; // the webpage from which we take the content

$curl_handler = curl_init(); // init the cURL session.

curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1); // return the page content.

curl_setopt($curl_handler, CURLOPT_URL, $url); // send the URL as a parameter.

$result = curl_exec($curl_handler); // transmit the URL to a variable.

curl_close($curl_handler); // close the cURL session and save the system resource

echo $result; //show the page content

Used function:
curl_init() – init the curl session
curl_setopt() – set an option for a cURL session identified by the $curl_handler parameter
curl_exec() – this function should be called after the initialization of the curl session and all option for the session are set.

One big advantage of the curl libraries is transferring data through a from. As we know there are two types of forms: those who send parameters using GET and those who send parameters using POST.

For forms which uses GET it is quite simple. With a server-side language should be settled the sent parameters, then concatenated the get string with the given URL.

Example

$url = 'http://www.assistprogramming.com/';
$fields = 'tutorialDisplay.php?id=40&submit=yes';
$url = $url . $fields;
$curl_handler = curl_init();
curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handler, CURLOPT_URL, $url);
$result = curl_exec($curl_handler); .
curl_close($curl_handler);

echo $result;

We have only one sent parameter and that is “id”. The submit parameters represent the action.

If we want to use curl for sending data thorough POST, the parameters should be in a string (name=value). This POST uses the content type application/x-www-form-urlencoded, so data sent should be encoded.

$url = 'http://www.assistprogramming.com/';
$fields = 'tutorialDisplay.php?id=40&submit=yes';
$url = $url;
$ curl_handler = curl_init();
curl_setopt($curl_handler, CURLOPT_URL, $url);
curl_setopt($curl_handler, CURLOPT_POST, 1); //specifies how we send data

curl_setopt($curl_handler, CURLOPT_POST_FIELDS, $fields); //specifies  the parameters sent through POST

curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl_handler); .

curl_close($curl_handler);

echo $result;

Downloading an image or a binary content file

Using the cURL library we can download binary content file or even images. Google use this technique too.

$url = 'http://www.assistprogramming/image.jpg';
$curl_handler = curl_init();
curl_setopt($curl_handler, CURLOPT_URL, $url);
curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handler, CURLOPT_BINARYTRANSFER, 1); //return the transfer in a binary format .

$data = curl_exec($curl_handler);

curl_close($curl_handler);

header('Content-type: image/jpeg'); //set the image header

echo $data; // show the image

Identification

Many times, identification in a HTML form is implemented using plain/text field

Example:

$url = ' http://www.assistprogramming.com';
$post_fields = 'username:password';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_ USERPWD, $post_fields); //identification through POST
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl_handle);

curl_close($curl_handle);

echo $result;

Cookies

Browser perform a server-side control through cookies. The location of the cookies is on client server.

$cookie_file_path = ‘D:/wwwroot/cookie/cook’; // this file should have CHMOD 777(Read / Write rights).
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); // the name of the file which contains information about cookie. The cookie file can be in a Netscape format, or only HTTP headers.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); // the file name for saving all the internal cookies until we close the connexion .

If we want to find the location we can use the php function realpath($file).

<?php
realpath('test.php');
?>

<?php$url = 'http://www.example.com/login.php'; // URL$post_fields = 'name=admin&password=guest&submit=save';

$reffer = 'http://site.com/index.php';

$agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4)

Gecko/20030624 Netscape/7.1 (ax)';

$cookie_file_path = 'C://wwwroot/test/cookie/cook';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_USERAGENT, $agent); // the content of the User-Agent header for a request HTTP

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POST_FIELDS,$POST_FIELDS);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // TRUE for a string value return by curl_exec()

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // TRUE to follow any header  'Location: '

curl_setopt($ch, CURLOPT_REFERER, $reffer); //the content of the 'Referer: 'header which will be used in the HTTP request.

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); //the name of the file which contains information about cookie

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); //the name of the file where are saved all the cookies when the session is closed.

$result = curl_exec($ch); // put the URL intp a variable.

curl_close($ch);

echo $result;

?>

 

Post a comment

Name (required)

Mail (will not be published) (required)

Website

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word