function checkProxyIp($proxyIp, $times = 10, $targetUrl = 'https://www.baidu.com/') {
$headers = [
'Accept: application/json',
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US,en;q=0.8',
'Content-Type: application/json',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
];
$result = [
'proxy' => $proxyIp,
'total' => $times,
'success' => 0,
'failed' => 0,
'total_time' => 0,
'avg_time' => 0,
'details' => []
];
for ($i = 0; $i < $times; $i++) {
$start = microtime(true);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $targetUrl,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => 'gzip,deflate',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
CURLOPT_PROXY => $proxyIp,
CURLOPT_TIMEOUT => 10,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_COOKIEFILE => sys_get_temp_dir() . '/proxy_cookie.txt',
CURLOPT_COOKIEJAR => sys_get_temp_dir() . '/proxy_cookie.txt',
]);
// 隨機偽造 X-Forwarded-For 防封禁
$fakeIp = sprintf('%d.%d.%d.%d', mt_rand(1,255), mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($headers, [
"Client-Ip: $fakeIp",
"X-Forwarded-For: $fakeIp"
]));
$content = curl_exec($ch);
$elapsed = microtime(true) - $start;
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$success = ($httpCode === 200 && strpos($content, '百度一下') !== false);
$result['details'][] = [
'seq' => $i + 1,
'success' => $success,
'http_code' => $httpCode,
'time' => round($elapsed, 4),
'content_length' => strlen($content)
];
if ($success) {
$result['success']++;
$result['total_time'] += $elapsed;
} else {
$result['failed']++;
}
}
$result['success_rate'] = $times > 0 ? round($result['success'] / $times * 100, 2) : 0;
$result['avg_time'] = $result['success'] > 0 ? round($result['total_time'] / $result['success'], 4) : 0;
return $result;
}
// 使用示例
$proxy = '117.95.100.126:8998';
$report = checkProxyIp($proxy, 10);
print_r($report);
Array
(
[proxy] => 117.95.100.126:8998
[total] => 10
[success] => 8
[failed] => 2
[total_time] => 3.2456
[avg_time] => 0.4057
[success_rate] => 80.00
[details] => Array
(
[0] => Array([success]=>1 [http_code]=>200 [time]=>0.3821 ...)
[1] => Array([success]=>1 [http_code]=>200 [time]=>0.4102 ...)
[2] => Array([success]=>0 [http_code]=>0 [time]=>10.0000 ...) // 超時
...
)
)
function checkProxiesConcurrent($proxies, $times = 5, $concurrency = 20) {
$mh = curl_multi_init();
$handles = [];
$results = [];
// 初始化所有 easy handles
foreach ($proxies as $proxy) {
for ($i = 0; $i < $times; $i++) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://www.baidu.com/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_PROXY => $proxy,
CURLOPT_TIMEOUT => 10,
CURLOPT_NOSIGNAL => true, // 多線程必須
]);
curl_multi_add_handle($mh, $ch);
$handles[(int)$ch] = ['proxy' => $proxy, 'seq' => $i];
}
}
// 並發執行
$running = null;
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh, 0.1);
} while ($running > 0);
// 收集結果
foreach ($handles as $ch => $meta) {
$content = curl_multi_getcontent($ch);
$info = curl_getinfo($ch);
$success = ($info['http_code'] === 200 && strpos($content, '百度一下') !== false);
$results[$meta['proxy']][] = [
'success' => $success,
'time' => $info['total_time'],
'http_code' => $info['http_code']
];
curl_multi_remove_handle($mh, $ch);
curl_close($ch);
}
curl_multi_close($mh);
// 聚合統計
$summary = [];
foreach ($results as $proxy => $details) {
$succ = array_filter($details, fn($d) => $d['success']);
$summary[$proxy] = [
'total' => count($details),
'success' => count($succ),
'rate' => count($details) ? round(count($succ)/count($details)*100, 2) : 0,
'avg_time' => count($succ) ? round(array_sum(array_column($succ, 'time'))/count($succ), 4) : 0
];
}
return $summary;
}
代理 IP 檢測與使用需遵守目標網站
robots.txt、服務條款及當地法律法規。請勿用於大規模爬蟲攻擊、賬號暴力破解、數據盜取等非法用途。建議優先使用合法授權的代理服務商 API。