@Mile4311112

Как при вызове changeProxyIdentity() обновят опцию curl_setopt( $this -> ch, CURLOPT_PROXY, $torSocks5Proxy )?

Вот сам класс Proxy с методами changeProxyIdentity(), и отправкой curl

class Proxy {
    private $ch, $proxy;

    function __construct() {
        $torSocks5Proxy = "socks5://127.0.0.1:9050";
//        $torSocks5Proxy = "127.0.0.1:9050";

        $this -> ch = curl_init();

        curl_setopt( $this -> ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5 );
        curl_setopt( $this -> ch, CURLOPT_PROXY, $torSocks5Proxy );
        curl_setopt( $this -> ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $this -> ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $this -> ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $this -> ch, CURLOPT_HEADER, false );
    }

    public function curl( $url, $postParameter = null ) {

        if ( is_array($postParameter) && count( $postParameter ) > 0 )
            curl_setopt( $this -> ch, CURLOPT_POSTFIELDS, $postParameter );

        curl_setopt( $this -> ch, CURLOPT_URL, $url );
        return curl_exec( $this -> ch );
    }

    public function changeProxyIdentity() {
        $ip = '127.0.0.1';
        $port = '9051';

        $fp = fsockopen( $ip, $port, $error_number, $err_string, 10 );

        if ( !$fp ) {
            echo "Error while changing Tor proxy identity: {$error_number} : {$err_string}";
            return false;
        } else {
            fwrite( $fp, "AUTHENTICATE\n" );
            $received = fread( $fp, 512 );
            fwrite( $fp, "signal NEWNYM\n" );
            $received = fread( $fp, 512 );
        }
        sleep(1);
        fclose( $fp );
        return $received;
    }

    function __destruct() {
        curl_close( $this -> ch );
    }
}


вот так отправляю запрос
$proxy = new Proxy();
        $curlData = $proxy->curl($page);
        $html = HtmlDomParser::str_get_html($curlData);
        $find = $this->is_find($html, ".leftcol > .area > .listItem");


после чего делаю проверку и узнаю пришли ли данные страницы, Если данные не пришли то меняет IP и отправляем повторно CURL

if ($find == 0) {
                $proxy = new Proxy();
                $proxy->changeProxyIdentity();
                $this->new_ip = $this->getIp();
                $curlData = $proxy->curl($page);
                $html = HtmlDomParser::str_get_html($curlData);
                $find = $this->is_find($html, ".leftcol > .area > .listItem");

                echo 'new: '.$this->new_ip . '| old: '. $this->old_ip;
                dd($find);
        }


Смена IP проходит но почему то контент не приходит, есть предположения что при вызове changeProxyIdentity() не обновляется опция curl_setopt( $this -> ch, CURLOPT_PROXY, $torSocks5Proxy ) потому что она в конструкторе

Каким образом это можно исправить?
  • Вопрос задан
  • 110 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы