百度翻译非vip接口2018 php版

最近在做游戏的汉化的时候,因为需要弄一个聊天插件来翻译,调用谷歌翻译的话感觉用语不是很符合我们的习惯,所以要用到百度翻译。发现百度翻译在2018年改成post方式并且带sign参数的了。
研究了一下发现,这个sign参数跟谷歌的tk参数是差不多的。网上也有人已经发了调试js之后找到的方法出来,对比后发现跟谷歌的差不多。以前有人发过谷歌的php调用接口,修改一下就能适用于百度翻译。
首先是计算一个固定值的方法,当然你是直接把这个固定值放进去也行:

function TKK()   

{

$a = 1394951105;  
$b = 1526272306;  
return 320305 . '.' . ($b - $a);  

}

计算偏移值的方法:

function RL($a, $b)  

{

for($c = 0; $c < strlen($b) - 2; $c +=3) {  
    $d = $b{$c+2};  
    $d = $d >= 'a' ? charCodeAt($d,0) - 87 : intval($d);  
    $d = $b{$c+1} == '+' ? shr32($a, $d) : $a << $d;  
    $a = $b{$c} == '+' ? ($a + $d & 4294967295) : $a ^ $d;  
}  
return $a;  

}

需要引用到的方法:

 //这个就是javascript的charCodeAt  

//PHP版本的在这里http://www.phpjiayuan.com/90/225.html
function 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;  
}  

}
//这个函数是无符号右移
//参考http://www.shangxueba.com/jingyan/1911053.html
function shr32($x, $bits)
{

if($bits <= 0){  
    return $x;  
}  
if($bits >= 32){  
    return 0;  
}  

$bin = decbin($x);  
$l = strlen($bin);  

if($l > 32){  
    $bin = substr($bin, $l - 32, 32);  
}elseif($l < 32){  
    $bin = str_pad($bin, 32, '0', STR_PAD_LEFT);  
}  

return bindec(str_pad(substr($bin, 0, 32 - $bits), 32, '0', STR_PAD_LEFT));  

}

计算sign的方法:

function sign($str){

$str = str_replace(array("/r/n", "/r", "/n"), "", $str);
$slen = mb_strlen($str,'utf-8');//获取中文长度
if($slen > 30) {
$n= floor($slen/2)-5;
$str = "".mb_substr($str,0,10,'utf-8').mb_substr($str,$n,10,'utf-8').mb_substr($str,-10,10,'utf-8');
}
$tkk = explode('.', TKK());

$h = $tkk[0]; 
$i = $tkk[1];

for($d = array(), $e = 0, $f = 0; $f <$slen; $f++) {
$g= charCodeAt($str,$f);
if (128 > $g) {

        $d [$e++] = $g;  
    } else {  
        if (2048 > $g) {  
            $d [$e++] = $g >> 6 | 192;  
        } else {  
            if (55296 == ($g & 64512) && $f + 1 < mb_strlen ( $str, 'UTF-8' ) && 56320 == (charCodeAt ( $str, $f+1 ) & 64512)) {  
                $g = 65536 + (($g & 1023) << 10) + (charCodeAt ( $str, ++$f ) & 1023);  
                $d [$e++] = $g >> 18 | 240;  
                $d [$e++] = $g >> 12 & 63 | 128;  
            } else {  
                $d [$e++] = $g >> 12 | 224;  
                $d [$e++] = $g >> 6 & 63 | 128;  
            }  
        }  
        $d [$e++] = $g & 63 | 128;  
    } 

}
$s = $h;
for($ss=0;$ss<count ( $d );$ss++){
$s+=$d[$ss];
$s= RL($s,"+-a^+6");
}
$s = RL ($s,"+-3^+b+-f" );
$s ^= $i;
if (0 > $s) {

    $s = ($s & 2147483647) + 2147483648;  
}  

$s = fmod ( $s, pow ( 10, 6 ) );
return $s . "." . ($s ^ $h);
}

百度翻译除了sign值之外,还需要获取token值才行:
function huoqu(){
$cookie_file = dirname(__FILE__).'/bdcookie.txt';
$url = "http://fanyi.baidu.com/#zh/en/%E4%BD%A0%E5%A5%BD";
$ch = curl_init();

 curl_setopt($ch, CURLOPT_URL, $url); 
 curl_setopt($ch, CURLOPT_HEADER, 1); 
 $header = array("Host:fanyi.baidu.com",
    'Content-Type:application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie:',//需要填写cookie
    'Referer:http://fanyi.baidu.com/',
  'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果把这行注释掉的话,就会直接输出 
 curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
 curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie_file); //存储cookies
 $result=curl_exec($ch); 
 curl_close($ch); 

// preg_match_all("%token: '(.*?)'%", $result, $matches);
preg_match("%token: '(.*?)'%", $result,$token);
return $token[1];
}
获取token之后就可以正式翻译了

function request_url_data($str,$f,$t)

{
if(empty($token)){
$token = huoqu();
}
loop:
$url = "http://fanyi.baidu.com/v2transapi";
$post_data =array();
$post_data['query']=$str;
$post_data['sign']=sign($str);
$post_data['from']=$f;
$post_data['to']=$t;
$post_data['simple_means_flag']=3;
$post_data['token']=$token;

$header = array("Host:fanyi.baidu.com",

    'Content-Type:application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie:BAIDUID=6ACA16DE0551132BDA0AD7C868BA2875:FG=1; __guid=37525047.3425545495696378400.1520760922495.3843; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BIDUPSID=6ACA16DE0551132BDA0AD7C868BA2875; PSTM=1520761193; BDUSS=lVub25vazk5S2taTGphZXE0UVowc2hqSDBqUUZ0OWpUdnFMV3FVa3NHSWhuTTlhQUFBQUFBJCQAAAAAAAAAAAEAAACm1h8SwdMxMjMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACEPqFohD6had; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=7; H_PS_PSSID=1446_21118_18559_17001; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; locale=zh; monitor_count=8; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1520856590,1520863193,1520944633,1521003596; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1521003596;',
    'Referer:http://fanyi.baidu.com/',
  'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');

$cookie = "xxxxxxxxxxx"; //注意填写cookie

$result = curl_request($url,$post_data,$cookie,1);

$s = $result["content"];
$s =json_decode($s);
if($s->error=="998"){
$token = huoqu();
goto loop;
}
if($s->error=="997" or !empty($s->error)){
return "对不起cookie过期!";
}
return $s->trans_result->data[0]->dst;
}
//参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
function curl_request($url,$post='',$cookie='', $returnCookie=0){

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
    if($post) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
    }
    if($cookie) {
        curl_setopt($curl, CURLOPT_COOKIE, $cookie);
    }
    curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    if (curl_errno($curl)) {
        return curl_error($curl);
    }
    curl_close($curl);
    if($returnCookie){
        list($header, $body) = explode("\r\n\r\n", $data, 2);
        preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
        $info['cookie']  = substr($matches[1][0], 1);
        $info['content'] = $body;
        return $info;
    }else{
        return $data;
    }

}
最后调用:request_url_data($str,$fL,$tL);

版权声明:若无特殊注明,本文皆为( )原创,转载请保留文章出处。
小劣博客

小劣博客

骨有三分傲,情得一点痴

正文到此结束

热门推荐

发表评论

    正在提交中...

    captcha
    请输入验证码

    已有 2 条评论