Stars: 127
Forks: 74
Pull Requests: 8
Issues: 9
Watchers: 6
Last Updated: 2023-04-08 21:54:38
Simple PHP proxy script
License: The Unlicense
Languages: PHP
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.
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.
composer require zounar/php-proxy
Proxy::run();
line to where you want to execute it (usually into a controller action)
AppController
- actionProxy
:
use Zounar\PHPProxy\Proxy;
class AppController extends Controller {
public function actionProxy() {
Proxy::$AUTH_KEY = '<your-new-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
}
}
http://your-web.com/app/proxy
In order to protect using proxy by unauthorized users, consider changing Proxy-Auth
token by calling
Proxy::$AUTH_KEY = '<your-new-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);
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);
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);