在线翻译类 使用百度翻译接口 无需申请Api Key<?php <br />
// +----------------------------------------------------------------------<br>
// | PHP MVC FrameWork v1.0 在线翻译类 使用百度翻译接口 无需申请Api Key<br>
// +----------------------------------------------------------------------<br>
// | Copyright (c) 2014-2099 http://qiling.org All rights reserved.<br>
// +----------------------------------------------------------------------<br>
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )<br>
// +----------------------------------------------------------------------<br>
// | Author: qiling 2015年4月13日 下午2:22:15<br>
// +----------------------------------------------------------------------<br>
/**<br>
* 在线翻译类<br>
* @author qiling <br>
*/<br>
class Translate {<br>
/**<br>
* 支持的语种<br>
* @var ArrayAccess<br>
*/<br>
static $Lang = Array (<br>
'auto' => '自动检测',<br>
'ara' => '阿拉伯语',<br>
'de' => '德语',<br>
'ru' => '俄语',<br>
'fra' => '法语',<br>
'kor' => '韩语',<br>
'nl' => '荷兰语',<br>
'pt' => '葡萄牙语',<br>
'jp' => '日语',<br>
'th' => '泰语',<br>
'wyw' => '文言文',<br>
'spa' => '西班牙语',<br>
'el' => '希腊语',<br>
'it' => '意大利语',<br>
'en' => '英语',<br>
'yue' => '粤语',<br>
'zh' => '中文' <br>
);<br>
/**<br>
* 获取支持的语种<br>
* @return array 返回支持的语种<br>
*/<br>
static function getLang() {<br>
return self::$Lang;<br>
}<br>
/**<br>
* 执行文本翻译<br>
* @param string $text 要翻译的文本<br>
* @param string $from 原语言语种 默认:中文<br>
* @param string $to 目标语种 默认:英文<br>
* @return boolean string 翻译失败:false 翻译成功:翻译结果<br>
*/<br>
static function exec($text, $from = 'zh', $to = 'en') {<br>
// http://fanyi.baidu.com/v2transapi?from=zh&query=%E7%94%A8%E8%BD%A6%E8%B5%84%E8%AE%AF&to=fra<br>
<br>
$url = "http://fanyi.baidu.com/v2transapi";<br>
$data = array (<br>
'from' => $from,<br>
'to' => $to,<br>
'query' => $text <br>
);<br>
$data = http_build_query ( $data );<br>
$ch = curl_init ();<br>
curl_setopt ( $ch, CURLOPT_URL, $url );<br>
curl_setopt ( $ch, CURLOPT_REFERER, "http://fanyi.baidu.com" );<br>
curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0' );<br>
curl_setopt ( $ch, CURLOPT_HEADER, 0 );<br>
curl_setopt ( $ch, CURLOPT_POST, 1 );<br>
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );<br>
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );<br>
curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );<br>
$result = curl_exec ( $ch );<br>
curl_close ( $ch );<br>
<br>
$result = json_decode ( $result, true );<br>
<br>
if (!isset($result ['trans_result'] ['data'] ['0'] ['dst'])){<br>
return false; <br>
}<br>
return $result ['trans_result'] ['data'] ['0'] ['dst'];<br>
}<br>
}<br>
echo Translate::exec ( "你好世界!" );
Translate.class.rar ( 1.3 KB 下载:69 次 )
AD:真正免费,域名+虚机+企业邮箱=0元