关于免费调用google翻译接口

google翻译的请求图

最重要的就是那个tk值,如果算不对..基本是翻译不出来的

file

给小伙伴们提供一个类,用于计算TK值调用google翻译

当然这个算法不是我写的...我只是当了搬运工..本来想贴出处的...真的忘记出之哪里了..sorry~

我这里用了guzzle来发送Http请求,,小伙伴们可以用curl也可以

<?php

namespace App\Handles;
use GuzzleHttp\Client;

class GoogleTranslate
{
    const GOOGLE_URL = 'https://translate.google.cn/translate_a/';
    const UA = 'Mozilla/5.0 (Android; Mobile; rv:22.0) Gecko/22.0 Firefox/22.0';

    public 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));
    }

    //这个就是javascript的charCodeAt
    //PHP版本的在这里http://www.phpjiayuan.com/90/225.html
    public 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;
        }
    }

    //直接复制google
    public function RL($a, $b)
    {
        for($c = 0; $c < strlen($b) - 2; $c +=3) {
            $d = $b{$c+2};
            $d = $d >= 'a' ? $this->charCodeAt($d,0) - 87 : intval($d);
            $d = $b{$c+1} == '+' ? $this->shr32($a, $d) : $a << $d;
            $a = $b{$c} == '+' ? ($a + $d & 4294967295) : $a ^ $d;
        }
        return $a;
    }

    //静态TKK,动态获取请使用另外一个方法
    public function TKK()
    {
        $a = 561666268;
        $b = 1526272306;
        return 406398 . '.' . ($a + $b);
    }

    //直接复制google
    public function TL($a)
    {

        $tkk = explode('.', $this->TKK());
        $b = $tkk[0];

        for($d = array(), $e = 0, $f = 0; $f < mb_strlen ( $a, 'UTF-8' ); $f ++) {
            $g = $this->charCodeAt ( $a, $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 ( $a, 'UTF-8' ) && 56320 == ($this->charCodeAt ( $a, $f + 1 ) & 64512)) {
                        $g = 65536 + (($g & 1023) << 10) + ($this->charCodeAt ( $a, ++ $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;
            }
        }
        $a = $b;
        for($e = 0; $e < count ( $d ); $e ++) {
            $a += $d [$e];
            $a = $this->RL ( $a, '+-a^+6' );
        }
        $a = $this->RL ( $a, "+-3^+b+-f" );
        $a ^= $tkk[1];
        if (0 > $a) {
            $a = ($a & 2147483647) + 2147483648;
        }
        $a = fmod ( $a, pow ( 10, 6 ) );
        return $a . "." . ($a ^ $b);
    }

    public function translate($sl, $tl, $q, $param = 't?client=webapp', $method = 'get')
    {

        $tk = $this->TL($q);
        $q = urlencode(stripslashes($q));
        $resultRegexes = array(
            '/,+/'  => ',',
            '/\[,/' => '[',
        );

        $url = self::GOOGLE_URL . $param . "&sl=".$sl."&tl=".$tl."&hl=".$tl."&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&ssel=0&tsel=0&kc=1&tk=". $tk ;
        if ( $method == 'get' ) $url .= "&q=" . $q;
        $client = new Client();
        $headers = [
            'User-Agent'=>self::UA
        ];
        $response = $client->request($method,$url,$headers);
        if ($response->getStatusCode() === 200) {
            return $response->getBody()->getContents();
        } else {
            return false;
        }
    }

调用方法

sl: 源语言 tl: 目标语言 q: 翻译的文本

return (new GoogleTranslate())->translate('zh','en','你好');

google官方付费api看这里

推荐各位小伙伴用这种,,支持google

下回再写吧...