// 地址 http://api.fanyi.baidu.com/api/trans/product/desktop?req=detail
public function baidu(){
//数组格式转换,待下划线字段也会被转换,需要自定义术语库
$arr= array (
'id'=> '9',
"real_name" => '质量工程师A',
"mark_name" => '测试质量工程师',
"text" => '这是测试的一段话'
);
//字符串转换直接传入就可以了
$query='小公司业务代码存于一个服务器上,而这个服务器有的时候回宕机,导致业务停顿,造成影响。这个时候 就需要做高可用 两个ngix+两个tomcat+两个mysql实现高可用,避免单点问题。中间使用keepalived监听。下面先从简单的mysql主从搞起。下面按照老方式,what->why->how ,是什么,为什么,怎么做来讲解一波。';
$query= json_encode($query,JSON_UNESCAPED_UNICODE);
$from='zh';//zh字符串为中文,不明确可设置为auto
$to='en';//想要转换的语言,auto
$arr= $this->translate($query, $from, $to);
print_r($arr);
}
//翻译入口
function translate($query,$from,$to)
{
$appid= '';
$sec_key='';
$url= 'http://api.fanyi.baidu.com/api/trans/vip/translate';
$args= array(
'q' => $query,
'appid' => $appid,
'salt' => rand(10000,99999),
'from' => $from,
'to' => $to,
);
$args['sign']= $this->buildSign($query, $appid, $args['salt'], $sec_key);
$ret= $this->call($url, $args);
$ret= json_decode($ret,true);
return $ret;
}
//加密
function buildSign($query,$appID,$salt,$secKey)
{/*{{{*/
$str= $appID . $query . $salt . $secKey;
$ret= md5($str);
return $ret;
}/*}}}*/
//发起网络请求
function call($url,$args=null,$method="post",$testflag = 0,$timeout = 10,$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_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 ($valas $k=> $v) {
$data.= $key. '[' . $k. ']=' . rawurlencode($v). '&';
}
}else {
$data.= "$key=" . rawurlencode($val). "&";
}
}
return trim($data,"&");
}
return $args;
}
中文转英文 上面代码运行如下
Array
(
[from] => zh
[to] => en
[trans_result] => Array
(
[0] => Array
(
[src] => "小公司业务代码存于一个服务器上,而这个服务器有的时候回宕机,导致业务停顿,造成影响。这个时候 就需要做高可用 两个ngix+两个tomcat+两个mysql实现高可用,避免单点问题。中间使用keepalived监听。下面先从简单的mysql主从搞起。下面按照老方式,what->why->how ,是什么,为什么,怎么做来讲解一波。"
[dst] => "The business code of a small company is stored on a server, and this server sometimes goes back down, resulting in business pause and impact. At this time, we need to do two ngix + two Tomcat + two Mysql to achieve high availability and avoid single point problems. In the middle, keep alive monitoring is used. Let's start with a simple MySQL master-slave. Now I will explain a wave according to the old way, what - > Why - > how. "
)
)
)