what is the best way to get og:image meta property [duplicate]

為{幸葍}努か 提交于 2020-08-22 04:45:27

问题


I am trying to show rss feed links in my website, All going well but its taking so much time to get og:image property by using file_get_contents() method. Is there any other way to fetch meta tag properties?

Is Python helpful to get these tags faster ?


回答1:


This is how I used to get all the og:tags:

libxml_use_internal_errors(true);
$doc = new DomDocument();
$doc->loadHTML(file_get_contents($url));
$xpath = new DOMXPath($doc);
$query = '//*/meta[starts-with(@property, \'og:\')]';
$metas = $xpath->query($query);
foreach ($metas as $meta) {
    $property = $meta->getAttribute('property');
    $content = $meta->getAttribute('content');
    echo '<h1>Meta '.$property.' <span>'.$content.'</span></h1>';
}



回答2:


<?php
$page_content = file_get_contents('http://example.com');

$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');
}
}
echo $meta_val;
?>


来源:https://stackoverflow.com/questions/22012922/what-is-the-best-way-to-get-ogimage-meta-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!