Sergey Cherkasov
Sergey Cherkasov

Reputation: 31

Check proxy using PHP

I'm writing a web app that requires a lot of proxies to work. I also have a list of proxies, but I don't know which of them works and what type are they (socks, http, https).

Let assume I have 5000 proxies in ip:port format. What is the fastest way to check all of them?

I tried fsockopen, but it is quite slow. Maybe pinging them first will save the time?

Upvotes: 3

Views: 21859

Answers (6)

EnseiTankado
EnseiTankado

Reputation: 11

There is a PHP tool on github that can scan the proxy list as multithread. It will not be difficult to integrate it into your own project. The tool also analyzes the security level of the proxies it scans. Yes, I wrote it. :)

https://github.com/enseitankado/proxy-profiler

Upvotes: 0

Killinks
Killinks

Reputation: 31

<?php
$proxies = file ("proxies.txt");
$mc = curl_multi_init ();
for ($thread_no = 0; $thread_no<count ($proxies); $thread_no++)
{
$c [$thread_no] = curl_init ();
curl_setopt ($c [$thread_no], CURLOPT_URL, "http://google.com");
curl_setopt ($c [$thread_no], CURLOPT_HEADER, 0);
curl_setopt ($c [$thread_no], CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($c [$thread_no], CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($c [$thread_no], CURLOPT_TIMEOUT, 10);
curl_setopt ($c [$thread_no], CURLOPT_PROXY, trim ($proxies [$thread_no]));
curl_setopt ($c [$thread_no], CURLOPT_PROXYTYPE, 0);
curl_multi_add_handle ($mc, $c [$thread_no]);
}

do {
while (($execrun = curl_multi_exec ($mc, $running)) == CURLM_CALL_MULTI_PERFORM);
if ($execrun != CURLM_OK) break;
while ($done = curl_multi_info_read ($mc))
{
$info = curl_getinfo ($done ['handle']);
if ($info ['http_code'] == 301) {
echo trim ($proxies [array_search ($done['handle'], $c)])."\r\n";
}
curl_multi_remove_handle ($mc, $done ['handle']);
}
} while ($running);
curl_multi_close ($mc);
?>

Upvotes: 3

James Steward
James Steward

Reputation: 29

Here is the code I use. You can modify it to meet your requirements:

    function _check($url,$usecookie = false,$sock="",$ref) {
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, False);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$_POST['timeoutpp']);
    curl_setopt($ch, CURLOPT_HEADER, 0);   
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3");  
    if($sock){
        curl_setopt($ch, CURLOPT_PROXY, $sock);
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
    }
    if ($usecookie){  
        curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie);  
        curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);     
    } 
    if ($ref){  
        curl_setopt($ch, CURLOPT_REFERER,$ref); 
    }
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
    $result=curl_exec ($ch);  
    curl_close($ch);  
    return $result;  
}

Upvotes: 0

Lix
Lix

Reputation: 162

This proxy checker API could be exactly what you're looking for. You can easily check a proxy list with that.

If you want to develop it yourself, is not difficult to do a small script to do the same than that API does.

Upvotes: 0

John
John

Reputation: 7826

The port usually gives you a good clue about the proxy type.
80,8080,3128 is typically HTTP
1080 is typically SOCKS

But let's be realistic, you seem to have a list of public proxies. It's not unlikely that every single one is not working anymore.
You can use curl or wget or lynx in a script or similar to test the proxies.

You can also try sort your list up into SOCKS and HTTP as good as you can and enter it into the Proxycollective . That's a free project but you need an invitation code or a 99cent ticket to become member. Once you are member you can upload your proxy lists and they will be tested. All working ones will be given back to you sortable.
So if you don't want to program something on your own that's maybe your best bet, invitation codes can sometimes be found in various forums.

But keep in mind what I said, if you have a list of 5000 random proxies I bet you'll hardly find more than 10 working ones in there anymore. Public proxies only live short.

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

You could use cURL to check for proxies. Some good article is given here Hope it helps

Upvotes: 1

Related Questions