Files
WechatMomentScreenshot/get_article_info.php

43 lines
1.6 KiB
PHP
Raw Normal View History

2019-05-14 15:25:07 +08:00
<?php
/* 自动获取微信公众号文章标题/封面 */
/* https://github.com/TransparentLC/WechatMomentScreenshot */
//允许跨域
2019-05-14 23:52:52 +08:00
header('Access-Control-Allow-Origin: *');
2022-12-21 22:15:17 +08:00
header('Content-Type: application/json');
2019-05-14 15:25:07 +08:00
2020-05-25 21:47:48 +08:00
if (empty($_GET['url']) || strpos($_GET['url'], 'mp.weixin.qq.com') === false) {
2019-05-14 15:25:07 +08:00
$result['title'] = '';
$result['cover'] = '';
} else {
$ch = curl_init(htmlspecialchars_decode($_GET['url']));
curl_setopt_array($ch, [
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko',
]);
$response = curl_exec($ch);
2020-05-25 21:47:48 +08:00
preg_match('/var msg_title = \'[\S\s]*?\'.html\\(false\\);/', $response, $matches);
$result['title'] = empty($matches[0]) ? '' : htmlspecialchars_decode(str_replace(['var msg_title = \'', '\'.html(false);'], '', $matches[0]));
2019-05-14 15:25:07 +08:00
preg_match('/var msg_cdn_url = "[\S\s]*?";/', $response, $matches);
2020-05-25 21:47:48 +08:00
$result['cover'] = empty($matches[0]) ? '' : str_replace(['var msg_cdn_url = "', '";'], '', $matches[0]);
2019-05-14 15:25:07 +08:00
curl_close($ch);
};
if (!empty($result['cover'])) {
2022-12-21 22:15:17 +08:00
$result['cover'] = 'https://images.weserv.nl/?' . http_build_query([
'url' => $result['cover'],
'il' => '',
'we' => '',
'h' => 360,
'q' => 70,
2019-05-14 15:25:07 +08:00
]);
}
$result['success'] = !empty($result['title']) && !empty($result['cover']);
2019-08-24 00:30:41 +08:00
echo json_encode($result, JSON_UNESCAPED_UNICODE);
2019-05-14 15:25:07 +08:00
?>