分享一个无需Api Key的在线翻译类

 

  1. <?php
  2. // +———————————————————————-
  3. // | PHP MVC FrameWork v1.0 在线翻译类 使用百度翻译接口 无需申请Api Key
  4. // +———————————————————————-
  5. // | Copyright (c) 2014-2099 http://qiling.org All rights reserved.
  6. // +———————————————————————-
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +———————————————————————-
  9. // | Author: qiling <[email protected]> 2015年4月13日 下午2:22:15
  10. // +———————————————————————-
  11. /**
  12.  * 在线翻译类
  13.  * @author qiling <[email protected]>
  14.  */
  15. class Translate {
  16.     /**
  17.      * 支持的语种
  18.      * @var ArrayAccess
  19.      */
  20.     static $Lang = Array (
  21.             ‘auto’ => ‘自动检测’,
  22.             ‘ara’ => ‘阿拉伯语’,
  23.             ‘de’ => ‘德语’,
  24.             ‘ru’ => ‘俄语’,
  25.             ‘fra’ => ‘法语’,
  26.             ‘kor’ => ‘韩语’,
  27.             ‘nl’ => ‘荷兰语’,
  28.             ‘pt’ => ‘葡萄牙语’,
  29.             ‘jp’ => ‘日语’,
  30.             ‘th’ => ‘泰语’,
  31.             ‘wyw’ => ‘文言文’,
  32.             ‘spa’ => ‘西班牙语’,
  33.             ‘el’ => ‘希腊语’,
  34.             ‘it’ => ‘意大利语’,
  35.             ‘en’ => ‘英语’,
  36.             ‘yue’ => ‘粤语’,
  37.             ‘zh’ => ‘中文’
  38.     );
  39.     /**
  40.      * 获取支持的语种
  41.      * @return array 返回支持的语种
  42.      */
  43.     static function getLang() {
  44.         return self::$Lang;
  45.     }
  46.     /**
  47.      * 执行文本翻译
  48.      * @param string $text 要翻译的文本
  49.      * @param string $from 原语言语种 默认:中文
  50.      * @param string $to 目标语种 默认:英文
  51.      * @return boolean string 翻译失败:false 翻译成功:翻译结果
  52.      */
  53.     static function exec($text$from = ‘zh’, $to = ‘en’) {
  54.         // http://fanyi.baidu.com/v2transapi?from=zh&query=%E7%94%A8%E8%BD%A6%E8%B5%84%E8%AE%AF&to=fra
  55.         $url = “http://fanyi.baidu.com/v2transapi”;
  56.         $data = array (
  57.                 ‘from’ => $from,
  58.                 ‘to’ => $to,
  59.                 ‘query’ => $text
  60.         );
  61.         $data = http_build_query ( $data );
  62.         $ch = curl_init ();
  63.         curl_setopt ( $ch, CURLOPT_URL, $url );
  64.         curl_setopt ( $ch, CURLOPT_REFERER, “http://fanyi.baidu.com” );
  65.         curl_setopt ( $ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0’ );
  66.         curl_setopt ( $ch, CURLOPT_HEADER, 0 );
  67.         curl_setopt ( $ch, CURLOPT_POST, 1 );
  68.         curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
  69.         curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  70.         curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );
  71.         $result = curl_exec ( $ch );
  72.         curl_close ( $ch );
  73.         $result = json_decode ( $result, true );
  74.         if (!isset($result [‘trans_result’] [‘data’] [‘0’] [‘dst’])){
  75.             return false;
  76.         }
  77.         return $result [‘trans_result’] [‘data’] [‘0’] [‘dst’];
  78.     }
  79. }
  80. // 使用示例:
  81. echo Translate::exec ( “你好,世界!” );

原创文章,作者:陌涛,如若转载,请注明出处:https://imotao.com/159.html

(0)
陌涛的头像陌涛
上一篇 2019年1月24日
下一篇 2019年1月24日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据