php百度翻译类


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

$result = json_decode($result, true);
var_dump($result);
// 出错状态码 999
if (isset($result[‘error‘])) {
return false;
}
return $result [‘trans_result‘] [‘data‘] [‘0‘] [‘dst‘];
}
}
echo Translate::exec ( "你好,我是" );
?>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。