我正在使用此脚本从网站获取og:image标记:
function getFrontImage($url){
$page_content = file_get_contents($url);
$dom_obj = new DOMDocument();
$dom_obj->loadHTML($page_content);
$meta_val = null;
foreach($dom_obj->getElementsByTagName('meta') as $meta) {
if($meta->getAttribute('property')=='og:image'){
$meta_val = $meta->getAttribute('content');
}
}
return $meta_val;
}
但是,这似乎只适用于某些网页。例如,我可以从以下链接获取og:image标记:http://lietuvosdiena.lrytas.lt/aktualijos/2017/06/16/news/partnerystei-nepritare-konservatoriai-sulauke-liberalu-kircio-1702264/
但是我无法从这个链接中得到它:http://sportas.lrytas.lt/krepsinis/2017/06/16/news/martynas-pocius-del-traumu-baigia-karjera-1703843/这很奇怪,因为它们没有任何不同,因为我不知道
答案 0 :(得分:0)
我使用curl解决了我的问题。这是最终的代码:
function getFrontImage($url){
libxml_use_internal_errors(true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0');
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$page_content = curl_exec($ch);
$dom_obj = new DOMDocument();
$dom_obj->loadHTML($page_content);
$meta_val = null;
foreach($dom_obj->getElementsByTagName('meta') as $meta) {
if($meta->getAttribute('property')=='og:image'){
$meta_val = $meta->getAttribute('content');
}
}
return $meta_val;
}