百度翻译接口类封装

1、官方参考文档:

http://api.fanyi.baidu.com/api/trans/product/apidoc

2、代码:

  1 <?php
  2 /**
  3  * @file baidu_transapi.php
  4  * @author whx https://www.cnblogs.com/whx-blogs/
  5  * @date 2018/12/03 18:32:18
  6  * @brief
  7  *
  8  **/
  9 class BaiduTranslate{
 10 
 11     public function translate($text, $from = "zh", $to = "en") {
 12         $apiUrl = 'http://api.fanyi.baidu.com/api/trans/vip/translate?';
 13         $appid = 'YOUR APPID';    //替换为您的 APPID
 14         $appkey = 'YOUR APPKEY';    //替换为您的 APPKEY
 15         $salt = time();
 16 
 17         // 生成签名串 appid+q+salt+密钥 的MD5值
 18         $sign = md5($appid . $text . $salt . $appkey);
 19 
 20         // 构建请求参数
 21         $args = [
 22             "q"      => $text,
 23             "from"   => $from,
 24             "to"     => $to,
 25             "appid"  => $appid,
 26             "salt"   => $salt,
 27             "sign"   => $sign
 28         ];
 29 
 30         // 发送请求 ( POST )
 31         $response = $this->curl($apiUrl, $args);
 32 
 33         // 返回的结果解码
 34         $result = json_decode($response, true);
 35 
 36         /**
 37         获取结果,如果请求成功,$result 结果类似如下:
 38 
 39         array:3 [▼
 40             "from" => "zh"
 41             "to" => "en"
 42             "trans_result" => array:1 [▼
 43                 0 => array:2 [▼
 44                     "src" => "苹果"
 45                     "dst" => "Apple"
 46                 ]
 47             ]
 48         ]
 49 
 50         **/
 51 
 52         // 获取翻译后的结果
 53         if (!isset($result['trans_result'])) {
 54             // 返回错误码及错误信息
 55             return [
 56                 'errorCode'   => $result['error_code'],
 57                 'errorMsg'    => $result['error_msg']
 58             ];
 59         }else {
 60             return $result['trans_result'][0]['dst'];
 61         }
 62     }
 63 
 64     private function curl($url,$post_Date_Array=array())
 65     {
 66         //对空格进行转义
 67         $url = str_replace(' ','+',$url);
 68         //初始化一个新的会话,返回一个cURL句柄
 69         $ch = curl_init();
 70         //设置选项,包括URL
 71         $option = [
 72             CURLOPT_URL              => $url,    //需要获取的URL地址
 73             CURLOPT_RETURNTRANSFER   => 1,        //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
 74             CURLOPT_HEADER           => 0,        //启用时会将头文件的信息作为数据流输出
 75             CURLOPT_TIMEOUT          => 3,        //设置cURL允许执行的最长秒数
 76             CURLOPT_POST             => 1,        //启用时会发送一个常规的POST请求
 77             CURLOPT_POSTFIELDS       => http_build_query($post_Date_Array)    //多维数组http_bulid_query()处理
 78         ];
 79         //为cURL传输会话批量设置选项
 80         curl_setopt_array ($ch,$option);
 81         //执行给定的cURL会话
 82         $output = curl_exec($ch);
 83         //返回最后一次cURL操作的错误号
 84         $errorCode = curl_errno($ch);
 85         //释放curl句柄
 86         curl_close($ch);
 87         if(0 !== $errorCode) {
 88             return false;
 89         }
 90         return $output;
 91 
 92     }
 93 }
 94 
 95 $translate = new BaiduTranslate();
 96 $text = "中华人民共和国";
 97 $from = "zh";
 98 $to = "en";
 99 $result = $translate->translate($text, $from, $to);
100 
101 echo '翻译前的内容:' . $text . '<br>翻译后的内容:'. $result;

3、运行结果:

posted @ 2018-12-03 18:36  Blogs-whx  阅读(587)  评论(0编辑  收藏  举报