4

我正在尝试从我的 HTML 中获取 Facebook 的元标记。

我正在使用简单的 html dom 从站点获取所有 html 数据。我试过 preg_replace,但没有运气。

例如,我想获取此 fb 元标记的内容:

<meta content="IMAGE URL" property="og:image" />

希望有人能帮忙!:-)

4

2 回答 2

23

我打算建议使用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;
//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;
?>

希望能帮助到你

于 2012-08-17T23:55:17.020 回答
1

按照这种方法,您将获得 fabcebook 开放图形标签的密钥对数组。

 $url="http://fbcpictures.in";
 $site_html=  file_get_contents($url);
    $matches=null;
    preg_match_all('~<\s*meta\s+property="(og:[^"]+)"\s+content="([^"]*)~i',     $site_html,$matches);
    $ogtags=array();
    for($i=0;$i<count($matches[1]);$i++)
    {
        $ogtags[$matches[1][$i]]=$matches[2][$i];
    }

facebook 开放图标签的输出

于 2015-03-01T10:42:47.910 回答