2016-12-14 64 views
0

我想從外部頁面鏈接源獲取標題,說明。當我嘗試獲取Facebook頁面源並正在返回某個其他頁面的源代碼時,這不起作用。它工作在其他網站像谷歌等。這裏是我的PHP代碼:從外部頁面鏈接獲取「標題」和「描述」

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 

public function previewLink(){ 
    $url = "https://www.facebook.com/NASA/"; 
    $html = $this->file_get_contents_curl($url); 
    $title = ""; 
    $description =""; 
    $image = ""; 

    //parsing begins here: 
    $doc = new \DOMDocument(); 
    @$doc->loadHTML($html); 
    $nodes = $doc->getElementsByTagName('title'); 
    $title = $nodes->item(0)->nodeValue(); 
    } 

我沒有得到什麼,我所面臨的問題。有人能提出一些建議嗎提前致謝。

回答

1

Facebook在http請求中需要UserAgent字符串。您可以使用此添加

curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12')); 

供參考:facebook用於顯示驗證碼頁面,當任何人沒有登錄頁面進入頁面。

+0

謝謝。它正在工作。 – Ishan