- Saved searches
- Use saved searches to filter your results more quickly
- License
- zounar/php-proxy
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Using proxy servers with PHP’s build-in HTTP functions
- Proxy Authentication
- Tools:
- PHP: Using cURL with a proxy.
- Why use a proxy?
- Using a proxy with PHP’s cURL functions.
- “Failed to connect to 1.2.3.4 port 1129: Timed out”
- “Failed to connect to 1.2.3.4 port 1129: Connection refused”
- “Received HTTP code 407 from proxy after CONNECT”
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
License
zounar/php-proxy
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Add static properties to control curlopt options
Git stats
Files
Failed to load latest commit information.
README.md
This proxy script allows you to forward all HTTP/HTTPS requests to another server. Works for all common request types including GET, POST requests with files, PATCH and PUT requests. It has minimal set of requirements (PHP >=5.6, libcurl, gzip) which are available even on the smallest free hostings and has its own simple authorization and cookie support.
- Copy the Proxy.php script to publicly-accessible folder of a PHP web server (the script is standalone and has no PHP dependencies)
- Make a cURL request targeting this script
- Add Proxy-Auth header with auth key found here
- Add Proxy-Target-URL header with URL to be requested by the proxy
- (Optional) Add Proxy-Debug header for debug mode
In order to protect using proxy by unauthorized users, consider changing Proxy-Auth token in proxy source file and in all your requests.
This might be useful when you want to redirect requests coming into your app.
- Run composer require zounar/php-proxy
- Add Proxy::run(); line to where you want to execute it (usually into a controller action)
- In this example, the script is in AppController — actionProxy :
use Zounar\PHPProxy\Proxy; class AppController extends Controller < public function actionProxy() < Proxy::$AUTH_KEY = ''; // Do your custom logic before running proxy $responseCode = Proxy::run(); // Do your custom logic after running proxy // You can utilize HTTP response code returned from the run() method > >
In order to protect using proxy by unauthorized users, consider changing Proxy-Auth token by calling Proxy::$AUTH_KEY = »; before Proxy::run() . Then change the token in all your requests.
Following example shows how to execute GET request to https://www.github.com. Proxy script is at http://www.foo.bar/Proxy.php. All proxy settings are kept default, the response is automatically echoed.
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com' )); curl_exec($request);
In order to show some debug info from the proxy, add Proxy-Debug: 1 header into the request. This will show debug info in plain-text containing request headers, response headers and response body.
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com', 'Proxy-Debug: 1' )); curl_exec($request);
Some sites may return different content for different user agents. In such case add User-Agent header to cURL request, it will be automatically passed to the request for target site. In this case it’s Firefox 70 for Ubuntu.
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0', 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com' )); curl_exec($request);
Error 301 Moved permanently
It might occur that there’s a redirection when calling the proxy (not the target site), eg. during http -> https redirection. You can either modify/fix the proxy URL (which is recommended), or add CURLOPT_FOLLOWLOCATION option before curl_exec .
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com' )); curl_exec($request);
Save response into variable
The default cURL behavior is to echo the response of curl_exec . In order to save response into variable, all you have to do is to add CURLOPT_RETURNTRANSFER cURL option.
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_RETURNTRANSFER, true); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com' )); $response = curl_exec($request);
About
Using proxy servers with PHP’s build-in HTTP functions
How to perform HTTP requests trough proxy servers in PHP.
Sometimes, for whatever reason, you may need to hide your own IP address. A common way to hid your IP is to use a proxy server to process your HTTP requests. Setting up your browser to use a proxy server is easy enough, but setting up scripts and programs to use proxies can require a bit more effort. This tutorial shows you how to make your PHP scripts use a proxy when performing HTTP requests with functions such as fread and file_get_contents.
If you remember the example from the last tutorial, adding a proxy can be done as easily as demonstrated in the below example:
$sURL = "http://beamtic.com/Examples/ip.php"; // The Request URL $aHTTP['http']['proxy'] = 'tcp://127.0.0.1:8118'; // The proxy ip and port number $aHTTP['http']['request_fulluri'] = true; // use the full URI in the Request. I.e. http://beamtic.com/Examples/ip.php $aHTTP['http']['method'] = 'GET'; $aHTTP['http']['header'] = "User-Agent: My PHP Script\r\n"; $aHTTP['http']['header'] .= "Referer: http://beamtic.com/\r\n"; $context = stream_context_create($aHTTP); $contents = file_get_contents($sURL, false, $context); echo 'Real IP:' . $_SERVER['REMOTE_ADDR'] . '
'; echo 'Proxy IP:'. $contents;As in the prior Tutorials, you can also use the URL provided in this example for testing purposes. ip.php is a simple php script outputting the IP of the client. If this is different than your own IP, the proxy works.
Proxy Authentication
If the proxy that you are using requires authentication, you will simply add the proxy authorization http header, in a manner similar to what is shown in the last php tutorial. Logging in to a proxy is done by combining the username and password, only separated by a single colon «:», then encoding it using base64, and send it in the Proxy-Authorization http header like shown in below example:
$sLogin = base64_encode('username:password'); $sURL = "http://beamtic.com/Examples/ip.php"; // The Request URL $aHTTP['http']['proxy'] = 'tcp://127.0.0.1:8118'; // The proxy ip and port number $aHTTP['http']['request_fulluri'] = true; // Use the full URI in the Request. I.e. http://beamtic.com/Examples/ip.php $aHTTP['http']['method'] = 'GET'; $aHTTP['http']['header'] = "User-Agent: My PHP Script\r\n"; $aHTTP['http']['header'] .= "Referer: http://beamtic.com/\r\n"; $aHTTP['http']['header'] .= "Proxy-Authorization: Basic $sLogin"; $context = stream_context_create($aHTTP); $contents = file_get_contents($sURL, false, $context); echo 'Real IP:' . $_SERVER['REMOTE_ADDR'] . '
'; echo 'Proxy IP:'. $contents;Tools:
You can use the following API endpoints for testing purposes:
https://beamtic.com/api/user-agent
https://beamtic.com/api/request-headers
PHP: Using cURL with a proxy.
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 = 'http://php.net'; //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. if(curl_errno($ch)) < throw new Exception(curl_error($ch)); >//Print the output. echo $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.2.3.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.2.3.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.