Getting Page Contents Using PHP Curl

Hello All,

Here is  a code for getting a web page contents using curl.

function getPageContents( $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 for all http requests
            CURLOPT_COOKIEFILE     =>"cookie.txt", //set cookie file
            CURLOPT_COOKIEJAR      =>"cookie.txt", //set cookie jar
            CURLOPT_RETURNTRANSFER => true,     // return web page
            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 on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        CURLOPT_SSL_VERIFYPEER    => false    // Bypassing ssl
        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $details  = curl_getinfo( $ch );
        curl_close( $ch );

        $details['errno']   = $err;
        $details['errmsg']  = $errmsg;
        $details['content'] = $content;
        return $details;
    }
    // setting web url here
    $url = 'https://mobile.twitter.com/session/new';
    // getting all page html contents  here
    $resp = getPageContents($url);
  
    // checking error
    if ( $resp['errno'] != 0 )
        exit("Bad Url");
    if ( $resp['http_code'] != 200 )
        exit("No Permission");
    $content =  $resp['content'];
        echo $content;

Comments

Popular Posts