Get Facebook Meta Tags with PHP

I am trying to get Facebook meta tags from my HTML.

I use simple html dom to get all html data from the site. I tried with preg_replace, but with no luck.

For example, I want to get the contents of this fb meta tag:

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

Hope someone can help! :-)

+2
source share
2 answers

I was going to suggest get_meta_tags () , but it doesn't seem to work (for me): s

<?php
$tags = get_meta_tags('http://www.example.com/');
echo $tags['og:image'];
?>

But I would prefer to use DOMDocument anyway:

<?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 this helps

+21
source

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];
    }

Output of facebook open graph tags

+1

Source: https://habr.com/ru/post/1621064/


All Articles