How to use CURL via a proxy? – Stack Overflow
I am looking to set curl to use a proxy server. The url is provided by an html form, which has not been a problem. Without the proxy it works fine. I have found code on this and other sites, but they do not work. Any help in finding the correct solution would be much appreciated. I feel that the bellow are close, but that I am missing something. Thank You.
The bellow code I adapted from here but it returns an error message about a missing T_VARIABLE on line 12.
$url = '$_POST'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); curl_setopt($ch, CURLOPT_PROXY, '66. 96. 200. 39:80'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt ($ch, CURLOPT_HEADER, 1) curl_exec ($ch); $curl_info = curl_getinfo($ch); curl_close($ch); echo '
The bellow is from curl through proxy returns no content
$proxy = “66. 39:80”;
$proxy = explode(‘:’, $proxy);
$url = “$_POST”;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy);
curl_setopt($ch, CURLOPT_HEADER, 1);
$exec = curl_exec($ch);
echo $exec;? >
is currently live on but also does not work.
Thank you for all your help, I made the above changes. Now it only returns a blank screen.
$url = $_POST[‘1’];
curl_setopt ($ch, CURLOPT_HEADER, 1);
$curl_scraped_page = curl_exec($ch);
echo $curl_scraped_page;? >
PHP: Using cURL with a proxy. – This Interests Me
This is a guide on how to use a proxy with PHP’s cURL functions. In this tutorial, we will send our HTTP request via a specific proxy IP and port.
Why use a proxy?
There are various reasons why you might want to use a proxy with cURL:
To get around regional filters and country blocks.
Using a proxy IP allows you to mask your own IP address.
To debug network connection issues.
Using a proxy with PHP’s cURL functions.
Take a look at the following PHP code, which you can use to authenticate with a proxy via cURL and send a HTTP GET request.
//The URL you want to send a cURL proxy request to.
$url = ”;
//The IP address of the proxy you want to send
//your request through.
$proxyIP = ‘1. 2. 3. 4’;
//The port that the proxy is listening on.
$proxyPort = ‘1129’;
//The username for authenticating with the proxy.
$proxyUsername = ‘myusername’;
//The password for authenticating with the proxy.
$proxyPassword = ‘mypassword’;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
//Set the proxy IP.
curl_setopt($ch, CURLOPT_PROXY, $proxyIP);
//Set the port.
curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
//Specify the username and password.
curl_setopt($ch, CURLOPT_PROXYUSERPWD, “$proxyUsername:$proxyPassword”);
//Execute the request.
$output = curl_exec($ch);
//Check for errors.
throw new Exception(curl_error($ch));}
//Print the output.
In the code snippet above, we connected to a proxy that requires authentication before sending a simple GET request.
If the proxy in question does not require authentication, then you can omit the CURLOPT_PROXYUSERPWD line from your code.
“Failed to connect to 1. 4 port 1129: Timed out”
This means that cURL could not connect to the proxy on that IP and port. Make sure that both the IP and port are correct and that the proxy is operating correctly.
“Failed to connect to 1. 4 port 1129: Connection refused”
This error usually occurs when you have specified an incorrect port number. i. e. The IP address of the proxy was correct, but it is not listening for requests on that port. There is also the possibility that the server is up, but the software that runs the proxy is not running.
“Received HTTP code 407 from proxy after CONNECT”
The username and password combination that you are using with CURLOPT_PROXYUSERPWD is incorrect. Make sure that you are separating the username and password by a colon: character.
Using proxy servers with cURL in PHP | Beamtic
Tutorial on how to use proxy servers with cURL and PHP
Edited: 2021-04-12 08:45
Setting a proxy server to be used with cURL and PHP is relatively simple, it mostly depends on the server that you are using, and authentication method (if any). The HTTP authentication method is controlled with the CURLOPT_PROXYAUTH option, the default method is CURLAUTH_BASIC – if the proxy requires authentication, a username and password can be set in the [username]:[password] format, using the CURLOPT_PROXYUSERPWD option.
For now we’ll just focus on using a proxy that doesn’t require any authentication. Setting a proxy server and a port number in PHP for cURL can be done using the CURLOPT_PROXY option, like shown in the below example:
curl_setopt($ch, CURLOPT_PROXY, ‘128. 0. 3:8080’);
As shown in the above example, you can set the a proxy with the IP:PORT syntax in PHP using cURL. But if you prefer to keep the ip seperated from the port, you can also use the CURLOPT_PROXYPORT option, which would result in the below PHP code:
curl_setopt($ch, CURLOPT_PROXY, ‘128. 3’);
curl_setopt($ch, CURLOPT_PROXYPORT, ‘8080’);
After setting a proxy server, you will be able to perform the request using the curl_exec function. I. e.
$ch = curl_init($url);
$url = “;
// Perform the request, and save content to $result
$result = curl_exec($ch);
Setting cURL Proxy Type
cURL supports two proxy types, the default is HTTP, and the other option is SOCKS5. You can set the proxy type using the CURLOPT_PROXYTYPE option. e.
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
You really only need to set the type of the proxy, if you are not using a HTTP proxy.
Setting Authentication Method
As mentioned in the beginning of the tutorial, setting the authentication method of a proxy server can be done using the CURLOPT_HTTPAUTH option. To make this work properly, we will also need to provide a username and password for the proxy server, this is all accomplished in the below script, in which we are just using a BASIC authentication method.
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// The username and password
curl_setopt($ch, CURLOPT_PROXYUSERPWD, ‘USERNAME:PASSWORD’);
Other authentication methods include the following:
vertical bar | (or) operator can be used to combine methods. If this is done, cURL will poll the server to see what methods it supports and pick the best.
Your User Agent
Your Request Headers
You can use the following API endpoints for testing purposes:
How to use the AVIF image format in PHP; A1 or AVIF is a new image format that offers better compression than WebP, JPEG and PNG, and that already works in Google Chrome. How to create a router in PHP to handle different request types, paths, and request parameters. How much faster is C++ than PHP to increment and display a counter in a loop? Detecting the request method used to fetch a web page with PHP, routing, HTTP responses, and more. How to create a custom error handler for PHP that handles non-fetal in: PHP Tutorials