/*************************************************** ***********************
* Copyright (c) 2015 Baidu.com, Inc. All Rights Reserved *
***** ************************************************* *******************/
/**
* @file baidu_transapi.php
* @author mouyantao(mouyantao@baidu.com)
* @date 2015/06/23 14:32:18
* @brief
*
**/
define("CURL_TIMEOUT", 10); URL", "http://api.fanyi.baidu.com/api/trans/vip/translate");
define("APPID", "YOUR APP ID"); //APPID に置き換えます
define("Password", "YOUR SEC KEY");//キーに置き換えます
//翻訳エントリ
function translation($query, $from, $to)
{
$args = array(
'q' => $query,
'appid' => APP_ID,
'salt' => rand(10000,99999),
' from' => $from,
'to' => $to,
$args['sign'] = buildSign($query, APP_ID, $args'] , SEC_KEY);
$ret = call($ret, true);
return $ret;
/ /Encryption
関数 buildSign($query, $appID, $salt, $secKey)
{/*{{{*/
$str = $appID . $secKey ; 🎜> $ret = md5($str);
return $ret
}/*}}*/
//ネットワークリクエストを開始します
関数呼び出し($ url, $args=null, $method="post", $testflag = 0, $timeout = CURL_TIMEOUT, $headers=array())
{/*{{{*/
$ret = false; > $ i = 0;
While ($ RET === FALSE)
{
IF ($ i & gt; 1)
Break;
if ($ i & gt; 0) )
{
sleep(1);
}
$ret = callOnce($url, $args, $method, false, $timeout, $headers); 🎜> }
return $ret;
}/*}}}*/
function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = CURL_TIMEOUT, $headers=array())
{/*{{{*/
$ch =curl_init();
if($method == "post")
{
$data = Convert($args);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1); {
$data = 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 ($val as $k=>$v)
{
$data .= $key.'['.$k.']='.rawurlencode($v).'&';
}
}
else
{
$data .="$key=".rawurlencode($val)."&";
}
}
return trim($data, "&");
}
return $args;
}/*}}}*/
?>
直接调用就可以了,但首先你要在百度申请一个APP_ID
直接调用就可以了,但首先你要在百度申请一个APP_ID