2012-09-26 44 views
-1

工作我使用這個代碼來獲得一個URL的內容進入: -讓網頁內容不適用於某些鏈接

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; 
    } 

這個類適用於某些網頁,但對於像這樣的一些網址 - http://www.dnaindia.com/india/report_in-a-first-upa-govt-tweets-the-press_1745346 當我嘗試獲取數據我得到這個錯誤: - 「警告:get_meta_tags(http://www.dnaindia.com/india/report_in-a-first-upa-govt-tweets-the-press_1745346):未能打開流:HTTP請求失敗!HTTP/1.1 403第52行禁止C:\ xampp \ htdocs \ prac \ index.php「

它不工作,爲什麼t他正在發生?

+1

該網站不喜歡你刮。 –

+0

但是當這個鏈接在Facebook上發佈時,它很容易從網頁中提取內容.... – Manish

回答

1

有時候,網站管理員不傻,知道如何保護頁面從啜和打水所以你要欺騙他的保護和現在的用戶代理等從正常的瀏覽器的到來。添加此行:

CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1", 
相關問題