'; echo "📥 抓取URL: {$result['url']}\n"; echo "✅ HTTP 状态码: {$result['http_code']}\n"; echo "🌐 content-type: {$result['content_type']}\n"; echo "🔁 重定向次数: {$result['redirects']}\n"; echo "⏱️ 总耗时: {$result['total_time']} 秒\n"; echo "📦 下载数据: {$result['download_size']} 字节\n"; echo "📑 内容大小: {$result['get_body_size']} 字节\n"; if ($result['error']) { echo "\r\n❌ 抓取失败: {$result['error']}\n"; } if (str_exists($result['http_code'], '200') && str_exists($result['content_type'], 'text/html') ) { $encoding = mb_detect_encoding($result['body'], "UTF-8, GB2312, GBK, ISO-8859-1", true); echo "ℹ️ 编码(charset):$encoding\n"; } print_r($result['info']); echo ''; exit; } // 判断要不要从源站重新抓数据 if (is_file($cacheFile)) { // 是否需要 刷新缓存 $needRefresh = filemtime($cacheFile)<(time()-$cacheTime) ? true : false; if ($re=='upjs') { $needCrawl = $needRefresh; } else { $needCrawl = false; # 没有加参数 re=upjs 的,都只读缓存数据 // 缓存过期的,加入任务列表 if ($needRefresh) { add_task($uri, $tasklist); } } } /** * 获取源数据,并缓存 * */ if ( $needCrawl ) { // 源URL $u = $domain.$uri; // 开始抓取 $fetch = zz_curl($u, $curlopt, false); // print_r($fetch); $http_code = trim($fetch['http_code']); // 连接失败的话,输出错误提示 if (!$fetch['success']) { if ($fetch['error']) { e502('1. The server was unable to complete your request.'); // echo "❌ 错误: {$fetch['error']}\n"; } exit; } if ($http_code!='200') { if ($http_code=='404') e404(); elseif ($http_code=='403') e403(); elseif ($http_code=='401') e401(); else e502(); } $h = $fetch['body']; // 源代码处理 if (function_exists('rph')) $h = rph($h); // 创建缓存 if ($cacheTime) { mkdirs(dirname($cacheFile)); if (is_writable(dirname($cacheFile))) { if ( str_exists($fetch['content_type'], 'text/html') && strtolower(trim($charset))!='utf-8' ) { // 网页编码如果不是utf-8的,转换为utf-8 $h = mb_convert_encoding($h, "utf-8", $charset); } if ($h) { file_put_contents($cacheFile, $h); } } else { exit('缓存目录没有写入权限'); } } } // 读取缓存 if (is_file($cacheFile)) { // 设置HTTP头部 $contenttype = get_content_type($cacheFile); if (str_exists($contenttype, 'text/html')) { $contenttype .= "; charset=utf-8"; } header('Content-Type: '.$contenttype); if (filesize($cacheFile)>0) { $cacheFileMtime = filemtime($cacheFile); $ccfMDate = date('Y-m-d H:i:s', $cacheFileMtime); // 读取缓存数据 $h = file_get_contents($cacheFile); // 缓存代码处理 if (function_exists('rpc')) $h = rpc($h); if (str_exists($h, '')) { // HTML页面尾部加上缓存时间 $h .= "\r\n\r\n"; // 在session里存储最后访问的uri $session->lastviewedpage = $uri; // 触发执行刷新缓存任务 if (is_file($tasklist) && filesize($tasklist)>1) { $upUrl = "/taskexc.php?t=trigger"; $h = str_replace('', "\r\n
\r\n", $h); } } // if ($cacheFileMtime < time()-$cacheTime && $re!='upjs') { // // 使用标签异步触发缓存更新,提升网页打开速度 // $upUrl = str_exists($uri, '?') ? "$uri&re=upjs" : "$uri?re=upjs"; // $h = str_replace('', "\r\n
\r\n", $h); // } echo $h; } } else { if ($output) e502('3. The server was unable to complete your request. '); else exit(); }