API仅供学习交流使用,禁止用于商业用途、违法用途等,否则后果自负!
请求链接:http://xxx.com/1.php?url=https://mp.weixin.qq.com/s/xxx
PHP版:
<?php
// 获取用户传入的文章URL
$articleUrl = $_GET['url'] ?? '';
if (empty($articleUrl)) {
header('Content-Type: application/json');
echo json_encode([
'code' => 400,
'msg' => '缺少文章URL参数'
], JSON_UNESCAPED_UNICODE);
exit;
}
// 配置cURL请求(终极版模拟)
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $articleUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Linux; Android 13; MI 11 Build/TKQ1.220829.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/126.0.6478.188 Mobile Safari/537.36 MicroMessenger/8.0.50.2401(0x2800325F) WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64',
CURLOPT_REFERER => 'https://mp.weixin.qq.com/',
CURLOPT_ENCODING => 'gzip, deflate, br',
CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
'Cache-Control: max-age=0',
'Connection: keep-alive',
'Sec-Fetch-Dest: document',
'Sec-Fetch-Mode: navigate',
'Sec-Fetch-Site: same-origin',
'Sec-Fetch-User: ?1',
'Upgrade-Insecure-Requests: 1',
'X-Requested-With: com.tencent.mm',
'Cookie: ' . http_build_query([
'appmsg_token' => generate_token(),
'mm_lang' => 'zh_CN',
'reward_sn' => ''
])
]
]);
// 获取文章HTML内容
$html = curl_exec($ch);
curl_close($ch);
if (!$html) {
header('Content-Type: application/json');
echo json_encode([
'code' => 500,
'msg' => '获取文章内容失败'
], JSON_UNESCAPED_UNICODE);
exit;
}
// 解析HTML内容
$dom = new DOMDocument();
libxml_use_internal_errors(true);
@$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
libxml_clear_errors();
/****************** 提取文章标题******************/
$title = '未知标题';
$metaNodes = $dom->getElementsByTagName('meta');
foreach ($metaNodes as $node) {
$property = $node->getAttribute('property');
if ($property === 'og:title') {
$title = htmlspecialchars_decode(trim($node->getAttribute('content')));
break;
}
}
/****************** 图片提取部分******************/
$contentDiv = $dom->getElementById('js_content') ??
$dom->getElementsByTagName('div')->item(0);
$imageUrls = [];
if ($contentDiv) {
$images = $contentDiv->getElementsByTagName('img');
foreach ($images as $img) {
$src = $img->getAttribute('data-src') ?: $img->getAttribute('src');
if ($src) {
$fullUrl = parse_url($src, PHP_URL_SCHEME) ?
$src : 'https://' . ltrim($src, '/');
$fullUrl .= (strpos($fullUrl, '?') === false ? '?' : '&') . 'wxfrom=5&wx_lazy=1';
$imageUrls[] = $fullUrl;
}
}
}
// 返回结果
header('Content-Type: application/json');
echo json_encode([
'code' => empty($imageUrls) ? 201 : 200,
'title' => $title,
'image_urls' => $imageUrls,
'tips' => '解析失败多试几次,Meiyan美言AI'
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// 生成动态token(模拟微信客户端)
function generate_token($length = 32) {
return substr(str_shuffle('0123456789abcdef'), 0, $length);
}
适配图集和公众号昵称版:
<?php
// 获取用户传入的文章URL
$articleUrl = $_GET['url']?? '';
if (empty($articleUrl)) {
header('Content-Type: application/json');
echo json_encode([
'code' => 400,
'msg' => '缺少文章URL参数'
], JSON_UNESCAPED_UNICODE);
exit;
}
// 配置cURL请求
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $articleUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
// 尝试更换User-Agent
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
CURLOPT_REFERER => 'https://mp.weixin.qq.com/',
CURLOPT_ENCODING => 'gzip, deflate, br',
CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
'Cache-Control: max-age=0',
'Connection: keep-alive',
'Sec-Fetch-Dest: document',
'Sec-Fetch-Mode: navigate',
'Sec-Fetch-Site: same-origin',
'Sec-Fetch-User:?1',
'Upgrade-Insecure-Requests: 1',
'X-Requested-With: com.tencent.mm',
'Cookie: '. http_build_query([
'appmsg_token' => generate_token(),
'mm_lang' => 'zh_CN',
'reward_sn' => ''
])
]
]);
// 获取文章HTML内容
$html = curl_exec($ch);
if ($html === false) {
// 获取详细的cURL错误信息
$error = curl_error($ch);
header('Content-Type: application/json');
echo json_encode([
'code' => 500,
'msg' => '获取文章内容失败: '. $error
], JSON_UNESCAPED_UNICODE);
exit;
}
curl_close($ch);
// 解析HTML内容
$dom = new DOMDocument();
libxml_use_internal_errors(true);
@$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
libxml_clear_errors();
/****************** 提取文章标题******************/
$title = '未知标题';
$metaNodes = $dom->getElementsByTagName('meta');
foreach ($metaNodes as $node) {
$property = $node->getAttribute('property');
if ($property === 'og:title') {
$title = htmlspecialchars_decode(trim($node->getAttribute('content')));
break;
}
}
/****************** 提取公众号名称 ******************/
$username = '未知公众号';
$divs = $dom->getElementsByTagName('div');
foreach ($divs as $div) {
if ($div->hasAttribute('class') && strpos($div->getAttribute('class'), 'wx_follow_nickname')!== false) {
$username = htmlspecialchars_decode(trim($div->textContent));
break;
}
}
/****************** 图片提取部分******************/
$imageUrls = [];
$allImages = $dom->getElementsByTagName('img');
foreach ($allImages as $img) {
$src = $img->getAttribute('data-src')?: $img->getAttribute('src');
if (empty($src)) continue;
// 处理相对路径
if (substr($src, 0, 2) === '//') {
$fullUrl = 'https:'. $src;
} elseif ($src[0] === '/') {
$fullUrl = 'https://'. parse_url($articleUrl, PHP_URL_HOST). $src;
} else {
$fullUrl = $src;
}
// 过滤非微信图片
$host = parse_url($fullUrl, PHP_URL_HOST);
if (strpos($host, 'qpic.cn') === false) continue;
// 添加必要参数
$separator = strpos($fullUrl, '?') === false? '?' : '&';
$fullUrl.= $separator. 'wxfrom=5&wx_lazy=1';
$imageUrls[] = $fullUrl;
}
// 提取纯图集中的图片链接
if (preg_match_all('/cdn_url:\s*\'([^\']+)\'/', $html, $matches)) {
foreach ($matches[1] as $cdnUrl) {
// 添加必要参数
$separator = strpos($cdnUrl, '?') === false? '?' : '&';
$fullUrl = $cdnUrl. $separator. 'wxfrom=5&wx_lazy=1';
$imageUrls[] = $fullUrl;
}
}
// 返回结果
header('Content-Type: application/json');
echo json_encode([
'code' => empty($imageUrls)? 201 : 200,
'title' => $title,
'username' => $username,
'image_urls' => array_values(array_unique($imageUrls)), // 去重并确保索引连续
'tips' => '解析失败多试几次,Meiyan美言AI'
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// 生成动态token(模拟微信客户端)
function generate_token($length = 32) {
return substr(str_shuffle('0123456789abcdef'), 0, $length);
}