Using cURL to get a page

What is cURL?


cURL is a library that can be used to connect and communicate with different types of servers with different types of protocols. It supports http, https, ftp, gopher, telnet, dict, file, and ldap protocols. It also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user+password authentication.

First of all it needs to initialize;

$ch= curl_init();
then use curl_setopt() for single value and curl_setopt_array() to set multiple options.
then use curl_exec();
in last use curl_close();

How to get a page's content using cURL?


function getURLData( $url ){
        $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
        $options = array(
            CURLOPT_CUSTOMREQUEST  => "GET",         // set request type post or get
            CURLOPT_POST           => false,         // set to GET
            CURLOPT_USERAGENT      => $user_agent,   // set user agent
            CURLOPT_RETURNTRANSFER => true,          // don't output just return web page content
            CURLOPT_SSL_VERIFYPEER => false,         // don't exit on ssl check
            CURLOPT_HEADER         => false,         // don't return headers
            CURLOPT_FOLLOWLOCATION => true,          // follow redirects
            CURLOPT_ENCODING       => "",            // handle all encodings
            CURLOPT_AUTOREFERER    => true,          // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,           // timeout in sec on connect
            CURLOPT_TIMEOUT        => 120,           // timeout in sec on response
            CURLOPT_MAXREDIRS      => 10,            // stop after 10 redirects
        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );//comment if you don't want info about download size,
//       header size, request_size,size_download etc
        curl_close( $ch );

        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;
        $header['content'] = $content;
        return $header;
    }

Usage:-

    $result= getURLData("http://www.google.com/search?q=post+data+with+get+curl+php");
    //print_r($result);
    if ( $result['errno'] != 0 ){
      if ( $result['http_code'] != 200 ){
         echo $page = $result['content'];
      }else echo $result['errmsg'];
   } else echo $result['errmsg'];


How to login using cURL? How to post form data using cURL?



function curlPost($url,$postData){
$ch= curl_init();
curl_setopt_array($ch, array(
   CURLOPT_URL => $url,
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_POST => true,
   CURLOPT_POSTFIELDS => $postData,
   CURLOPT_FOLLOWLOCATION => true,
   CURLOPT_CONNECTTIMEOUT=>30,
   CURLOPT_SSL_VERIFYPEER=>false,
   CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
   CURLOPT_COOKIESESSION => true,
   CURLOPT_COOKIEFILE => 'cookie.txt',
   CURLOPT_COOKIEJAR => 'cookie.txt'
));
$output = curl_exec($ch);
curl_close( $ch );
return $output;
}

Usage:

$postData = array(
   '_method' => 'POST',
   'email' => 'aryan022@gmail.com',
   'password' => 'aryan022',
   'redirect_to' => 'http://localhost/cakephp/account ',
   'testcookie' => '1'
);
$output=curlPost("http://localhost/cakephp/login",$postData);

/*use for subsequest request without passing all postData
  $postData = array();
  $output=curlPost("http://localhost/cakephp/account",$postData);
*/

echo $output;

Post a Comment