bloodynoob
Newbie
- Jan 19, 2014
- 1
- 3
Hi, I wrote article in 2009, and I find it equally relevant even as of date. I will explain How to use cuRL. I won't give any definitions or notes. I will just tell what is actually required to be known. We want to code, we do not want to fill our answer sheets on writing thesis about cURL.
So let's start.
There are 4 main tags/functions of curl without which the curl can?t work. Namely they are:
Curl_init
This function starts the curl session. When run successfully you get the result through curl_exec().
Eg:
Curl_setopt
This includes all the transferring of the curl session. This is the tough part about curl as it has more than 90 functions (parameters, which result in a different execution of the function CURL_SETOPT) in it and the good part about these 90+ functions is that they all begin with CURLOPT_AND_REST_FOLLOWS_HERE. To be a curl expert you need to know all of them. Curl_setopt has 3 values in it they are:
curl_setopt ( the curl_init part , The 90+ functions , The value to be set on the function )
I won?t be able to explain the 90+ functions so I will explain the basic functions.
CURLOPT_URL
This is the basic function of curl_setopt().It contains the value of the url to fetch. It is used when curl_init is left blank that means it can be used to store the link.
Eg:
CURLOPT_RETURNTRANSFER
This function returns the content when the value is set to 1 else it will print/echo it (when set to 0).This is a very important function and can be used for extracting values out of the page (although fopen and file are much simpler alternatives)
Eg:
CURLOPT_FOLLOWLOCATION
This function allows curl to redirect. Suppose you live in India and throughlocalhost by curl you try to open google.com it will show The document has moved here or the so called 301 Moved Permanently and here will contain the link/href of google.co.in.To move this 301 Moved Permanently away we have CURLOPT_FOLLOWLOCATION function which allows curl to redirect.
Eg:
CURLOPT_FILE
This is a very simple function it allows the user to save the contents of the site so there is no need for any explanation. But for this first you need to open the file using fopen() parameter.
Eg:
CURL_POSTFIELDS
This is one of the reasons why I learned cURL, it is used forusing posting on a form through cURL, loggining on sites thorough cURL andvarious other purposes or passing values through POST. This works on a simple formula or whatsoever like thename of the input type is usernameand the name of the second input is passwordso to post content you will type username=your_value_for_this_input&password=your_value_for_the_second_input.
Application of cURL just learnt!
If there is a file hosting site that requires a premium account to download a file (some_hosting_site/file/123abc.rar) in a single click and you have the premium account.
So let's start.
There are 4 main tags/functions of curl without which the curl can?t work. Namely they are:
- curl_init() ? It starts the curl session.
- curl_setopt() ? This includes all the transferring of the curl session. Its like the motherboard of the session.
- curl_exec() ? This executes your session.
- curl_close() ? This closes the session.
Curl_init
This function starts the curl session. When run successfully you get the result through curl_exec().
Eg:
PHP:
<?php
$ch = curl_init('somelink'); //start the session
curl_exec($ch); //execute the session
curl_close($ch); //close the session
?>
Curl_setopt
This includes all the transferring of the curl session. This is the tough part about curl as it has more than 90 functions (parameters, which result in a different execution of the function CURL_SETOPT) in it and the good part about these 90+ functions is that they all begin with CURLOPT_AND_REST_FOLLOWS_HERE. To be a curl expert you need to know all of them. Curl_setopt has 3 values in it they are:
curl_setopt ( the curl_init part , The 90+ functions , The value to be set on the function )
I won?t be able to explain the 90+ functions so I will explain the basic functions.
CURLOPT_URL
This is the basic function of curl_setopt().It contains the value of the url to fetch. It is used when curl_init is left blank that means it can be used to store the link.
Eg:
PHP:
<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "somelink");
curl_exec($ch); //execute the session
curl_close($ch); //close the session
?>
CURLOPT_RETURNTRANSFER
This function returns the content when the value is set to 1 else it will print/echo it (when set to 0).This is a very important function and can be used for extracting values out of the page (although fopen and file are much simpler alternatives)
Eg:
PHP:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'somelink');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content=curl_exec($ch);
curl_close($ch);
echo $content;
?>
CURLOPT_FOLLOWLOCATION
This function allows curl to redirect. Suppose you live in India and throughlocalhost by curl you try to open google.com it will show The document has moved here or the so called 301 Moved Permanently and here will contain the link/href of google.co.in.To move this 301 Moved Permanently away we have CURLOPT_FOLLOWLOCATION function which allows curl to redirect.
Eg:
PHP:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'somelink');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
?>
CURLOPT_FILE
This is a very simple function it allows the user to save the contents of the site so there is no need for any explanation. But for this first you need to open the file using fopen() parameter.
Eg:
PHP:
<?php
$file=fopen('file.txt','a+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'somelink');
curl_setopt($ch,CURLOPT_FILE,$file);
curl_exec($ch);
curl_close($ch);
?>
CURL_POSTFIELDS
This is one of the reasons why I learned cURL, it is used forusing posting on a form through cURL, loggining on sites thorough cURL andvarious other purposes or passing values through POST. This works on a simple formula or whatsoever like thename of the input type is usernameand the name of the second input is passwordso to post content you will type username=your_value_for_this_input&password=your_value_for_the_second_input.
PHP:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"somelink/login.php");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=bloodynoob&password=this_is_my_password_for_BHW");
$content=curl_exec ($ch);
curl_close ($ch);
echo $content;
?>
Application of cURL just learnt!
If there is a file hosting site that requires a premium account to download a file (some_hosting_site/file/123abc.rar) in a single click and you have the premium account.
- Initiate a cURL session.
- Using CURLOPT_POSTFIELDS login inside the some_hosting_site/premium
- Once logged in, open the files download link some_hosting_site/file/123abc.rar
- Once you open it, you will be able to see the generated link, using CURLOPT_RETURNTRANSFER , simply extract the link using simple Regex (preg_match).
- Once extracted, save it on your server using CURLOPT_FILE