`get_meta_tags` didn't read og:image

I want to read the og:image meta tag from a URL. I want to use get_meta_tags, but it doesn't return the value for this meta tag.

What is the reason for this and how can I solve it?

Code

<?php
$tags = get_meta_tags('https://stackoverflow.com/');
?>
<pre>
    <?php
        print_r($tags);
    ?>
</pre>

HTML Input

 <meta property="og:type" content= "website" />
 <meta property="og:url" content="https://stackoverflow.com/"/>
 <meta property="og:site_name" content="Stack Overflow" />
 <meta property="og:image" itemprop="image primaryImageOfPage" content="https://cdn.sstatic.net/Sites/stackoverflow/img/[email protected]?v=73d79a89bded" />
 <meta name="twitter:card" content="summary"/>
 <meta name="twitter:domain" content="stackoverflow.com"/>
 <meta name="twitter:title" property="og:title" itemprop="title name" content="Stack Overflow - Where Developers Learn, Share, &amp; Build Careers" />
 <meta name="twitter:description" property="og:description" itemprop="description" content="Stack Overflow | The World’s Largest Online Community for Developers" />

Result

   Array
(
    [description] => Stack Overflow is the largest, most trusted online community for developers to learn, share​ ​their programming ​knowledge, and build their careers.
    [viewport] => width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0
    [twitter:card] => summary
    [twitter:domain] => stackoverflow.com
    [twitter:title] => Stack Overflow - Where Developers Learn, Share, & Build Careers
    [twitter:description] => Stack Overflow | The World’s Largest Online Community for Developers
)

Upvotes: 1

Views: 807

Answers (1)

yivi
yivi

Reputation: 47370

This won't work.

get_meta_tags uses the name attribute of the meta tags to populate the array keys.

If you check the HTML of the page you are trying to get this values from, you'll see this:

 <meta property="og:type" content= "website" />
 <meta property="og:url" content="https://stackoverflow.com/"/>
 <meta property="og:site_name" content="Stack Overflow" />
 <meta property="og:image" itemprop="image primaryImageOfPage" content="https://cdn.sstatic.net/Sites/stackoverflow/img/[email protected]?v=73d79a89bded" />

As you can see, none of those tags have a name attribute, so it's impossible for the function to assign those values to any key.

You will have to use a different method to get these meta tags.

Upvotes: 2

Related Questions