微信分享链接显示缩略图和描述php代码
时间: 2023-12-04 11:00:46 浏览: 41
要实现微信分享链接显示缩略图和描述,可以使用以下的PHP代码:
<?php
// 获取页面链接的缩略图和描述信息
function getOpenGraphData($url) {
$html = file_get_contents($url);
// 使用正则表达式匹配缩略图链接和描述信息
preg_match('/<meta property="og:image" content="(.*?)">/i', $html, $matches);
$thumbnail = $matches[1];
preg_match('/<meta property="og:description" content="(.*?)">/i', $html, $matches);
$description = $matches[1];
// 返回缩略图URL和描述信息
return array(
'thumbnail' => $thumbnail,
'description' => $description
);
}
// 缩略图和描述信息存储在数据库中,根据需要去调用
$url = 'http://example.com'; // 需要分享的页面链接
$data = getOpenGraphData($url);
// 将缩略图和描述信息存储到数据库中
// 显示缩略图和描述信息
echo '<img src="' . $data['thumbnail'] . '" alt="Thumbnail">';
echo '<p>' . $data['description'] . '</p>';
?>
以上代码通过使用正则表达式来获取指定页面链接的Open Graph标签数据,其中"og:image"标签表示缩略图的链接,"og:description"标签表示描述信息。获取到这些数据后,我们可以存储到数据库中,并在需要展示的地方进行调用和显示。