`get_meta_tags` 没读 og:image

`get_meta_tags` didn't read og:image

我想从 URL 读取 og:image 元标记。我想使用 get_meta_tags,但它没有 return 此元标记的值。

这是什么原因,我该如何解决?

代码

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

HTML 输入

 <meta property="og:type" content= "website" />
 <meta property="og:url" content="https://whosebug.com/"/>
 <meta property="og:site_name" content="Stack Overflow" />
 <meta property="og:image" itemprop="image primaryImageOfPage" content="https://cdn.sstatic.net/Sites/Whosebug/img/apple-touch-icon@2.png?v=73d79a89bded" />
 <meta name="twitter:card" content="summary"/>
 <meta name="twitter:domain" content="whosebug.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" />

结果

   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] => whosebug.com
    [twitter:title] => Stack Overflow - Where Developers Learn, Share, & Build Careers
    [twitter:description] => Stack Overflow | The World’s Largest Online Community for Developers
)

这行不通。

get_meta_tags 使用元标记的 name 属性填充数组键。

如果您检查您试图从中获取此值的页面的 HTML,您会看到:

 <meta property="og:type" content= "website" />
 <meta property="og:url" content="https://whosebug.com/"/>
 <meta property="og:site_name" content="Stack Overflow" />
 <meta property="og:image" itemprop="image primaryImageOfPage" content="https://cdn.sstatic.net/Sites/Whosebug/img/apple-touch-icon@2.png?v=73d79a89bded" />

如您所见,其中 none 个标签具有 name 属性,因此该函数不可能将这些值分配给任何键。

您将不得不使用不同的方法来获取这些元标记。