123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/4/24 0024
- * Time: 11:38
- */
- namespace App\Libs;
- class Translate
- {
- const APP_KEY = '52e2f13b4d5f16ea';
- const SEC_KEY = 'TFXmzOx3EwYhJoS1p1Fma2A4JkqPXjYO';
- const CURL_TIMEOUT = 2000;
- const URL = 'http://openapi.youdao.com/api';
- public function request(string $q)
- {
- $salt = $this->createGuid();
- $args = array(
- 'q' => $q,
- 'appKey' => self::APP_KEY,
- 'salt' => $salt,
- );
- $args['from'] = 'zh-CHS';
- $args['to'] = 'EN';
- $args['signType'] = 'v3';
- $curtime = strtotime("now");
- $args['curtime'] = $curtime;
- $signStr = self::APP_KEY . $this->truncate($q) . $salt . $curtime . self::SEC_KEY;
- $args['sign'] = hash("sha256", $signStr);
- $ret = $this->call(self::URL, $args);
- return $ret;
- }
- function bubbleSort($arr)
- {
- $len = count($arr);
- for ($i = 0; $i < $len - 1; $i++) {
- for ($j = 0; $j < $len - 1 - $i; $j++) {
- if (mb_strlen($arr[$j]['source']) < mb_strlen($arr[$j + 1]['source'])) {
- $tmp = $arr[$j];
- $arr[$j] = $arr[$j + 1];
- $arr[$j + 1] = $tmp;
- }
- }
- }
- return $arr;
- }
- public function htmlRequest(string $q)
- {
- $pattern = "/<tr.*?>(.*?)<\/tr>/is";
- $source_q = $q;
- $q = preg_replace($pattern, '', $q);//去掉表格
- $q = explode('翻译分割符', preg_replace('/(<.*?>)/', '翻译分割符', $q));
- $source_array = array();
- foreach ($q as $value) {
- $source = trim($value);
- if (!$source) continue;
- $source_target['source'] = $source;
- $source_target['source_q'] = $value;
- $source_array[] = $source_target;
- }
- foreach ($source_array as $inx => $val) {
- $data = json_decode($this->request($val['source']), true);
- $source_array[$inx]['target'] = $data['translation'][0] ?? '';
- }
- $this->bubbleSort(array_values($source_array));
- foreach ($source_array as $value) {
- $source_q = str_replace($value['source_q'], $value['target'], $source_q);
- }
- return $source_q;
- }
- protected function call($url, $args = null, $method = "post", $timeout = self::CURL_TIMEOUT, $headers = array())
- {
- $ret = false;
- $i = 0;
- while ($ret === false) {
- if ($i > 1)
- break;
- if ($i > 0) {
- sleep(1);
- }
- $ret = $this->callOnce($url, $args, $method, false, $timeout, $headers);
- $i++;
- }
- return $ret;
- }
- protected function callOnce($url, $args = null, $method = "post", $withCookie = false, $timeout = self:: CURL_TIMEOUT, $headers = array())
- {
- $ch = curl_init();
- if ($method == "post") {
- $data = $this->convert($args);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_POST, 1);
- } else {
- $data = $this->convert($args);
- if ($data) {
- if (stripos($url, "?") > 0) {
- $url .= "&$data";
- } else {
- $url .= "?$data";
- }
- }
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- if (!empty($headers)) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- if ($withCookie) {
- curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
- }
- $r = curl_exec($ch);
- curl_close($ch);
- return $r;
- }
- protected function convert(&$args)
- {
- $data = '';
- if (is_array($args)) {
- foreach ($args as $key => $val) {
- if (is_array($val)) {
- foreach ($val as $k => $v) {
- $data .= $key . '[' . $k . ']=' . rawurlencode($v) . '&';
- }
- } else {
- $data .= "$key=" . rawurlencode($val) . "&";
- }
- }
- return trim($data, "&");
- }
- return $args;
- }
- protected function createGuid()
- {
- $microTime = microtime();
- list($a_dec, $a_sec) = explode(" ", $microTime);
- $dec_hex = dechex($a_dec * 1000000);
- $sec_hex = dechex($a_sec);
- $this->ensureLength($dec_hex, 5);
- $this->ensureLength($sec_hex, 6);
- $guid = "";
- $guid .= $dec_hex;
- $guid .= $this->create_guid_section(3);
- $guid .= '-';
- $guid .= $this->create_guid_section(4);
- $guid .= '-';
- $guid .= $this->create_guid_section(4);
- $guid .= '-';
- $guid .= $this->create_guid_section(4);
- $guid .= '-';
- $guid .= $sec_hex;
- $guid .= $this->create_guid_section(6);
- return $guid;
- }
- protected function create_guid_section($characters)
- {
- $return = "";
- for ($i = 0; $i < $characters; $i++) {
- $return .= dechex(mt_rand(0, 15));
- }
- return $return;
- }
- /**
- * 截断
- * @param $q
- * @return string
- */
- protected function truncate($q)
- {
- $len = mb_strlen($q);
- return $len <= 20 ? $q : (mb_substr($q, 0, 10) . $len . mb_substr($q, $len - 10, $len));
- }
- protected function ensureLength(&$string, $length)
- {
- $strLen = mb_strlen($string);
- if ($strLen < $length) {
- $string = str_pad($string, $length, "0");
- } else if ($strLen > $length) {
- $string = mb_substr($string, 0, $length);
- }
- }
- }
|