Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn online translation. You do not need to apply for an Api Key when using the Baidu translation interface.
// + ----------------------------------------------------------------------
// | Php mvc FrameWork v1.0 online translation class using Baidu translation interface no need to apply for Api Key
// + ----------------------------------------------------------------------
// | Copyright (c) 2014-2099 http://qiling.org All rights reserved.
// + ----------------------------------------------------------------------
// | Licensed (http://www.apache.org/licenses/LICENSE-2.0)
// + ----------------------------------------------------------------------
// | Author: qiling <70419470@qq.com> April 13, 2015 2:22:15
// + ----------------------------------------------------------------------
/**
* Online translation
* @ Author qiling <70419470@qq.com>
*/
Class Translate {
/**
* Supported languages
* @ Var ArrayAccess
*/
Static $ Lang = Array (
'Auto' => 'autodetect ',
'Ara' => 'Arabic ',
'De' => 'German ',
'Ru '=> 'Russian ',
'Fra' => 'French ',
'Kor' => 'Korean ',
'Nl '=> 'Dutch ',
'Pt' => 'portuguese ',
'Jp '=> 'Japanese ',
'Th' => 'thai ',
'Wyw' => 'classical Chinese ',
'Pa' => 'Spanish ',
'El' => 'Greek ',
'It' => 'Italian ',
'En' => 'English ',
'Yue' => 'cantonese ',
'Zh '=> 'Chinese'
);
/**
* Obtain Supported languages
* @ Return array return Supported languages
*/
Static function getLang (){
Return self: $ Lang;
}
/**
* Execute Text Translation
* @ Param string $ text the text to be translated
* @ Param string $ from the original language default: Chinese
* @ Param string $ to the default language: English
* @ Return boolean string translation failed: false translation succeeded: translation result
*/
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' => $,
'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/123456 ');
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 );
If (! Isset ($ result ['Trans _ result'] ['data'] ['0'] ['dst ']) {
Return false;
}
Return $ result ['Trans _ result'] ['data'] ['0'] ['dst '];
}
}
Echo Translate: exec ("Hello world! ");
Translate.class.rar (1.3 KB download: 69 times)
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB