chengkun
2025-09-15 0cc7f61de2b106c9664033fc27d6426d072ea019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
 
namespace app\common\service;
 
use think\facade\Config;
 
 
class YouDaoTrans {
    private $app_key;
    private $sec_key;
    private $trans_url;
    public function __construct($options = array()) {
        $this->app_key = isset($options['app_key']) ? $options['app_key'] : Config::get('youdao.app_key');
        $this->sec_key = isset($options['sec_key']) ? $options['sec_key'] : Config::get('youdao.sec_key');
        $this->trans_url = isset($options['trans_url']) ? $options['trans_url'] : Config::get('youdao.trans_url');
    }
    
    public function do_translate($q, $from, $to) {
        $salt = YouDaoTrans::create_guid();
        $args = array(
            'q'      => $q,
            'appKey' => $this->app_key,
            'salt'   => $salt,
        );
        $args['from'] = $from;
        $args['to'] = $to;
        $args['signType'] = 'v3';
        $curtime = strtotime("now");
        $args['curtime'] = $curtime;
        $signStr = $this->app_key . YouDaoTrans::truncate(implode("", $q)) . $salt . $curtime . $this->sec_key;
        $args['sign'] = hash("sha256", $signStr);
        $args['vocabId'] = '0';
        $ret = YouDaoTrans::call($this->trans_url, $args);
        return $ret;
    }
    
    // 发起网络请求
    public static function call($url, $args = NULL, $method = "post", $testflag = 0, $timeout = 2000, $headers = array()) {
        $ret = FALSE;
        $i = 0;
        while ($ret === FALSE) {
            if ($i > 1)
                break;
            if ($i > 0) {
                sleep(1);
            }
            $ret = YouDaoTrans::callOnce($url, $args, $method, FALSE, $timeout, $headers);
            $i++;
        }
        return $ret;
    }
    
    public static function callOnce($url, $args = NULL, $method = "post", $withCookie = FALSE, $timeout = 2000, $headers = array()) {
        $ch = curl_init();
        if ($method == "post") {
            $data = YouDaoTrans::convert($args);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
        } else {
            $data = YouDaoTrans::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;
    }
    
    public static function convert(&$args) {
        $data = '';
        if (is_array($args)) {
            foreach ($args as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $k => $v) {
                        $data .= 'q=' . rawurlencode($v) . '&';
                    }
                } else {
                    $data .= "$key=" . rawurlencode($val) . "&";
                }
            }
            return trim($data, "&");
        }
        return $args;
    }
    
    // uuid generator
    public static function create_guid() {
        $microTime = microtime();
        list($a_dec, $a_sec) = explode(" ", $microTime);
        $dec_hex = dechex($a_dec * 1000000);
        $sec_hex = dechex($a_sec);
        YouDaoTrans::ensure_length($dec_hex, 5);
        YouDaoTrans::ensure_length($sec_hex, 6);
        $guid = "";
        $guid .= $dec_hex;
        $guid .= YouDaoTrans::create_guid_section(3);
        $guid .= '-';
        $guid .= YouDaoTrans::create_guid_section(4);
        $guid .= '-';
        $guid .= YouDaoTrans::create_guid_section(4);
        $guid .= '-';
        $guid .= YouDaoTrans::create_guid_section(4);
        $guid .= '-';
        $guid .= $sec_hex;
        $guid .= YouDaoTrans::create_guid_section(6);
        return $guid;
    }
    
    public static function create_guid_section($characters) {
        $return = "";
        for ($i = 0; $i < $characters; $i++) {
            $return .= dechex(mt_rand(0, 15));
        }
        return $return;
    }
    
    public static function truncate($q) {
        $len = YouDaoTrans::abslength($q);
        return $len <= 20 ? $q : (mb_substr($q, 0, 10) . $len . mb_substr($q, $len - 10, $len));
    }
    
    public static function abslength($str) {
        if (empty($str)) {
            return 0;
        }
        if (function_exists('mb_strlen')) {
            return mb_strlen($str, 'utf-8');
        } else {
            preg_match_all("/./u", $str, $ar);
            return count($ar[0]);
        }
    }
    
    public static function ensure_length(&$string, $length) {
        $strlen = strlen($string);
        if ($strlen < $length) {
            $string = str_pad($string, $length, "0");
        } else if ($strlen > $length) {
            $string = substr($string, 0, $length);
        }
    }
    
 
}