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