我想从URL中读取og:image
元标记。我想使用get_meta_tags
,但它不返回此元标记的值。
这是什么原因,我该如何解决?
代码
<?php
$tags = get_meta_tags('https://stackoverflow.com/');
?>
<pre>
<?php
print_r($tags);
?>
</pre>
HTML输入
<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/apple-touch-icon@2.png?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, & 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] => stackoverflow.com
[twitter:title] => Stack Overflow - Where Developers Learn, Share, & Build Careers
[twitter:description] => Stack Overflow | The World’s Largest Online Community for Developers
)
答案 0 :(得分:2)
这行不通。
get_meta_tags
使用meta标签的name
属性填充数组键。
如果您查看试图从中获取此值的页面的HTML,则会看到以下内容:
<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/apple-touch-icon@2.png?v=73d79a89bded" />
如您所见,这些标签都不具有name
属性,因此该函数无法将这些值分配给任何键。
您将不得不使用其他方法来获取这些元标记。