不到30行代码实现谷歌的反向代理,可以放在虚拟主机搭建谷歌搜索镜像站

注意 服务器需要开启curl,index.php文件必须放在根目录,apache服务器需要新建(修改)一个内容为FallbackResource /index.php.htaccess文件,其他服务器请自己配置对应的路由。

index.php:

<?php

    /*
        Google Mirror
        Put this file to the ROOT directory, and write "FallbackResource /index.php" to the .htaccess file.
    */

    $params = $_SERVER['REQUEST_URI'];
    $response = curl('https://www.google.com'.$params);
    $header = $response['header'];
    $data = $response['data'];
    $data = str_replace('src="https://www.google.com','src="',$data);
    $contentType = [];
    preg_match("/Content-Type:([^:]+);/",$header,$contentType);
    header("Content-Type:".$contentType[1]);
    echo $data;
    
    function curl($url) {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
        curl_setopt($ch,CURLOPT_HEADER,TRUE);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
        curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
        $data = curl_exec($ch);
        $headerSize = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
        curl_close($ch);
        $header = substr($data,0,$headerSize);
        $result = [
            'header' => $header,
            'data' => substr($data,$headerSize,-1),
        ];
        return $result;
    }

?>

标签: PHP

评论已关闭