【发布时间】:2019-01-07 02:22:05
【问题描述】:
我正在尝试在我的网站中显示 rss 提要链接,一切顺利,但使用 file_get_contents() 方法获取 og:image 属性需要花费大量时间。还有其他方法可以获取元标记属性吗?
Python 是否有助于更快地获取这些标签?
【问题讨论】:
标签: php zend-framework
我正在尝试在我的网站中显示 rss 提要链接,一切顺利,但使用 file_get_contents() 方法获取 og:image 属性需要花费大量时间。还有其他方法可以获取元标记属性吗?
Python 是否有助于更快地获取这些标签?
【问题讨论】:
标签: php zend-framework
这就是我以前获取所有 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>';
}
【讨论】:
$queryT = '';的用途,如果没有用,应该放弃。
<?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;
?>
【讨论】: