简体   繁体   中英

Get Facebook meta tags with PHP

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

As per this method you will get key pair array of fabcebook open graph tags.

 $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开放图标签的输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM