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.
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.
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 18.104.22.168 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 22.214.171.124 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.
- Why use Guzzle Instead of cURL in PHP ?
- PHP | cURL
- How to find where the URL will redirected using cURL?
- How to get cookies from curl into a variable in PHP ?
- How to Install php-curl in Ubuntu ?
- How to enable cURL in PHP?
- Difference between file_get_contents and cURL in PHP
- How to install the ext-curl extension with PHP 7 ?
- Creating a Proxy Webserver in Python | Set 1
- Creating a Proxy Webserver in Python | Set 2
- jQuery | $.proxy() Method
- HTTP headers | Proxy-Authenticate
- HTTP headers | Proxy-Authorization
- What is Zed Attack Proxy?
- HTTP headers | Via
- How to get history changes notification via history.pushState() method ?
- Messaging Via Azure Service bus | SendMessage and ScheduleMessage
- Template Rendering via Marko.js in Node.js
- How to send button value to PHP backend via POST using ajax ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.