How can I help my younger brother see [Baidu PHP translation AP?

Source: Internet
Author: User
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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.