标签 PHP 下的文章

参考了别人的脚本,稍微修改了一下,目前还没发现bug。

如有问题请评论区告诉我。

$sqlInfo = array(
    'host' => 'localhost',
    'port' => '3306',
    'username' => 'root', // 只是示例
    'password' => '1234', // 只是示例
    'database' => '',
    'charset' => 'utf8mb4'
);
function sqlBackUp($sqlInfo, $filename) {
    $link = mysqli_connect($sqlInfo['host'], $sqlInfo['username'], $sqlInfo['password'], $sqlInfo['database'], $sqlInfo['port']);
    mysqli_options($link, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
    mysqli_query($link, "set names ".$sqlInfo['charset']);
    $tables = mysqli_query($link, 'SHOW TABLES FROM '.$sqlInfo['database'].'');
    $tabList = array();
    while ($row = mysqli_fetch_row($tables)) {
        $tabList[] = $row[0];
    }
    $mysql = '';
    foreach ($tabList as $val) {
        $sql = "show create table ".$val;
        $res = mysqli_query($link, $sql);
        $row = mysqli_fetch_array($res);
        $info = "DROP TABLE IF EXISTS `".$val."`;\r\n";
        $mysql .= $info.$row[1].";\r\n\r\n";
        mysqli_free_result($res);
    }
    foreach ($tabList as $val) {
        $sql = "select * from ".$val;
        $res = mysqli_query($link, $sql);
        if (mysqli_num_rows($res) < 1) {
            continue;
        }
        while ($row = mysqli_fetch_row($res)) {
            $mysql .= "INSERT INTO `".$val."` VALUES (";
            foreach ($row as $zd) {
                if (gettype($zd) == 'NULL') {
                    $mysql .= "NULL, ";
                } else if (gettype($zd) == 'string') {
                    $mysql .= "'".addslashes($zd)."', ";
                } else {
                    $mysql .= $zd.", ";
                }
            }
            $mysql = substr($mysql, 0, strlen($mysql)-2);
            $mysql .= ");\r\n";
        }
        mysqli_free_result($res);
        $mysql .= "\r\n";
    }
    mysqli_close($link);
    $fp = fopen($filename, 'w');
    fputs($fp, $mysql);
    fclose($fp);
}

不到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;
    }

?>