[英]get meta description , title and image from url like facebook link sharing
[英]Get “Title” & “Description” from external page 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都会显示验证码页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.