{"id":445,"title":"google\u7ffb\u8bd1\u7834\u89e3-\u52a0\u5bc6\u7b97\u6cd5","good":0,"bad":0,"hit":3260,"created_at":"2019-11-26 15:19:01","content":"

\u7b80\u5355\u7c97\u7206\u7684\u65b9\u5f0f\uff1a<\/p>

get https:\/\/translate.google.cn\/translate_a\/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl=\u6e90\u8bed\u8a00\u4ee3\u7801&tl=\u76ee\u6807\u8bed\u8a00\u4ee3\u7801&q=<\/a>\u8981\u7ffb\u8bd1\u7684\u6587\u5b57<\/p>

<\/p>

\u8fd9\u4e2a\u662f\u4ecegithub\u4e2d\u7684Stichoza\\GoogleTranslate\u4e2d\u62a0\u51fa\u6765\u7684\uff0c<\/p>

\u9879\u76ee\u5730\u5740\uff1ahttps:\/\/github.com\/Stichoza\/google-translate-php<\/a><\/p>

\u7528composer\u5b89\u88c5\uff1a<\/p>

composer require stichoza\/google-translate-php<\/p>

\u5c06vendor\\stichoza\\google-translate-php\\src\\GoogleTranslate.php\u4e2d\u7684<\/p>

https:\/\/translate.google.com\/translate_a\/single<\/p>

\u6539\u4e3a<\/p>

https:\/\/translate.google.cn\/translate_a\/single<\/p>

\u56fd\u4e3a\u6709\u5899<\/p>

<\/p>

\u9519\u8bef\u89e3\u51b3\u65b9\u6cd5\uff1acURL error 60: SSL certificate problem: unable to get local issuer certifica<\/p>

\u4e0b\u8f7dcacert.pem(https:\/\/curl.haxx.se\/ca\/cacert.pem);<\/p>

<\/p>

\u6253\u5f00php.ini  \u641c\u7d22curl.cainfo \u4e0e openssl.cafile,\u5c06\u5176\u914d\u7f6e\u6210\u4f60\u81ea\u5df1cacert.pem\u6587\u4ef6\u7684\u8def\u5f84<\/p>

curl.cainfo=" \u8def\u5f84 "<\/p>

openssl.cafile="\u8def\u5f84"<\/p>

<\/p>

\u5176\u5b9e\u5f88\u591a\u53c2\u6570\u4e0d\u4e00\u5b9a\u8981\uff0c\u4e0b\u9762\u662f\u4e2a\u4eba\u6574\u7406\u7684\u5fc5\u987b\u7684\u53c2\u6570<\/p>

google\u68c0\u6d4b\u8bed\u8a00\u63a5\u53e3\uff1a<\/p>

https:\/\/translate.google.cn\/translate_a\/single?client=webapp&sl=auto&q=china&tk=126587.515333<\/a><\/span><\/p>

q\u4e3a\u68c0\u6d4b\u7684\u6587\u5b57\uff0ctk\u7528\u4e0b\u5199\u7684\u7b97\u6cd5\u52a0\u5bc6\u751f\u6210\uff0c\u5176\u5b83\u4fdd\u6301\u9ed8\u8ba4 <\/span><\/p>

google\u7ffb\u8bd1\u63a5\u53e3\uff1a<\/p>

https:\/\/translate.google.cn\/translate_a\/single?client=webapp&sl=zh-CN&tl=en&dt=rm&dt=t&tk=792344.664678&q=\u4e2d\u56fd\u4eba<\/a><\/span><\/p>

\u5176\u4e2dsl\u4e3a\u6e90\u8bed\u8a00\uff0ctl\u4e3a\u76ee\u6807\u8bed\u8a00\uff0ctk\u53c2\u6570\u662f\u7531\u4ee5\u4e0b\u65b9\u6cd5\u751f\u6210\u7684\uff1a$a\u4e3a\u8981\u7ffb\u8bd1\u7684\u6587\u5b57\uff08\u4e2d\u56fd\u4eba\uff09\uff0c\u5176\u5b83\u53c2\u6570\u4fdd\u6301\u9ed8\u8ba4\u5373\u53ef<\/p>

private function TL($a)\n{\n    $tkk = $this->TKK();\n    $b = $tkk[0];\n\n    for ($d = [], $e = 0, $f = 0; $f < $this->JS_length($a); $f++) {\n        $g = $this->JS_charCodeAt($a, $f);\n        if (128 > $g) {\n            $d[$e++] = $g;\n        } else {\n            if (2048 > $g) {\n                $d[$e++] = $g >> 6 | 192;\n            } else {\n                if (55296 == ($g & 64512) && $f + 1 < $this->JS_length($a) && 56320 == ($this->JS_charCodeAt($a, $f + 1) & 64512)) {\n                    $g = 65536 + (($g & 1023) << 10) + ($this->JS_charCodeAt($a, ++$f) & 1023);\n                    $d[$e++] = $g >> 18 | 240;\n                    $d[$e++] = $g >> 12 & 63 | 128;\n                } else {\n                    $d[$e++] = $g >> 12 | 224;\n                }\n                $d[$e++] = $g >> 6 & 63 | 128;\n            }\n            $d[$e++] = $g & 63 | 128;\n        }\n    }\n    $a = $b;\n    for ($e = 0; $e < count($d); $e++) {\n        $a += $d[$e];\n        $a = $this->RL($a, '+-a^+6');\n    }\n    $a = $this->RL($a, '+-3^+b+-f');\n    $a ^= $tkk[1] ? $tkk[1] + 0 : 0;\n    if (0 > $a) {\n        $a = ($a & 2147483647) + 2147483648;\n    }\n    $a = fmod($a, pow(10, 6));\n\n    return $a.'.'.($a ^ $b);\n}<\/pre>

<\/span><\/p>

<\/span><\/p>

\u5176\u5b83\u4e0e\u4e0a\u8ff0\u7b97\u6cd5\u76f8\u5173\u7684\u4ee3\u7801<\/span><\/p>

<?php\n\nnamespace Stichoza\\GoogleTranslate\\Tokens;\n\n\/**\n * Google Token Generator.\n *\n * Thanks to @helen5106 and @tehmaestro and few other cool guys\n * at https:\/\/github.com\/Stichoza\/google-translate-php\/issues\/32\n *\/\nclass GoogleTokenGenerator implements TokenProviderInterface\n{\n    \/**\n     * Generate and return a token.\n     *\n     * @param string $source Source language\n     * @param string $target Target language\n     * @param string $text Text to translate\n     * @return string Token\n     *\/\n    public function generateToken(string $source, string $target, string $text) : string\n    {\n        return $this->TL($text);\n    }\n\n    \/**\n     * Generate a valid Google Translate request token.\n     *\n     * @param string $a text to translate\n     *\n     * @return string\n     *\/\n    private function TL($a)\n    {\n        $tkk = $this->TKK();\n        $b = $tkk[0];\n\n        for ($d = [], $e = 0, $f = 0; $f < $this->JS_length($a); $f++) {\n            $g = $this->JS_charCodeAt($a, $f);\n            if (128 > $g) {\n                $d[$e++] = $g;\n            } else {\n                if (2048 > $g) {\n                    $d[$e++] = $g >> 6 | 192;\n                } else {\n                    if (55296 == ($g & 64512) && $f + 1 < $this->JS_length($a) && 56320 == ($this->JS_charCodeAt($a, $f + 1) & 64512)) {\n                        $g = 65536 + (($g & 1023) << 10) + ($this->JS_charCodeAt($a, ++$f) & 1023);\n                        $d[$e++] = $g >> 18 | 240;\n                        $d[$e++] = $g >> 12 & 63 | 128;\n                    } else {\n                        $d[$e++] = $g >> 12 | 224;\n                    }\n                    $d[$e++] = $g >> 6 & 63 | 128;\n                }\n                $d[$e++] = $g & 63 | 128;\n            }\n        }\n        $a = $b;\n        for ($e = 0; $e < count($d); $e++) {\n            $a += $d[$e];\n            $a = $this->RL($a, '+-a^+6');\n        }\n        $a = $this->RL($a, '+-3^+b+-f');\n        $a ^= $tkk[1] ? $tkk[1] + 0 : 0;\n        if (0 > $a) {\n            $a = ($a & 2147483647) + 2147483648;\n        }\n        $a = fmod($a, pow(10, 6));\n\n        return $a.'.'.($a ^ $b);\n    }\n\n    \/**\n     * @return array\n     *\/\n    private function TKK()\n    {\n        return ['406398', (561666268 + 1526272306)];\n    }\n\n    \/**\n     * Process token data by applying multiple operations.\n     * (Params are safe, no need for multibyte functions)\n     *\n     * @param int $a\n     * @param string $b\n     *\n     * @return int\n     *\/\n    private function RL($a, $b)\n    {\n        for ($c = 0; $c < strlen($b) - 2; $c += 3) {\n            $d = $b[$c + 2];\n            $d = 'a' <= $d ? ord($d[0]) - 87 : intval($d);\n            $d = '+' == $b[$c + 1] ? $this->unsignedRightShift($a, $d) : $a << $d;\n            $a = '+' == $b[$c] ? ($a + $d & 4294967295) : $a ^ $d;\n        }\n\n        return $a;\n    }\n\n    \/**\n     * Unsigned right shift implementation\n     * https:\/\/msdn.microsoft.com\/en-us\/library\/342xfs5s(v=vs.94).aspx\n     * http:\/\/stackoverflow.com\/a\/43359819\/2953830\n     *\n     * @param $a\n     * @param $b\n     *\n     * @return number\n     *\/\n    private function unsignedRightShift($a, $b)\n    {\n        if ($b >= 32 || $b < -32) {\n            $m = (int)($b \/ 32);\n            $b = $b - ($m * 32);\n        }\n\n        if ($b < 0) {\n            $b = 32 + $b;\n        }\n\n        if ($b == 0) {\n            return (($a >> 1) & 0x7fffffff) * 2 + (($a >> $b) & 1);\n        }\n\n        if ($a < 0) {\n            $a = ($a >> 1);\n            $a &= 2147483647;\n            $a |= 0x40000000;\n            $a = ($a >> ($b - 1));\n        } else { \n            $a = ($a >> $b);\n        }\n\n        return $a;\n    }\n\n    \/**\n     * Get JS charCodeAt equivalent result with UTF-16 encoding\n     *\n     * @param string $str\n     * @param int    $index\n     *\n     * @return number\n     *\/\n    private function JS_charCodeAt($str, $index) {\n        $utf16 = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8');\n        return ord($utf16[$index*2]) + (ord($utf16[$index*2+1]) << 8);\n    }\n\n    \/**\n     * Get JS equivalent string length with UTF-16 encoding\n     *\n     * @param string $str\n     *\n     * @return number\n     *\/\n    private function JS_length($str) {\n        $utf16 = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8');\n        return strlen($utf16)\/2;\n    }\n}<\/pre>

<\/span><\/p>"}