1. Halo Guest, pastikan Anda selalu menaati peraturan forum sebelum mengirimkan post atau thread baru.

[ASK] PHP Curl menggunakan Proxy Rotation

Discussion in 'Pemrograman Web' started by mirdana, Aug 16, 2017.

  1. mirdana

    mirdana Newbie

    Joined:
    Mar 6, 2017
    Messages:
    11
    Likes Received:
    0
    source :
    Code:
    https://www.blackhatworld.com/seo/php-proxy-rotation.910239/
    class rotateproxy.php :
    Code:
    <?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;
            }
        }
    }
    ?>
    CONTOH grab.php :
    Code:
    <?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;
    }
    
    ?>
    yang jadi pertanyaan, ketika saya gabungkan dengan scrip graber yang udah ada
    jadi:
    Code:
    <?php
    require_once('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 = 1; // proxy will be used 5 times before moving to the next proxy.
    $rotate = new RotateProxy($proxylist,$counter);
    
    function get_contents($url, $referer = 'http://www.google.com/firefox?client=firefox-a&rls=org.mozilla:fr:official', $ua = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18') {
    if(function_exists('curl_exec')) {
     $proxy = $rotate->getProxy();
     $curl = curl_init();
     $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
     $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
     $header[] = "Cache-Control: max-age=0";
     $header[] = "Connection: keep-alive";
     $header[] = "Keep-Alive: 300";
     $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
     $header[] = "Accept-Language: en-us,en;q=0.5";
     $header[] = "Pragma: ";
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_PROXY, $proxy);
     curl_setopt($curl, CURLOPT_USERAGENT, $ua);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
     curl_setopt($curl, CURLOPT_REFERER, $referer);
     curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
     curl_setopt($curl, CURLOPT_AUTOREFERER, true);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_TIMEOUT, 10);
     $content = curl_exec($curl);
     curl_close($curl);
     } else {
     ini_set('user_agent', $ua);
     $content = file_get_contents($url);
     }
     return $content;
    }
    ?>
    hasilnya error : Call to a member function getProxy() on a non-object in grab.php on line 190
    dimana line 190 adalah $proxy = $rotate->getProxy();

    sedangkan kalau tanpa tambahan script proxy rotation, skrip grab berjalan sempurna.

    mohon solusinya para mastah php
     
  2. neody

    neody Hero

    Joined:
    Mar 24, 2010
    Messages:
    506
    Likes Received:
    117
    Location:
    jakarta
    coba pakai ini gan
    PHP:
    function curlDong($url,$proxy='',$proxyauth=''){

        
    $ch=curl_init();
      
        
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        
    curl_setopt($ch,CURLOPT_URL,$url);    // URL for CURL call
      
        
    if(!empty($proxy))curl_setopt($chCURLOPT_PROXY$proxy);
        if(!empty(
    $proxyauth))curl_setopt($chCURLOPT_PROXYUSERPWD$proxyauth);
      
        
    curl_setopt($chCURLOPT_PROXYTYPECURLPROXY_SOCKS5);
      
        
    $cx=curl_exec($ch);
        
    curl_close($ch);
        return(
    $cx);
     
    }

    //pemakaian
    $isinya curlDong($urlnya,$proxyPortnya,'usernamenya:passwordnya');   
     
  3. renkawan

    renkawan Ads.id Starter

    Joined:
    Mar 24, 2016
    Messages:
    72
    Likes Received:
    1
    Location:
    http://www.leblung.com
    seharusnya ga ada masalah sih gan (sudah sy coba sendiri jg ga ada masalah). klo boleh tau agan masukin potongan kode itu ke framework atau native?
    klo ke framework mungkin pengaruh case-sensitive nama file & penamaan class.
     
  4. AdhyKun

    AdhyKun Super Hero

    Joined:
    Jan 17, 2012
    Messages:
    872
    Likes Received:
    42
    Location:
    Bengkulu - Yogyakarta - Solo
    Coba di ganti
    PHP:
     this->getProxy() 
     
  5. Hikaru Strap

    Hikaru Strap Newbie

    Joined:
    Sep 18, 2017
    Messages:
    29
    Likes Received:
    3
    Location:
    Tokyo, Japan
    ini berguna juga buat main b0t gak gan ??
     
  6. gitumau

    gitumau Newbie

    Joined:
    Nov 28, 2017
    Messages:
    14
    Likes Received:
    2
    itu var $rotate dalam function get_contents masih null
    cmiiw

    ganti jadi gini gan:
    function get_contents($rotate,$url, ....{
    ...
    }

    terus make nya :
    get_contents($rotate,....);
     

Share This Page