How to use cURL via a proxy ? - GeeksforGeeks

01 Sep, 2020
This tutorial will show the way to use a proxy with PHP’s cURL functions. In this tutorial, we’ll send our HTTP request via a selected proxy IP address and port.
Why should you use a proxy?There are various reasons why you would possibly want to use a proxy with cURL:
To get around regional filters and country blocks.Using a proxy IP addresses allows you to mask or hide your own IP address.To debug network connection issues.
To get around regional filters and country blocks.
Using a proxy IP addresses allows you to mask or hide your own IP address.
To debug network connection issues.
Using a proxy with PHP’s cURL functions: To authenticate with a proxy via cURL and send a HTTP GET request follow along code given below and read the instructions specified as comments.
Note: All the credentials and links used are random and used for demo purpose only. Please use your own proxy, credentials and URL.
<?php  // Initialize URL you that want to// send a cURL proxy request to.$desturl = 'http://example.net';  // The IP address of the proxy that// you want to send your request// through$proxyipadd = '11.22.33.44';  // The port that the proxy is// listening on$proxyport = '1234';  // The username for authenticating// with the proxy$proxyuserid = 'testuser';  // The password for authenticating// with the proxy$proxypass = 'testpass';  // Initialize curl $ci = curl_init($url);  // Set curl attributescurl_setopt($ci, CURLOPT_RETURNTRANSFER, true);curl_setopt($ci, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ci, CURLOPT_HTTPPROXYTUNNEL , 1);  // Set the proxy IPcurl_setopt($ci, CURLOPT_PROXY, $proxyipadd);  // Set the portcurl_setopt($ci, CURLOPT_PROXYPORT, $proxyport);  // Set the username and passwordcurl_setopt($ci, CURLOPT_PROXYUSERPWD,             "$proxyuserid:$proxypass");  // Execute the request$result = curl_exec($ci);  // Check if any errorsif(curl_errno($ci)){    throw new Exception(curl_error($ci));}  // Print the result.echo $result;?>
In the above code, we connected to a proxy that needs authentication before sending an easy GET request. If the proxy doesn’t require authentication, then you could omit the CURLOPT_PROXYUSERPWD line from your code.
Some errors you might encounter while using curl:
“Failed to attach to 11.22.33.44 port 1234: Timed out” This means that cURL couldn’t hook up with the proxy IP address and port used. Make sure that both the IP and port are correct and also check if the proxy is working correctly.
“Failed to attach to 11.22.33.44 port 1234: Connection refused” This error usually occurs once you have specified an incorrect port number i.e. the IP address of the proxy was correct, but it’s not listening for requests on specified port. There is also the likelihood that the server is up, but the software that runs the proxy isn’t running.
“Received HTTP code 407 from proxy after CONNECT” The username and password combo that you simply are using with CURLOPT_PROXYUSERPWD is wrong. Make sure that username and password are correct and you are separating the username and password by a colon : character.
PHP-Misc
PHP
PHP Programs
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to fetch data from localserver database and display on HTML table using PHP ?
Different ways for passing data to view in Laravel
How to create admin login page using PHP?
Create a drop-down list that options fetched from a MySQL database in PHP
How to pass form variables from one page to other page in PHP ?
How to call PHP function on the click of a Button ?
How to fetch data from localserver database and display on HTML table using PHP ?
How to create admin login page using PHP?
How to calculate the difference between two dates in PHP ?
How to pass form variables from one page to other page in PHP ?