<?php
// 设置PHP内部编码为UTF-8
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
// 确保输出不进行转义,强制使用UTF-8编码
header('Content-Type: application/json; charset=utf-8');
// 配置选项
$config = [
'image_url' => 'https://tj2.mini1.cn/favicon.ico',
'document_url' => 'https://tj2.mini1.cn/miniworld?v1=1000&v2=undefined&v3=undefined&v4=undefined&v5=undefined&v6=1&v7=1&k=1', // 重要:替换为实际HTML页面URL
'cookie_file' => __DIR__ . '/cookies.txt',
'debug_mode' => true
];
// 验证配置
if ($config['document_url'] === 'https://tj2.mini1.cn/miniworld?v1=1000&v2=undefined&v3=undefined&v4=undefined&v5=undefined&v6=1&v7=1&k=1') {
die(json_encode([
'status' => 'error',
'message' => '请先配置$config[\'document_url\']为实际有Cookie的HTML页面URL'
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}
// 初始化CURL会话
function initCurl($url, $cookieFile, $referer = '') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// 设置请求头
$headers = [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
'Connection: keep-alive'
];
// 如果有referer,则添加到请求头
if (!empty($referer)) {
$headers[] = "Referer: $referer";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
return $ch;
}
// 提取响应头中的Cookie
function extractCookiesFromHeaders($headers) {
$cookies = [];
$header_lines = explode("\r\n", $headers);
foreach ($header_lines as $line) {
if (stripos($line, 'Set-Cookie:') === 0) {
$cookie_str = trim(substr($line, 11));
$eq_pos = strpos($cookie_str, '=');
if ($eq_pos !== false) {
$name = substr($cookie_str, 0, $eq_pos);
$value_part = substr($cookie_str, $eq_pos + 1);
$semicolon_pos = strpos($value_part, ';');
$value = ($semicolon_pos !== false)
? substr($value_part, 0, $semicolon_pos)
: $value_part;
$cookies[$name] = trim($value);
}
}
}
return $cookies;
}
// 步骤1:请求图片URL(模拟浏览器行为)
$ch1 = initCurl($config['image_url'], $config['cookie_file']);
$response1 = curl_exec($ch1);
$info1 = curl_getinfo($ch1);
$header_size1 = $info1['header_size'];
$headers1 = substr($response1, 0, $header_size1);
$cookies1 = extractCookiesFromHeaders($headers1);
curl_close($ch1);
// 步骤2:请求HTML页面获取Cookie,使用图片URL作为referer
$ch2 = initCurl($config['document_url'], $config['cookie_file'], $config['image_url']);
$response2 = curl_exec($ch2);
$info2 = curl_getinfo($ch2);
$header_size2 = $info2['header_size'];
$headers2 = substr($response2, 0, $header_size2);
$cookies2 = extractCookiesFromHeaders($headers2);
curl_close($ch2);
// 合并两次请求获得的Cookie
$allCookies = array_merge($cookies1, $cookies2);
// 检查是否有错误
if (curl_errno($ch2)) {
$result = [
'status' => 'error',
'message' => '请求失败: ' . curl_error($ch2),
'curl_info' => $info2
];
} else {
if (!empty($allCookies)) {
$result = [
'status' => 'success',
'cookies' => $allCookies,
'response_info' => [
'image_request' => [
'content_type' => $info1['content_type'],
'http_code' => $info1['http_code']
],
'document_request' => [
'content_type' => $info2['content_type'],
'http_code' => $info2['http_code']
]
]
];
} else {
$result = [
'status' => 'warning',
'message' => '未找到Cookie',
'debug_info' => [
'image_request_headers' => $headers1,
'document_request_headers' => $headers2,
'request_urls' => [
'image_url' => $config['image_url'],
'document_url' => $config['document_url']
],
'curl_info' => [
'image_request' => $info1,
'document_request' => $info2
],
'cookie_file_exists' => file_exists($config['cookie_file']),
'cookie_file_content' => file_exists($config['cookie_file']) ? file_get_contents($config['cookie_file']) : ''
]
];
}
}
// 确保所有字符串都是UTF-8编码
function convertToUtf8($data) {
if (is_string($data)) {
return mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data, 'UTF-8, GBK, GB2312, ISO-8859-1', true));
} elseif (is_array($data)) {
$newData = [];
foreach ($data as $key => $value) {
$newData[convertToUtf8($key)] = convertToUtf8($value);
}
return $newData;
} elseif (is_object($data)) {
foreach ($data as $key => $value) {
$data->$key = convertToUtf8($value);
}
return $data;
}
return $data;
}
// 转换所有数据为UTF-8编码
$result = convertToUtf8($result);
// 返回JSON结果,确保中文不被转义
echo json_encode($result, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | ($config['debug_mode'] ? JSON_PRETTY_PRINT : 0));
?>