问题描述
我正在尝试改编我在这里找到的一些PHP代码,以使其从任意网址获取og:image网址, 标签通常像这样(当一个标签不存在时,我会创建一个isset)
<meta property="og:image" content="https://MYGREATOTHERDOMAIN.com/images/rock.jpg" />
或者可能是
<meta name="og:image" content="https://images.MYGREATDOMAIN.co.uk/img.jpg"/>
我正在使用的代码。
$htmlContent = file_get_contents($singlemeal[0][link]);
// read all og:image tags into an array
preg_match_all('/<meta[^>]+>/i',$htmlContent,$imgTags);
for ($i = 0; $i < count($imgTags[0]); $i++) {
// get the source string
preg_match('/type=og:image',$imgTags[0][$i],$imgage);
preg_match('/content="([^"]+)/i',$imgage);
// remove opening 'content=' tag,can`t get the regex right
$origImageSrc[] = str_ireplace( 'content="','',$imgage[0]);
}
// will output all your img src's within the html string
echo "<pre>";
print_r ($origImageSrc);
echo "</pre>";
这现在提取所有在meta标记中找到的所有“内容”,我只需要搜索并返回og:image url,但不确定如何做到这一点,对您的帮助将不胜感激。
编辑以添加:我可能不需要数组,因为我将仅返回1个输出url,我只是从示例代码开始,认为这是一个开始和适应的好地方,但遇到了麻烦。再次感谢。
如果我们对https://www.bbcgoodfood.com/recipes/classic-sausage-mash进行上述操作,则输出的即时消息将是..
Array
(
[0] => width=device-width
[1] =>
[2] => Classic sausage & mash recipe - BBC Good Food
[3] => A classic family comfort food meal with creamy mash and golden brown sausages. Fresh seasonal veg like steamed broccoli make this meal 3 of your 5 a day
[4] => https://www.bbcgoodfood.com/recipes/classic-sausage-mash
[5] => Classic sausage & mash
[6] => BBC Good Food
[7] => A classic family comfort food meal with creamy mash and golden brown sausages. Fresh seasonal veg like steamed broccoli make this meal 3 of your 5 a day
[8] => article
[9] => https://images.immediate.co.uk/production/volatile/sites/30/2020/08/sausage-and-mash-2cb0bee.jpg
[10] => Classic sausage & mash
[11] => 400
[12] => 440
[13] => summary_large_image
[14] => BBC Good Food
[15] => @bbcgoodfood
[16] => Hkgeqt1fifrCaIrdMvoMK4wxVtQsqQPh3VY77hxXjWg
[17] => e0dc3ea18b52d6be8f7d588b29af3605
[18] => 31296838546
[19] => 37
)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)