Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

curl - PHP - How to make sure if a curl_multi proxy connection was succesful?

I want to use multi-cURL with proxies, but I can't figure out how to check if each proxy connection was succesful. With a the normal single cURL I make a simple loop which runs as long curl_errno isn't 0.

But how to make with multi-cURL?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can have a simple proxy checker before you run your query on multi-curl

Simple Proxy Checker

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 ;
}

Usage

$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("
", $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 
";
    else
        echo $proxy , " - bad 
";
}

Output

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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...