当前位置:首页 > 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);
?>

安全性注意事项

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

验证目标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');
}

性能优化建议

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

启用缓存减少重复请求:

$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实现代理服务器

<?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 实现打印功能的方法 PHP 可以通过多种方式实现打印功能,具体取决于打印的目标(如打印机、PDF、网页等)和需求(如直接打印、生成打印预览等)。以下是几种常见的实现方式: 使用 JavaSc…

php实现websocket

php实现websocket

PHP 实现 WebSocket WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议。PHP 可以通过多种方式实现 WebSocket 功能,以下是几种常见的方法: 使用 Ratc…

php 实现文件下载

php 实现文件下载

实现文件下载的基本方法 使用 PHP 实现文件下载的核心是通过设置 HTTP 头部信息,强制浏览器将文件作为附件下载而非直接显示。以下是一个基础实现示例: $file_path = '/path/t…

php实现跳转

php实现跳转

PHP 实现页面跳转的方法 在 PHP 中实现页面跳转有多种方式,以下是常见的几种方法及其适用场景: 使用 header() 函数 通过 header() 函数发送 HTTP 头部信息实现跳转。注意…

php实现分类

php实现分类

PHP实现分类的方法 数据库设计 创建分类表时,通常需要包含id、名称、父级id等字段。父级id用于实现多级分类结构。 CREATE TABLE categories ( id INT AU…

php搜索功能实现的

php搜索功能实现的

PHP 搜索功能实现方法 数据库搜索实现 使用 SQL 的 LIKE 语句进行模糊匹配,适用于小型网站和简单搜索需求。 $searchTerm = $_GET['search']; $query =…