[TUTORIAL] cURL for NOOBS. Ultimate guide for cURL, as easy as 1,2,3. Make simple leecher

bloodynoob

Newbie
Joined
Jan 19, 2014
Messages
1
Reaction score
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() ? 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.



  1. Initiate a cURL session.
  2. Using CURLOPT_POSTFIELDS login inside the some_hosting_site/premium
  3. Once logged in, open the files download link some_hosting_site/file/123abc.rar
  4. 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).
  5. Once extracted, save it on your server using CURLOPT_FILE
And enjoyy
 
I also made a PHP Class for cURL that provides a lot of features.

However, you may need some PHP knowledge to understand

Code:
<?php


    class Curl
    {
        //
        public $agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36';
        public $cookie_file_path = "ckk";


        /**
         * @param     $url
         * @param int $show_header
         *
         * @return mixed
         */
        function xg($url, $show_header = 0)
        {


            $ch = curl_init();


            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_HEADER, $show_header);
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file_path);
            $result['result'] = curl_exec($ch);
            $result['info']   = curl_getinfo($ch);
            return $result;
        }


        /**
         * @param       $url
         * @param int   $show_header
         * @param array $proxy_details
         *
         * @return mixed
         * @throws FailedProxy
         */
        function proxy_xg($url, $show_header = 0, $proxy_details = array())
        {


            if (!$proxy_details) {
                return $this->xg($url, $show_header);
            }


            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_HEADER, $show_header);
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_PROXY, $proxy_details['proxyip']);
            curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_details['proxyport']);
            curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '3');
            curl_setopt($ch, CURLOPT_TIMEOUT, '20');


            $result['result'] = curl_exec($ch);
            if (!$result['result']) {
                sleep(3);
                $result['result'] = curl_exec($ch);
                if (!$result['result']) {
                    $result['result'] = 'failed-proxy';
                    throw new FailedProxy("Failed proxy");
                }
            }
            $result['info'] = curl_getinfo($ch);
            return $result;


        }


        /**
         * @param $url
         * @param $referer
         *
         * @return mixed
         */
        function xg_referer($url, $referer)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_REFERER, $referer);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '3');
            curl_setopt($ch, CURLOPT_TIMEOUT, '20');
            $result['result'] = curl_exec($ch);
            $result['info']   = curl_getinfo($ch);
            return $result;
        }


        /**
         * @param       $url
         * @param       $referer
         * @param array $proxy_details
         *
         * @return mixed
         * @throws FailedProxy
         */
        function proxy_xg_referer($url, $referer, $proxy_details = array())
        {
            if (!$proxy_details) {
                return $this->xg_referer($url, $referer);
            }


            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_REFERER, $referer);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_PROXY, $proxy_details['proxyip']);
            curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_details['proxyport']);
            curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '3');
            curl_setopt($ch, CURLOPT_TIMEOUT, '20');


            $result['result'] = curl_exec($ch);
            if (!$result['result']) {
                sleep(3);
                $result['result'] = curl_exec($ch);
                if (!$result['result']) {
                    $result['result'] = 'failed-proxy';
                    throw new FailedProxy("Failed proxy");
                }
            }
            $result['info'] = curl_getinfo($ch);
            return $result;


        }


        /**
         * @param      $url
         * @param      $posts
         * @param null $referer
         *
         * @return mixed
         */
        function xp($url, $posts, $referer = null)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_REFERER, $referer);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
            $result['result'] = curl_exec($ch);
            $result['info']   = curl_getinfo($ch);
            return $result;
        }


        /**
         * @param       $url
         * @param       $posts
         * @param null  $referer
         * @param array $proxy_details
         *
         * @return mixed
         * @throws FailedProxy
         */
        function proxy_xp($url, $posts, $referer = null, $proxy_details = array())
        {


            if (!$proxy_details) {
                return $this->xp($url, $posts, $referer);
            }


            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_REFERER, $referer);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
            curl_setopt($ch, CURLOPT_PROXY, $proxy_details['proxyip']);
            curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_details['proxyport']);
            curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '3');
            curl_setopt($ch, CURLOPT_TIMEOUT, '20');
            $result['result'] = curl_exec($ch);
            if (!$result['result']) {
                sleep(3);
                $result['result'] = curl_exec($ch);
                if (!$result['result']) {
                    $result['result'] = 'failed-proxy';
                    throw new FailedProxy("Failed proxy");
                }
            }
            $result['info'] = curl_getinfo($ch);
            return $result;
        }


        /**
         * @param       $url
         * @param       $posts
         * @param null  $referer
         * @param array $custom_headers
         * @param       $show_header
         *
         * @return mixed
         */
        function xpajax($url, $posts, $referer = null, $custom_headers = array(), $show_header)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, $show_header);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
            curl_setopt($ch, CURLINFO_HEADER_OUT, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_REFERER, $referer);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '3');
            curl_setopt($ch, CURLOPT_TIMEOUT, '20');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
            $result['result'] = curl_exec($ch);
            $result['info']   = curl_getinfo($ch);
            return $result;
        }


        /**
         * @param       $url
         * @param       $posts
         * @param null  $referer
         * @param array $custom_headers
         * @param array $proxy_details
         * @param int   $show_header
         *
         * @return mixed
         * @throws FailedProxy
         */
        function proxy_xpajax($url, $posts, $referer = null, $custom_headers = array(), $proxy_details = array(), $show_header = 0)
        {


            if (!$proxy_details) {
                return $this->xpajax($url, $posts, $referer, $custom_headers, $show_header);
            }


            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, $show_header);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
            curl_setopt($ch, CURLINFO_HEADER_OUT, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_REFERER, $referer);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file_path);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
            curl_setopt($ch, CURLOPT_PROXY, $proxy_details['proxyip']);
            curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_details['proxyport']);
            curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '3');
            curl_setopt($ch, CURLOPT_TIMEOUT, '20');
            $result['result'] = curl_exec($ch);
            if (!$result['result']) {
                sleep(3);
                $result['result'] = curl_exec($ch);
                if (!$result['result']) {
                    $result['result'] = 'failed-proxy';
                    throw new FailedProxy("Failed proxy");
                }
            }
            $result['info'] = curl_getinfo($ch);
            return $result;
        }


    }


?>
 
Back
Top