从外部页面获取 "Title" & "Description" link
Get "Title" & "Description" from external page link
我正在尝试从外部页面 link 来源获取标题和描述。当我尝试获取 Facebook 页面源代码并返回其他页面的源代码时,这不起作用。它在 google 等其他网站上运行。这是我在 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();
}
我不明白我面临的问题是什么。有人可以建议吗?提前致谢。
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 会显示验证码页面。
我正在尝试从外部页面 link 来源获取标题和描述。当我尝试获取 Facebook 页面源代码并返回其他页面的源代码时,这不起作用。它在 google 等其他网站上运行。这是我在 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();
}
我不明白我面临的问题是什么。有人可以建议吗?提前致谢。
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 会显示验证码页面。