Location: PHPKode > scripts > Proxy > proxy/Proxy.php
<?php

/*
   @author Petter Kjelkenes
   @support hide@address.com

   Class to fetch random proxies. You may use these proxies to open connections with CURL or whatever you want to use the proxies for.

   HOWTO:
   $pr = new Proxy();
   $pr->initDefaultProxies();  // Get default proxies, if these dont work, you should use $pr->add($address, $regexppatern);
   $proxy = $pr->getProxies(); // gets proxies like this: array( 0 => ip:port, 1 => ip:port , ..... and so on .... )

*/



class Proxy{
    // Stores proxies.
    public $store=array();

    // Cache store for proxies.
    private $file = '__FILE_PROXY_CACHE.php';


    // Constructs the class.
    public function __construct(){
        if (file_exists($this->file)){
            include $this->file;

            if (isset($s) && is_array($s))$this->store = $s;
        }

    }

    // Adds default proxy list, these sites may be down so you must maybe create your own handlers...
    public function initDefaultProxies(){
        $this->add('http://www.ip-adress.com/proxy_list/',
            "#<td>(.*?):(\d{1,4})</td>#");

        $this->add('http://www.digitalcybersoft.com/ProxyList/fresh-proxy-list.shtml?',
            "#(.*?):(\d{1,4}).*?\n#");

        $this->add('http://www.secretip.com/proxylist.php',
            "#(.*?):(\d{1,4})<br />\n#");

        $this->add('http://www.proxyserverprivacy.com/free-proxy-list.shtml',
            "#(.*?):(\d{1,4})<br />\n#");
    }


    // Gets the proxies in associve array ( ip:port )
    public function getProxies(){
        $ip = array();
        foreach($this->store as $p){
            $ip[] = $p['proxy'].':'.$p['port'];
        }
        return $ip;
    }

    // Use this when adding own handlers to add proxy to list.
    public function debugadd($adr, $regexp){
        unset($this->store);
        $this->add($adr,$regexp);
        echo "<pre>",print_r($this->store),"</pre>";

        die();

    }

    // Use this to add a site to the proxy array.
    public function add($adr, $regexp){
        if (!$this->timeupdate($adr))return false;

        $c = file_get_contents($adr);
        if ($c){
            $matches = array();
            preg_match_all($regexp,$c,$matches);
            if (count($matches) > 0){
                foreach($matches[0] as $k => $m){
                    $port = intval($matches[2][$k]);

                    $ip = trim($matches[1][$k]);
                    $st = array('proxy' => $ip, 'port' => $port, 'site' => md5($adr), 'time' => time());
                    if ($port && !$this->inStore($st))$this->store[] = $st;
                }

                if (count($this->store) > 0)$this->dump();
            }
        }
    }


    private function inStore($st){
        foreach($this->store as $s){
            if ($s['proxy']==$st['proxy'] && $s['port']==$st['port'])return true;
        }
        return false;
    }

    private function dump(){
        file_put_contents($this->file, '<?php $s = '.var_export($this->store,true).'; ?>');
    }

    private function timeupdate($adr){
        foreach($this->store as $s){
            if ($s['site']==md5($adr)){
                if (time() > ($s['time']+(60*60*2)) ){
                    return false;
                }
            }
        }
        return true;

    }
}


?>
Return current item: Proxy