PHP - 如何确保curl多代理连接是否成功?

时间:2012-09-23 23:45:44

标签: php curl

我想使用带有代理的multi-cURL,但我无法弄清楚如何检查每个代理连接是否成功。使用正常的单个cURL,我创建一个简单的循环,只要curl_errno不为0就会运行。

但如何使用multi-cURL?

谢谢!

1 个答案:

答案 0 :(得分:5)

在multi-curl

上运行查询之前,您可以使用简单的代理检查程序

简单代理检查器

function __proxyChecker($proxy)
{
    $ch = curl_init("http://google.com");
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    $handle = curl_exec($ch);
    curl_close($ch);
    return $handle ;
}

用法

$proxies = "211.136.10.29:80
88.146.161.215:3128 
211.136.10.29:80 
61.35.0.39:6515 
77.78.197.15:8080
211.161.152.106:80";

$proxies = explode("\n", $proxies);
//shuffle($proxies);
$url = "http://google.com";
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);

echo "<pre>";
foreach ( $proxies as $proxy ) {
    $proxy = trim($proxy);
    if(empty($proxy))
        continue ;

    if(__proxyChecker($proxy))  
        echo $proxy , " - ok \n";
    else
        echo $proxy , " - bad \n";
}

输出

211.136.10.29:80 - ok 
88.146.161.215:3128 - ok 
211.136.10.29:80 - ok 
61.35.0.39:6515 - bad 
77.78.197.15:8080 - bad 
211.161.152.106:80 - ok