getting webpage contents using curl_init not working for some links

Posted by Manish on Stack Overflow See other posts from Stack Overflow or by Manish
Published on 2012-09-26T05:22:53Z Indexed on 2012/10/25 5:02 UTC
Read the original article Hit count: 163

Filed under:
|

I am using this code to get contents of a url entered:-

class MetaTagParser
{
    public $metadata;
    private $html;
    private $url;




    public function __construct($url)
    {
        $this->url=$url;

        $this->html=  $this->file_get_contents_curl();

        $this->set_title();
        $this->set_meta_properties();
    }

    public function file_get_contents_curl()
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }

    public function set_title()
    {
        $doc = new DOMDocument();
        @$doc->loadHTML($this->html);
        $nodes = $doc->getElementsByTagName('title');

        $this->metadata['title'] = $nodes->item(0)->nodeValue;
    }

this class works for some pages but for some url like this one - http://www.dnaindia.com/india/report_in-a-first-upa-govt-tweets-the-press_1745346 when I try to fetch data I get this error:-"Warning: get_meta_tags(http://www.dnaindia.com/india/report_in-a-first-upa-govt-tweets-the-press_1745346): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in C:\xampp\htdocs\prac\index.php on line 52"

it is not working, any ideas why this is happening??

© Stack Overflow or respective owner

Related posts about php

Related posts about curl