1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Proxy Rotation

Discussion in 'PHP & Perl' started by tasburrfoot, Jan 22, 2017.

  1. tasburrfoot

    tasburrfoot Regular Member

    Joined:
    Dec 16, 2008
    Messages:
    323
    Likes Received:
    152
    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($chCURLOPT_URL,$url);
        
    curl_setopt($chCURLOPT_PROXY$proxy);
        
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
    curl_setopt($chCURLOPT_HEADER1);
        
    $curl_scraped_page curl_exec($ch);
        
    curl_close($ch);
        echo 
    $curl_scraped_page;
    }

    ?>
     
  2. seohug

    seohug Jr. VIP Jr. VIP

    Joined:
    Feb 14, 2011
    Messages:
    336
    Likes Received:
    61
    Gender:
    Male
    Occupation:
    Automator
    Location:
    UK
    Home Page:
    Nice, thanks