I
I
Igor Agarkov2019-04-07 14:25:20
PHP
Igor Agarkov, 2019-04-07 14:25:20

Does cURL PHP require any special proxies?

I bought a proxy from a certain site. 4 pieces. All private, with login and password. I have a PHP code that fetches a page using cURL (I took ip-api.com/json/ for testing). The checker on their website says that all 4 proxies are valid. My code is only able to work with one of the four. On the other three, instead of the page, it gives out "407 Proxy Authentication Required". This is fine? Is it the proxy or the code? Or maybe some special proxies are needed for cURL?

My code
class mycurl {
    protected $_url; 
    protected $_headers = []; 
    protected $_post; 
    protected $_postFields; 
    protected $_proxy; 
    protected $_proxyAddr; 
    protected $_proxyPort; 
    protected $_proxyUserPass; 
    protected $_useragent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36';
    protected $_referer = "https://www.google.com"; 		
    
    protected $_timeout = 10; 
    protected $_maxRedirects = 4; 
    protected $_followlocation = true; 
    protected $_includeHeader = true; 
    
    protected $_info;


    public function __construct($url,
                  $headers = null,
                  $post = false,
                  $postFields = null,
                  $isproxy = false,
                  $proxy = null,
                  $includeHeader = true) { 
      $this->_url = $url; 
      ($headers == null) ? $this->_headers = [] : $this->_headers = $headers; 
      $this->_post = $post; 
      $this->_postFields = $postFields; 
      $this->_proxy = $isproxy; 
      $this->_includeHeader = $includeHeader; 
      
      if ($isproxy) {
        $proxy_arr = explode(":", $proxy);
        if (count($proxy_arr) == 4) {
          $this->_proxyAddr = $proxy_arr[0]; 
          $this->_proxyPort = $proxy_arr[1]; 
          $this->_proxyUserPass = $proxy_arr[2] . ":" . $proxy_arr[3]; 
        }
        else {
          $this->_proxyAddr = $proxy_arr[0]; 
          $this->_proxyPort = $proxy_arr[1]; 
          $this->_proxyUserPass = null; 
        }
      }
      else {
        $this->_proxyAddr = null; 
        $this->_proxyPort = null; 
        $this->_proxyUserPass = null; 
      }
    } 

    public function GetResponse() {
      $s = curl_init(); 

      curl_setopt($s, CURLOPT_RETURNTRANSFER,		true); 
      curl_setopt($s, CURLOPT_SSL_VERIFYPEER, 	false);
      curl_setopt($s, CURLOPT_URL,				$this->_url); 
      curl_setopt($s, CURLOPT_HTTPHEADER,			$this->_headers); 
      curl_setopt($s, CURLOPT_TIMEOUT,			$this->_timeout); 
      curl_setopt($s, CURLOPT_MAXREDIRS,			$this->_maxRedirects); 
      curl_setopt($s, CURLOPT_FOLLOWLOCATION,		$this->_followlocation); 
      
      if ($this->_post) 
      { 
        curl_setopt($s, CURLOPT_POST, 			true); 
        curl_setopt($s, CURLOPT_POSTFIELDS, 	$this->_postFields); 
      } 

      if ($this->_proxy) {
        curl_setopt($s, CURLOPT_PROXYTYPE, 				'HTTP');
        curl_setopt($s, CURLOPT_HTTPPROXYTUNNEL, 	 	0);
        curl_setopt($s, CURLOPT_PROXY, 					$this->_proxyAddr);
        curl_setopt($s, CURLOPT_PROXYPORT, 				$this->_proxyPort);
        if ($this->_proxyUserPass != null) {
          curl_setopt($s, CURLOPT_PROXYUSERPWD, 		$this->_proxyUserPass);
          curl_setopt($s, CURLOPT_PROXYAUTH, 			CURLAUTH_BASIC);
        }
      }
      
      if ($this->_includeHeader) curl_setopt($s,	CURLOPT_HEADER,true); 
      
      
      curl_setopt($s, CURLOPT_USERAGENT, 			$this->_useragent); 
      curl_setopt($s, CURLOPT_REFERER, 			$this->_referer); 

      $response = curl_exec($s); 
      $this->_info = curl_getinfo($s); 
      
      curl_close($s);
      
      return $response;
    }
    
    public function GetInfo() {
      return $this->_info;
    }
} 


      $MyCURL = new mycurl(
        "http://ip-api.com/json/",
        false,
        false,
        null,
        true,
        GetProxy(),
        true
      );
      
      $result = $MyCURL->GetResponse();
      echo $result;

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2019-04-07
@dimonchik2013

Are you sure you pass the login pass from a specific one and not from the first one?

M
makag, 2019-04-07
@makag

depending on the type of proxy - different parameters.
HTTP proxy:

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);

HTTPS proxy:
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS); 
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);

SOCKS4 proxy:
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);

curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);

SOCKS5 proxy without authorization:
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);

SOCKS5 proxy with login and password authorization:
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_pass);

As for https proxy with authorization by login / password, socks4a proxy - I won’t say anything, I haven’t come across them.
In addition, you may have a proxy with authorization by ip. Then just find out the external ip of the server and set this binding in the settings in the store where you purchased the proxy.
PS If the proxy type is not known for certain (public proxies from obscure sources), then consistently make a request and look at the answer. When working with public proxy lists, I do just that. At the same time, you can cut off proxies with unacceptable response times. In the piggy bank - determining the type of proxy using php / curl

I
Igor Agarkov, 2019-04-08
@id01

Thank you all, I made a crutch through Python requests. For some reason it works with a proxy adequately

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question