zoukankan      html  css  js  c++  java
  • PHP 之谷歌翻译

    一、效果图

     二、示例代码

    class GoogleTranslate
    {
        const GOOGLE_URL = 'https://translate.google.cn/translate_a/';

    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)); $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; $response = trim($this->curl_request($url), '"'); if ($response) { return $response; } return null; } function curl_request($url, $data = null, $method = 'get', $https = true) { $ch = curl_init();//初始化 curl_setopt($ch, CURLOPT_URL, $url);//访问的URL curl_setopt($ch, CURLOPT_HEADER, false);//设置不需要头信息 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只获取页面内容,但不输出 if ($https) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https请求 不验证证书 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//https请求 不验证HOST } if (strtolower($method) == 'post') { curl_setopt($ch, CURLOPT_POST, true);//请求方式为post请求 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//请求数据 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data) )); } $result = curl_exec($ch);//执行请求 curl_close($ch);//关闭curl,释放资源 return $result; } } echo (new GoogleTranslate())->translate('zh','en','挂号直接过来医院就好了')."<br>"; echo (new GoogleTranslate())->translate('zh','en','麻烦留一下地址和联系方式,我们业务经理会电话和你联系')."<br>";
  • 相关阅读:
    [洛谷 U68862] 奶牛滑迷宫 题解
    STL的妙用(二)——洛谷 P2073 送花
    平衡树 x 01-trie √
    最小生成树(大纲,待补全)
    单源最短路算法
    黑科技:如何提高整数域内高斯消元的精度和速度——高斯消元与辗转相除法的结合
    Scratch的入门笔记
    Ubuntu18.04安装Tensorflow
    Ubuntu18.04安装英伟达显卡驱动
    macOS下appstore提示未能完成该操作的解决办法
  • 原文地址:https://www.cnblogs.com/yang-2018/p/14617901.html
Copyright © 2011-2022 走看看