You do not need to apply for an ApiKey when using the Baidu translation interface for online translation.

Source: Internet
Author: User
Tags echo translate
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

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.