得到"标题" &安培; "说明"来自外部页面链接

时间:2016-12-14 11:02:46

标签: php facebook curl title

我正在尝试从外部页面链接源获取标题,描述。当我试图获取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 个答案:

答案 0 :(得分: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会使用显示验证码页面。

相关问题