用PHP获取Facebook元标记 [英] Get Facebook meta tags with PHP
本文介绍了用PHP获取Facebook元标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用简单的html dom来获取网站上的所有html数据。
我已经尝试过preg_replace,但没有运气。
我想要获取这个fb元标记的内容:
< meta content =IMAGE URLproperty =og:image/>
希望有人可以帮助! : - )
解决方案
我建议使用 get_meta_tags(),但似乎不起作用(对我来说):s
<?php
$ tags = get_meta_tags('http://www.example.com/');
echo $ tags ['og:image'];
?>
但我宁愿建议使用 DOMDocument 反正:
<?php
$ sites_html = file_get_contents('http://example.com');
$ html = new DOMDocument();
@ $ html-> loadHTML($ sites_html);
$ meta_og_img = null;
//获取所有元标记并循环遍历它们。
foreach($ html-> getElementsByTagName('meta')as $ meta){
//如果元标记的属性为og:image
if($ meta-> ; getAttribute('property')=='og:image'){
//将值从content属性分配给$ meta_og_img
$ meta_og_img = $ meta-> getAttribute('content');
}
}
echo $ meta_og_img;
?>
希望有帮助
I'm trying to get Facebook's meta tags from my HTML.
I'm using simple html dom to get all html data from the site. I've tried with preg_replace, but without luck.
I want for example to get the content of this fb meta tag:
<meta content="IMAGE URL" property="og:image" />
Hope someone can help! :-)
解决方案
I Was going to suggest to use get_meta_tags() but it seems to not work (for me) :s
<?php
$tags = get_meta_tags('http://www.example.com/');
echo $tags['og:image'];
?>
But I would rather suggest using DOMDocument anyways:
<?php
$sites_html = file_get_contents('http://example.com');
$html = new DOMDocument();
@$html->loadHTML($sites_html);
$meta_og_img = null;
//Get all meta tags and loop through them.
foreach($html->getElementsByTagName('meta') as $meta) {
//If the property attribute of the meta tag is og:image
if($meta->getAttribute('property')=='og:image'){
//Assign the value from content attribute to $meta_og_img
$meta_og_img = $meta->getAttribute('content');
}
}
echo $meta_og_img;
?>
Hope it helps
这篇关于用PHP获取Facebook元标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文