问题
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