有道翻译接口对接
小白 2022-02-10 【原创文章】
拿来就能用
<?php /** * Notes: * User: Administrator * DateTime: 2022/2/10 13:18 * Rose home */ class Youdao { private $APP_KEY = '*****'; private $SEC_KEY = '*****'; private $URL = 'https://openapi.youdao.com/api'; function do_request($q) { $salt = $this->create_guid(); $args = array( 'q' => $q, 'appKey' => $this->APP_KEY, 'salt' => $salt, ); $args['from'] = 'zh-CHS'; $args['to'] = 'en'; $args['signType'] = 'v3'; $curtime = strtotime("now"); $args['curtime'] = $curtime; $signStr = $this->APP_KEY . $this->truncate($q) . $salt . $curtime . $this->SEC_KEY; $args['sign'] = hash("sha256", $signStr); // $args['vocabId'] = '您的用户词表ID'; $ret = $this->call($this->URL, $args); return $ret; } // 发起网络请求 function call($url, $args = null, $method = "post", $testflag = 0, $timeout = '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; } function callOnce($url, $args = null, $method = "post", $withCookie = false, $timeout = '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_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 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; } 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; } function create_guid() { $microTime = microtime(); list($a_dec, $a_sec) = explode(" ", $microTime); $dec_hex = dechex($a_dec * 1000000); $sec_hex = dechex($a_sec); $this->ensure_length($dec_hex, 5); $this->ensure_length($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; } function create_guid_section($characters) { $return = ""; for ($i = 0; $i < $characters; $i++) { $return .= dechex(mt_rand(0, 15)); } return $return; } function truncate($q) { $len = $this->abslength($q); return $len <= 20 ? $q : (mb_substr($q, 0, 10) . $len . mb_substr($q, $len - 10, $len)); } 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]); } } 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); } } }
1
最新发布
- 守护进程-kill端口-输出日志-查看等!
- git 本地分支关联远程分支,合并!
- pytorch安装(含cuda、cudnn安装教程)!
- linux系统下ubuntu22.04安装Docker方法!
- 视频截取封面 =php-ffmpeg/php-ffmpeg!
- 常用实例2023-5-10!
- 富文本 塞入mysql 报错: General error: 1366 Incorrect string value: ‘\xF0\x9F\x98\x84‘ for column ‘content‘!
- 百度地图拖拽获取地址实例!
- php使用smtp鉴权方式发送邮件 插件PHPMailer!
- 有道翻译接口对接!