PHP OOP CURL Proxy Error

<?php
class Isis_Expl
{
    const USERAGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13';

    public static $ch;

    public static $proxyType;
    public static $proxy;
    public static $postFields;
    public static $cookie;

    public static function setProxy($proxy, $proxyType = '1')
    {
        self::$proxy = $proxy;
        switch ($proxyType)
        {
            case '4':
                self::$proxyType = 'CURLPROXY_SOCKS4';
                break;
            case '5':
                self::$proxyType = 'CURLPROXY_SOCKS5';
                break;
            default:
                break;
        }
    }

    public static function init($url)
    {
        self::$ch = curl_init();
        curl_setopt(self::$ch, CURLOPT_URL, $url); 
        curl_setopt(self::$ch, CURLOPT_REFERER, $url);        
        curl_setopt(self::$ch, CURLOPT_HEADER, 1);
        curl_setopt(self::$ch, CURLOPT_USERAGENT, self::USERAGENT);
        curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt(self::$ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt(self::$ch, CURLOPT_TIMEOUT, 15);
        curl_setopt(self::$ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt(self::$ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        self::_getProxy();
        self::_getPost();
        self::_getCookie();

        $content = curl_exec(self::$ch); 

        if (curl_errno(self::$ch))
        {
            echo curl_error(self::$ch);
            return false;
        }
        else
        {
            return $content;
        }
    }

    protected function _getProxy()
    {
        if (self::$proxyType) curl_setopt(self::$ch, CURLOPT_PROXYTYPE, self::$proxyType);        
        if (self::$proxy) curl_setopt(self::$ch, CURLOPT_PROXY, self::$proxy);
    }

    protected function _getPost()
    {
        if (self::$postFields)
        {
            curl_setopt(self::$ch, CURLOPT_POST, 1);
            curl_setopt(self::$ch, CURLOPT_POSTFIELDS, self::$postFields);
        }
    }

    protected function _getCookie()
    {
        if (self::$cookie) curl_setopt(self::$ch, CURLOPT_COOKIE, self::$cookie);
    }   
}

Isis_Expl::setProxy('XXX.XXX.XXX.XXX:XXXX');
echo Isis_Expl::init('http://google.com');

Output: Error Recv: connection was reset

But if I commented on "//Isis_Expl::setProxy('XXX.XXX.XXX.XXX:XXXX ');" this output is ok .. where is the problem?

+3
source share
1 answer

First, do not use a static class when the instance is more appropriate. You have a state here, so use an instance. Secondly, you can better name your methods. _getProxy()I would think that I would return something, but would not affect the state of the application.

Now, for your exact question:

  • Is this an HTTP proxy? If yes, set CURLOPT_HTTPPROXYTUNNELto true?

  • proxyType 1 setProxy, self::$proxyType. CURLOPT_PROXYTYPE CURLPROXY_HTTP ( , ).

  • . ?

  • , - IP- ? ... , :

    $f  = fsockopen('XXX.XXX.XXX.XXX', 'XXXX');
    if (!$f) die('Server not accepting connections');
    fwrite($f, "q\r\n\r\n");
    var_dump(fread($f, 4048));
    fclose($f);
    
+2

Source: https://habr.com/ru/post/1794800/


All Articles