2016-12-14 63 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