PHP Proxy Rotation

tasburrfoot

Regular Member
Joined
Dec 16, 2008
Messages
338
Reaction score
163
Here's a quick class and usage example for proxy rotation. I've used it in a few scraping tools in the past.

Class:
PHP:
<?php

Class RotateProxy{
    private $proxy = '';
    private $fp = '';
    private $list = array();
    private $counter; //number of times proxy used. Default is 1
    private $current; //number of times proxy HAS been used.
   
    public function __constructor($path,$counter=1){
        $this->list = trim(file($path));
        $this->$counter = $counter;
    }
   
    public function getProxy(){
        if($this->current > $this->counter){
            array_shift($this->list);
           
            $this->current = 1;
           
            $this->proxy = $this->list[0];
           
            return $this->proxy;
        }else{
            $this->current++;
            return $this->proxy;
        }
    }
}



?>

Example:
PHP:
<?PHP
require_once('path/to/rotateproxy.php'); //The above class.
$url = 'http://somesite.com';
$proxylist = __DIR__ . 'proxies.txt'; // just an example.

/*********
** $counter is how many times the proxy can be used before it is discarded. Defaults to 1.
*/
$counter = 5; // proxy will be used 5 times before moving to the next proxy.
$rotate = new RotateProxy($proxylist,$counter);

while(SomeAction){
    $proxy = $rotate->getProxy();

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $curl_scraped_page = curl_exec($ch);
    curl_close($ch);
    echo $curl_scraped_page;
}

?>
 
Back
Top