用PHP写了个简单的镜像站程序

公司需要,然后写了个镜像站的程序,PHP7.4下写的,理论支持PHP8.3吧,程序很简单,但是能支持替换内容、写css和js。代码如下:

<?php
// 配置源网站地址和镜像网站地址
const SOURCE_URL = 'https://www.q.com';//源站
const MIRROR_URL = 'https://www.w.com';//镜像站点域名

// 获取请求的路径
$requestPath = $_SERVER['REQUEST_URI'];

// 构建源网站和镜像网站的完整地址
$sourceUrl = SOURCE_URL . $requestPath;
$mirrorUrl = MIRROR_URL . $requestPath;


// 获取原始 GET 参数
$originalGetParams = $_GET;

// 添加新的 GET 参数
$newGetParams = array(
    'biaoji' => 'jingxiang',
);

// 合并原始和新的 GET 参数
$mergedGetParams = array_merge($originalGetParams, $newGetParams);

// 构建带有 GET 参数的 URL
$sourceUrlWithParams = $sourceUrl . '?' . http_build_query($mergedGetParams);
$mirrorUrlWithParams = $mirrorUrl . '?' . http_build_query($mergedGetParams);


// 创建 cURL 实例
$ch = curl_init($sourceUrlWithParams);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 发起请求并获取响应
$response = curl_exec($ch);

// 获取响应的 HTTP 状态码
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// 关闭 cURL 实例
curl_close($ch);

// 替换响应中的源网址为镜像网址
$response = str_replace(SOURCE_URL, MIRROR_URL, $response);



// 对于WordPress静态文件,还原非必要地址
$response = str_replace(MIRROR_URL."/wp-admin", SOURCE_URL."/wp-admin", $response);
$response = str_replace(MIRROR_URL."/wp-content", SOURCE_URL."/wp-content", $response);
$response = str_replace(MIRROR_URL."/wp-includes", SOURCE_URL."/wp-includes", $response);
$response = str_replace(MIRROR_URL."/wp-content/uploads/", SOURCE_URL."/wp-content/uploads/", $response);
$response = str_replace("\"/wp-content/uploads/", '"'.SOURCE_URL."/wp-content/uploads/", $response);




// 替换内容,将www.q.com替换为www.w.com
$response = str_replace("www.q.com", "www.w.com", $response);


// 添加 CSS 样式
$css = <<<CSS
<style>
header .info-box-wrapper {
    display:none;
}
</style>
CSS;

// 将 CSS 插入到响应的 HTML 页面中
$pos = strpos($response, '</head>');
if ($pos !== false) {
    $response = substr_replace($response, $css, $pos, 0);
}

// 设置响应的状态码
http_response_code($httpStatus);

// 输出响应内容
echo $response;
© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称常用语 夸夸
夸夸
还有吗!没看够!
表情代码图片

    暂无评论内容