zounar/php-proxy
Composer 安装命令:
composer require zounar/php-proxy
包简介
Forward your HTTP/HTTPS requests to another server.
README 文档
README
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.
How to use
- 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.
How to use (via composer)
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 = '<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 } }
- In this example, the script is in
- Make a cURL request to your web
- In the example, it would be
http://your-web.com/app/proxy
- In the example, it would be
- 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 by calling
Proxy::$AUTH_KEY = '<your-new-key>'; before Proxy::run(). Then change the token in all your requests.
Usage example
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);
Debugging
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);
Specifying User-Agent
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);
zounar/php-proxy 适用场景与选型建议
zounar/php-proxy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.53k 次下载、GitHub Stars 达 183, 最近一次更新时间为 2020 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「curl」 「http」 「proxy」 「curlphp」 「http-proxy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zounar/php-proxy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zounar/php-proxy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zounar/php-proxy 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
cURL and cURL based Soap-Client for PHP
repository php library
Simple cURL wrapper
A fluent PHP CURL wrapper
http客户端
Easily add Excepct-CT header to your project
统计信息
- 总下载量: 4.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 183
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: unlicense
- 更新时间: 2020-12-07