spys[dot]ru Proxy Checker [PHP]

Script untuk cek list proxy dari spys.ru dengan fitur pengecekan proxy tersebut cukup cepat atau tidak sesuai dengan koneksi internet yang digunakan.

Script untuk cek list proxy dari spys.ru dengan fitur pengecekan proxy tersebut cukup cepat atau tidak sesuai dengan koneksi internet yang digunakan. Caranya melalui respond dari proxy server / timeout-nya (defaultnya saya buat 3 detik).

  1<?php
  2/**
  3 * spys.ru Proxy Checker
  4 * 
  5 * This program designed to display a list of proxies from spys.ru site.
  6 * In addition, you can also show 'good proxy' that fit with your current
  7 * internet connection..
  8 * 
  9 * Still very early release, just for fun coding purpose :)
 10 * 
 11 * @author <[email protected]>
 12 * 
 13 * Example usage : 
 14 * $proxy = new spysRU();
 15 * $proxy->goodProxy(); // show proxies fits your current internet connection.
 16 * [or]
 17 * $proxy->goodProxy(1); // show debug for error connection from proxy server.
 18 * [or]
 19 * $proxy->showProxy(); // show any available free proxy from spys.ru
 20 * 
 21 *
 22 * LICENSE :
 23 * This program is free software; you can redistribute it and/or modify it
 24 * under the terms of the GNU General Public License version 2 as published by
 25 * the Free Software Foundation.
 26 * This program is distributed in the hope that it will be useful, but WITHOUT
 27 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 28 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 29 * more details.
 30 */
 31
 32class spysRU {
 33    
 34    protected $url = 'http://spys.ru/en/free-proxy-list/';
 35    protected $defaultTimeOut = 3; // in seconds
 36    private $data = '';
 37    private $ports = array();
 38    private $proxyData;
 39    private $userAgent = array(
 40        'Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5',
 41        'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
 42        'Opera/9.25 (Windows NT 5.1; U; en)',
 43        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
 44        'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
 45        'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
 46        'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:50',
 47        'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+2011-10-16 20:21:10',
 48        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.0',
 49        'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6'
 50    );
 51    
 52    function __construct() {
 53        $postValue = array('sto' => 'View 150 per page');
 54        $eva = curl_init();
 55        curl_setopt($eva, CURLOPT_URL, $this->url);
 56        curl_setopt($eva, CURLOPT_USERAGENT, $this->userAgent[array_rand($this->userAgent)]); 
 57        curl_setopt($eva, CURLOPT_RETURNTRANSFER, 1);
 58        curl_setopt($eva, CURLOPT_POST, 1);
 59        curl_setopt($eva, CURLOPT_POSTFIELDS, $postValue);
 60        $this->data = curl_exec($eva);
 61        
 62        preg_match('/<\/table><script type="text\/javascript">(.*?)<\/script>/', $this->data, $js ); // get the javascript
 63        preg_match_all('/[a-z0-9]{6}=[0-9]/', $js[1], $jsPort, PREG_SET_ORDER );
 64        preg_match_all('/<font class=spy14>(.\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})<script type="text\/javascript">(.*?)<\/script>/', $this->data, $proxys, PREG_SET_ORDER ); // proxy data
 65        curl_close($eva);
 66        
 67        $this->ports = $this->jsToArrayPort($jsPort);
 68        $this->proxyData = $proxys;
 69        
 70    }
 71    
 72    function showProxy($print=1) {
 73        $proxy = '';
 74        foreach ( $this->proxyData as $proxyK ) {
 75            if ( !$print )
 76                $proxy[] = $proxyK[1] . ':' . $this->replacePort($proxyK[2]);
 77            else
 78                print $proxyK[1] . ':' . $this->replacePort($proxyK[2]) . "\n";
 79        }
 80        return $proxy;
 81    }
 82    
 83    private function replacePort ($string) {
 84        $string = str_replace('document.write("<font class=spy2>:<\/font>"', '', $string);
 85        $string = explode("+", $string);
 86        $port = '';
 87        foreach ( $string as $encPort ) {
 88            $plainPort = preg_replace(array("/\^[a-z0-9]{4}/", "/[^a-z0-9]+/i"), array("",""), $encPort);
 89            if ( array_key_exists($plainPort, $this->ports) )
 90                $port .= $this->ports[$plainPort];
 91        }
 92        return $port;
 93    }
 94    
 95    private function jsToArrayPort ($arr) {
 96        foreach ( $arr as $key => $val ) {
 97            $arrayimu = explode("=", $val[0]);
 98            $port[$arrayimu[0]] = $arrayimu[1];
 99        }
100        return $port;
101    }
102    
103    function goodProxy ($debug=0) {
104        $status = '';
105        if ( $debug )
106            $status = " [OK]";
107        foreach ( $this->showProxy(0) as $proxy ) {
108            $part = explode(':',$proxy);
109            if($con = @fsockopen($part[0], $part[1], $errNum, $errStr, $this->defaultTimeOut)) {
110                print $proxy . $status . "\n";
111                fclose($con);
112            }
113            else
114                if ( $debug )
115                    print $proxy . " [" . $errStr . "]\n";
116        }
117    }
118}
119?>

Cara penggunaannya:

pertama panggil dulu classnya, misal:

1$proxy = new spysRU(); 

Lalu eksekusi public functionnya. Misal :

1$proxy->showProxy(); // untuk menampilkan proxy yg tersedia 
2$proxy->goodProxy(); // untuk menampilkan proxy yg cukup sesuai dengan koneksi internet kita 
3$proxy->goodProxy(1); // debug, menampilkan juga pesan error dari proxy server. 

Feel free to use / modify.