[PHP] 纯文本查看 复制代码 <?php
function makeRequest($url, $headers) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if ($response === false) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
function checkQRCodeStatus($checkUrl, $headers) {
// 发起请求并获取返回数据
$response_check = makeRequest($checkUrl, $headers);
// 解压缩返回的数据
$data_check = gzdecode($response_check);
if ($data_check !== false) {
// 将 JSON 数据解码为 PHP 关联数组
$decoded_data_check = json_decode($data_check, true);
if ($decoded_data_check !== null) {
// 输出解码后的数据
echo json_encode($decoded_data_check, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} else {
echo "Error decoding JSON data.";
}
} else {
echo "Error decoding compressed data for check URL.";
}
}
// 设置请求URL
$url = 'https://passport.weibo.com/sso/v2/qrcode/image?entry=miniblog&size=180';
// 设置请求头部信息
$headers = array(
'Host: passport.weibo.com',
'Connection: keep-alive',
'sec-ch-ua: "Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
'Accept: application/json, text/plain, */*',
'X-Requested-With: XMLHttpRequest',
'X-CSRF-TOKEN: CK-Yco1Rc7C-N7I9J_lspWCmc_DvD-DaAGc74zxH1-gacIgX1=',
'sec-ch-ua-mobile: ?0',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'sec-ch-ua-platform: "Windows"',
'Sec-Fetch-Site: same-origin',
'Sec-Fetch-Mode: cors',
'Sec-Fetch-Dest: empty',
'Referer: https://passport.weibo.com/sso/signin?entry=miniblog&source=miniblog&disp=popup&url=https%3A%2F%2Fweibo.com%2Fnewlogin%3Ftabtype%3Dweibo%26gid%3D102803%26openLoginLayer%3D0%26url%3Dhttps%253A%252F%252Fweibo.com%252F',
'Accept-Encoding: gzip, deflate, br, zstd',
);
// 初始化 cURL 会话并获取二维码图片的 JSON 数据
$response = makeRequest($url, $headers);
// 解压缩返回的数据
$data = gzdecode($response);
if ($data !== false) {
$json_data = json_decode($data, true);
if ($json_data && isset($json_data['retcode']) && $json_data['retcode'] === 20000000) {
$qrid = $json_data['data']['qrid'];
echo "QR ID: " . $qrid . "<br>";
// 显示原始获取到的图片
echo '<img src="' . $json_data['data']['image'] . '" alt="QR Code" /><br>';
// 构建新的请求URL
$checkUrl = 'https://passport.weibo.com/sso/v2/qrcode/check?tabtype=weibo&gid=102803&openLoginLayer=0&url=https%3A%2F%2Fweibo.com%2F&qrid=' . $qrid . '&disp=popup';
// 打印出 $checkUrl 以进行检查
echo "Check URL: " . $checkUrl . "<br>";
// 获取并输出二维码状态
checkQRCodeStatus($checkUrl, $headers);
} else {
echo "Invalid JSON data or retcode is not 20000000.";
}
} else {
echo "Error decoding compressed data.";
}
?>
问题是这样子的,我是在宝塔直接运行的,我获取的二维码是可以正常扫码的,但是获取到是qird值我捕获状态调试的时候发现是“该二维码已过期,请重新扫描”,我前面还加了定时器,php代码里面显示的居然是未使用,但是没办法捕获到扫码成功的返回。大牛帮我看一下,我这个写法是不是有问题,
|