当前位置:首页 > PHP

php实现代理服务器

2026-01-29 07:56:07PHP

实现 PHP 代理服务器的基本方法

PHP 可以通过流上下文(Stream Context)或 cURL 扩展实现代理服务器功能。以下是两种常见实现方式:

使用 stream_context_create 实现

通过 PHP 内置的流功能创建代理请求:

<?php
$targetUrl = $_GET['url'] ?? '';
if (empty($targetUrl)) {
    die('Missing URL parameter');
}

$context = stream_context_create([
    'http' => [
        'method' => $_SERVER['REQUEST_METHOD'],
        'header' => getallheaders(),
        'ignore_errors' => true
    ]
]);

$response = file_get_contents($targetUrl, false, $context);
header('Content-Type: ' . $http_response_header[0]);
echo $response;
?>

使用 cURL 扩展实现

更强大的代理实现方式,支持更多协议和选项:

<?php
$targetUrl = $_GET['url'] ?? '';
if (empty($targetUrl)) {
    die('Missing URL parameter');
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);

// 传递原始请求头
$headers = [];
foreach (getallheaders() as $name => $value) {
    $headers[] = "$name: $value";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);

// 转发响应头
foreach (explode("\r\n", $headers) as $header) {
    if (!empty($header)) {
        header($header);
    }
}

echo $body;
curl_close($ch);
?>

安全性注意事项

实现代理服务器时需要考虑以下安全措施:

php实现代理服务器

验证目标URL,防止SSRF攻击:

$allowedDomains = ['example.com', 'trusted-site.com'];
$parsedUrl = parse_url($targetUrl);
if (!in_array($parsedUrl['host'], $allowedDomains)) {
    die('Access to this domain is not allowed');
}

限制请求大小防止滥用:

$maxSize = 1024 * 1024; // 1MB
if (strlen($response) > $maxSize) {
    die('Response too large');
}

性能优化建议

对于高频使用的代理服务器,可以考虑以下优化:

php实现代理服务器

启用缓存减少重复请求:

$cacheKey = md5($targetUrl);
$cacheFile = "/tmp/proxy_cache_$cacheKey";

if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < 3600)) {
    readfile($cacheFile);
    exit;
}

// ...处理请求...

file_put_contents($cacheFile, $response);

使用连接池保持持久连接:

curl_setopt($ch, CURLOPT_FORBID_REUSE, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, false);

完整代理服务器示例

结合上述技术的完整实现:

<?php
// 安全配置
$allowedDomains = ['example.com', 'trusted-site.com'];
$maxSize = 1024 * 1024; // 1MB

// 获取目标URL
$targetUrl = $_GET['url'] ?? '';
if (empty($targetUrl)) {
    http_response_code(400);
    die('Missing URL parameter');
}

// 验证目标域名
$parsedUrl = parse_url($targetUrl);
if (!isset($parsedUrl['host']) || !in_array($parsedUrl['host'], $allowedDomains)) {
    http_response_code(403);
    die('Access to this domain is not allowed');
}

// 初始化cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);

// 传递请求头
$headers = [];
foreach (getallheaders() as $name => $value) {
    if (strtolower($name) !== 'host') { // 排除Host头
        $headers[] = "$name: $value";
    }
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// 执行请求
$response = curl_exec($ch);
if (strlen($response) > $maxSize) {
    http_response_code(413);
    die('Response too large');
}

// 处理响应
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);

// 转发响应头
foreach (explode("\r\n", $headers) as $header) {
    if (!empty($header)) {
        header($header);
    }
}

echo $body;
curl_close($ch);
?>

以上代码提供了PHP实现代理服务器的基本框架,可根据实际需求进行调整和扩展。

分享给朋友:

相关文章

php实现登录

php实现登录

PHP 登录功能实现 数据库连接与用户表创建 创建一个数据库连接文件 db.php,用于管理数据库连接。MySQL 示例代码如下: <?php $host = 'localhost'; $db…

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。链表的头节点是访问整个链表的入口。 单链表的节点类实现 在PHP中,可以通过类来定义链表节点。每个…

php 线程实现

php 线程实现

PHP 线程实现方法 PHP 本身是单线程语言,但可以通过扩展或库实现多线程功能。以下是几种常见的实现方式: 使用 pthreads 扩展 pthreads 是一个 PHP 扩展,允许在 PHP 中…

php实现推送消息推送消息

php实现推送消息推送消息

PHP 实现消息推送的方法 使用 WebSocket 实现实时推送 WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议,适合实现实时消息推送。 安装 Ratchet 库(WebSo…

php https实现

php https实现

PHP 中实现 HTTPS 的方法 在 PHP 中实现 HTTPS 主要通过配置服务器和编写代码来确保通信的安全性。以下是几种常见的方法: 配置服务器支持 HTTPS 确保服务器已安装 SSL/T…

php 链表实现

php 链表实现

在 PHP 中实现链表可以通过类来模拟链表节点(Node)和链表(LinkedList)结构。以下是链表的完整实现示例,包含基础操作如插入、删除、遍历等。 链表节点类(Node) 链表的基本单元是节…