Simon Thomsen Simon Thomsen - 5 months ago 82
PHP Question

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

$tags = get_meta_tags('');
echo $tags['og:image'];

But I would rather suggest using DOMDocument anyways:

$sites_html = file_get_contents('');

$html = new DOMDocument();
$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
        //Assign the value from content attribute to $meta_og_img
        $meta_og_img = $meta->getAttribute('content');
echo $meta_og_img;

Hope it helps