MENU

自己写的Google 翻译接口 大陆可用

October 30, 2019 • 已被 378 位童鞋围观过 • 代码分享

整理之前写的代码,之前写了这个垃圾站 准备做英文垃圾站的 然后没有做。分享出来算了,有需要的自取。
自己写的接口 有5000字数限制。但是文件上传没有字数限制,目前上传文件这个函数失效了,没有修复,有需要的联系。

<?php
function Google_Vq($a,$b='426833.230278880'){
    $d = explode(".", $b);
    $b = intval($d[0]);
    for ($e = array(), $f = 0, $g = 0; $g < mb_strlen($a); $g++) {
        $l = Google_charCodeAt($a, $g);
        if (128 > $l) {
            $e[$f++] = $l;
        } else {
            if (2048 > $l) {
                $e[$f++] = $l >> 6 | 192;
            } else {
                if (55296 == ($l & 64512) && $g + 1 < strlen($a) && 56320 == (Google_charCodeAt($a, $g + 1) & 64512)) {
                    $l = 65536 + (($l & 1023) << 10) + (Google_charCodeAt($a, ++$g) & 1023);
                    $e[$f++] = $l >> 18 | 240;
                    $e[$f++] = $l >> 12 & 63 | 128;
                } else {
                    $e[$f++] = $l >> 12 | 224;
                    $e[$f++] = $l >> 6 & 63 | 128;
                    $e[$f++] = $l & 63 | 128;
                }
            }
        }
    }
    $a = $b;
    for ($f = 0; $f < count($e); $f++) {
        $a += $e[$f];
        $a = Google_TQ($a, "+-a^+6");
    }
    $a = Google_TQ($a, "+-3^+b+-f");
    $a ^= floatval($d[1]);
    0 > $a && ($a = ($a & 2147483647) + 2147483648);

    $a = intval(fmod(floatval($a), 1000000));
    return (strval($a) . "." . strval(($a ^ $b)));
}
function Google_charCodeAt($str, $index)
{
    $char = mb_substr($str, $index, 1, 'UTF-8');

    if (mb_check_encoding($char, 'UTF-8')) {
        $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
        return hexdec(bin2hex($ret));
    } else {
        return null;
    }
}
function Google_TQ($a, $b)
{
    for ($c = 0; $c < strlen($b) - 2; $c += 3) {
        $d = $b[$c + 2];
        $d = "a" <= $d ? Google_charCodeAt($d, 0) - 87 : intval($d);
        $d = "+" == $b[$c + 1] ? Google_Demo($a, $d) : $a << $d;
        $a = "+" == $b[$c] ? $a + $d & 4294967295 : $a ^ $d;
    }
    return $a;
}
function Google_Demo($v, $y)
{
    $t = $v >> $y;
    $f = 0x7fffffff >> ($y - 1);
    return ($t & $f);
}
//获取TKK的值
function Google_GetTkk(){
    $tkfile="Google.tkk";
    if(!file_exists($tkfile)){
        $tkk = 0;
        $tmpInfo = Google_Get('https://translate.google.cn');
        //print_r($tmpInfo);
        if(preg_match("/tkk:\'(.*)\'/isU",$tmpInfo, $arr)){
            $tkk = $arr[1] ;
        }
        file_put_contents($tkfile,$tkk);
    }else{
        $tkk=file_get_contents($tkfile);
    }
    return $tkk;
}
function Google_Get($url,$cookie=""){ // 模拟提交数据函数
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
    curl_setopt($curl, CURLOPT_COOKIE, $cookie);
    curl_setopt($curl, CURLOPT_REFERER,$url);// 设置Referer
    curl_setopt($curl, CURLOPT_POST, 0); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $tmpInfo = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
       echo 'Errno'.curl_error($curl);//捕抓异常
    }
    curl_close($curl); // 关闭CURL会话
    return $tmpInfo; // 返回数据
}
function Google_Get_Zimu($str){ 
  $nword = explode(" ",$str); 
  foreach($nword as $letter){ 
    $new_name .= $letter{0}; 
  } 
  return strtoupper($new_name); 
} 
function Google_Post_UPload($data){
    $url="https://translate.googleusercontent.cn/translate_f";
    $header = array( //头部信息,上面的函数已说明
        'Accept:application/json, text/javascript, */*; q=0.01',
        'Accept-Charset:text/html,application/xhtml+xml,application/xml;q=0.7,*;q=0.3',
        'Accept-Encoding:gzip,deflate,sdch',
        'Accept-Language:zh-CN,zh;q=0.8',
        'Connection:keep-alive',
        'Content-Type: multipart/form-data; boundary=----WebKitFormBoundarykU2RB8zXt3dUA7lk',
        //'CLIENT-IP:'.$ip, 
        //'X-FORWARDED-FOR:'.$ip,
    );
    $curl = curl_init();  //这里并没有带参数初始化
    curl_setopt($curl, CURLOPT_URL, $url);//这里传入url
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);//对认证证书来源的检查,不开启次功能
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);//从证书中检测 SSL 加密算法
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    //模拟用户使用的浏览器,自己设置,我的是"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0" 
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);//自动设置referer
    curl_setopt($curl, CURLOPT_POST, 1);//开启post+
    curl_setopt($curl, CURLOPT_ENCODING, "gzip" );
    curl_setopt($curl, CURLOPT_REFERER,'https://translate.google.com/');// 设置Referer
    //HTTP请求头中"Accept-Encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型。
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//要传送的数据
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时限制,防止死循环
    curl_setopt($curl, CURLOPT_HEADER, 0);//0表示不输出Header,1表示输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $tmpInfo = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
       echo 'Errno'.curl_error($curl);//捕抓异常
    }
    curl_close($curl); // 关闭CURL会话
    return $tmpInfo; // 返回数据
}
function Google_UPload_Doc($file,$mod){//mod=1 本地读取 mod=2 远程获取 mod=其他 直接提交
    if($mod==1){
        $content=file_get_contents($file);
    }elseif($mod==2){
        $content=Google_Get($file);
    }else{
        $content=$file;
    }
    //定义POST数据
    $data   =    "------WebKitFormBoundarykU2RB8zXt3dUA7lk\r\n"
            ."Content-Disposition: form-data; name=\"file\"; filename=\"1.doc\"\r\n"
            ."Content-Type: application/msword\r\n"
            . "\r\n"
            . $content. "\r\n"
            . "------WebKitFormBoundarykU2RB8zXt3dUA7lk\r\n"
            . "Content-Disposition: form-data; name=\"sl\"\r\n"
            . "\r\n"
            . "zh-CN\r\n"                                             //原始语言
            . "------WebKitFormBoundarykU2RB8zXt3dUA7lk\r\n"
            . "Content-Disposition: form-data; name=\"tl\"\r\n"
            . "\r\n"
            . "en\r\n"                                                    //翻译结果语言
            . "------WebKitFormBoundarykU2RB8zXt3dUA7lk\r\n"
            . "Content-Disposition: form-data; name=\"js\"\r\n"
            . "\r\n"
            . "y\r\n"
            . "------WebKitFormBoundarykU2RB8zXt3dUA7lk\r\n"
            . "Content-Disposition: form-data; name=\"prev\"\r\n"
            . "\r\n"
            . "_t\r\n"
            . "------WebKitFormBoundarykU2RB8zXt3dUA7lk\r\n"
            . "Content-Disposition: form-data; name=\"hl\"\r\n"
            . "\r\n"
            . "en\r\n"
            . "------WebKitFormBoundarykU2RB8zXt3dUA7lk\r\n"
            . "Content-Disposition: form-data; name=\"ie\"\r\n"
            . "\r\n"
            . "UTF-8\r\n"
            . "------WebKitFormBoundarykU2RB8zXt3dUA7lk\r\n"
            . "Content-Disposition: form-data; name=\"text\"\r\n"
            . "\r\n"
            . "\r\n"
            . "------WebKitFormBoundarykU2RB8zXt3dUA7lk\r\n"
            . "Content-Disposition: form-data; name=\"edit-text\"\r\n"
            . "\r\n"
            . "\r\n"
            . "------WebKitFormBoundarykU2RB8zXt3dUA7lk";
    $html=Google_Post_UPload($data);
    return $html;
}

function Google($file,$mod){//mod=1 本地读取 mod=2 远程获取 mod=其他 直接提交
    $html=Google_UPload_Doc($file,$mod);
    preg_match_all("/<p style=margin:0pt>(.*)<\/p>/isU",$html,$htmlP);
    foreach($htmlP[1] as $k => $v){
        preg_match_all("/<span class=\"notranslate\"(.*)><span class=\"google-src-text\"(.*)><span(.*)>(.*)<\/span><\/span> <span(.*)>(.*)<\/span><\/span>/isU",$v,$htmlSpan);
        foreach($htmlSpan[4] as $m => $n){
            $co[$k]["Zh"] .= $htmlSpan[4][$m];
            $co[$k]["En"] .= $htmlSpan[6][$m];
        }

    }
    return $co;
}

function Google_API($word,$mod=""){//$mod=name的时候 翻译标题 返回值为 翻译结果,拼音,英文首字母大写缩写
    $word = htmlspecialchars_decode($word);
    $TKK = Google_GetTkk();
    $Tk = Google_Vq($word,$TKK);
    $url="https://translate.google.cn/translate_a/single?client=t&sl=zh-cn&tl=en&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&pc=1&otf=1&ssel=5&tsel=5&kc=1&tk=".$Tk."&q=".urlencode($word);
    $html = Google_Get($url);
    $json=json_decode($html,true);
    $translate=$json[0];
    foreach($translate as $k => $v){
        $data["Zh"] .= $v[1];
        $data["En"] .= $v[0];
        if(!empty($v[3])){
            $PinYin=$v[3];
        }
    }
    if($mod=="name"){
        $data["En"]=$data["En"].",".$PinYin.",".Google_Get_Zimu($data["En"]); 
    }
    return $data;

}
//Demo
//$word="八零锦绣俏甜妻";
//$json=Google_API($word,"name");
//print_r($json);
$word="江小荞重生了,重生在不知道的华国村庄,穷乡僻壤,家里人口众多,阴盛阳衰,三个妹妹,江小麦,江小米,江小谷,一家子都是软柿子,被人家捏来揉去的欺负。打到渣爹,翻身做主人。好在江小荞来了,从今天起姐姐妹妹站起来,挺胸抬头吃饱饭,顺带过上幸福生活。那人邪邪一笑,是我早就看中的媳妇,怎么能便宜别人。媳妇儿,碗里来吧!";
$json=Google_API($word);
print_r($json);
//exit();
//文件上传失效了 暂时没有修复,需要的联系我。
 //$file="https://yooer.me/zh.php上传文件的地址 或者远程地址 本地地址需要完整路径 或者数据直接提交 
 //$json=Google($file,2);//mod=1 本地读取 mod=2 远程获取 mod=其他 直接提交
 //print_r($json);
// exit();

Last Modified: September 28, 2023