How can I help my younger brother see [Baidu PHP translation AP? /*************************************** ************************************
* 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 );
Define ("URL", "http://api.fanyi.baidu.com/api/trans/vip/translate ");
Define ("APPID", "your app id"); // replace it with YOUR APPID
Define ("Password", "your sec key"); // replace it with YOUR KEY
// Translation entry
Function translate ($ query, $ from, $)
{
$ Args = array (
'Q' => $ query,
'Appid '=> APP_ID,
'Salt' => rand (Random, 99999 ),
'From' => $ from,
'To' => $,
);
$ Args ['sign'] = buildSign ($ query, APP_ID, $ args ['Salt'], SEC_KEY );
$ Ret = call (URL, $ args );
$ Ret = json_decode ($ ret, true );
Return $ ret;
}
// Encryption
Function buildSign ($ query, $ appID, $ salt, $ secKey)
{/*{{{*/
$ Str = $ appID. $ query. $ salt. $ secKey;
$ Ret = md5 ($ str );
Return $ ret;
}/*}}}*/
// Initiate a network request
Function call ($ url, $ args = null, $ method = "post", $ testflag = 0, $ timeout = CURL_TIMEOUT, $ headers = array ())
{/*{{{*/
$ Ret = false;
$ I = 0;
While ($ ret = false)
{
If ($ I> 1)
Break;
If ($ I> 0)
{
Sleep (1 );
}
$ Ret = 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 = convert ($ args );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
}
Else
{
$ 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;
}/*}}}*/
?>
Reply to discussion (solution)
You can call it directly, but first you need to apply for an APP_ID in Baidu
You can call it directly, but first you need to apply for an APP_ID in Baidu
Application passed. But it is not very useful. you can see another HTM version.
You can view the results in the browser console.