Baidu Translate API - PHP Implementation

Original: http://www.isaced.com/post-177.html


I have shared with you the experience of Baidu application engine BAE . I have been struggling with iOS projects for a few days. After thinking about it for a few days, I decided to make a handheld translation application and call the translation API to realize it. As for what translation, it was later designated as Baidu translation. , for the following reasons:

  1. Chinese Baidu is the boss
  2. Baidu's official description of the translation API is very detailed
  3. There are two methods of Get and Post. The maximum character limit of Get is 2k, and the maximum of Post is 5k. It returns standard Json format.

At that time, I thought of another project, a monitoring, sending screenshots through the PC to the designated mailbox, and the iOS App receiving and reading pictures through the mail, to achieve the most basic remote control function. It is too difficult to communicate between PC and iOS, or through Webservice, a web interface to transfer, too troublesome, three-terminal coding, and finally this idea had to be lost in sleep!

I'm going to see how PHP is implemented first. I found it online. The Get method is quite simple to implement. You need to apply for an API Key in the Baidu Developer Center . The following is a piece of PHP code that implements Baidu's translation API from the Internet. The test can be implemented, but English-Chinese translation may require conversion encoding.

 
 
  1. <?php
  2. function language($value,$from="auto",$to="auto")
  3. {
  4. $value_code=urlencode($value);
  5. #First urlencode the text to be translated
  6. $appid="YourApiKey";
  7. #Your registered API Key
  8. $languageurl ="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=". $appid ."&q=".$value_code."&from=".$from."&to=".$to;
  9. #Generate the URL GET address of the translation API
  10. $text=json_decode(language_text($languageurl));
  11. $text = $text->trans_result;
  12. return $text[0]->dst;
  13. }
  14. function language_text ( $url ) #Get the content printed by the target URL
  15. {
  16. if(!function_exists('file_get_contents')){
  17. $file_contents = file_get_contents($url);
  18. }else{
  19. $ch = curl_init();
  20. $timeout =5;
  21. curl_setopt ($ch, CURLOPT_URL, $url);
  22. curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
  23. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  24. $file_contents = curl_exec($ch);
  25. curl_close($ch);
  26. }
  27. return $file_contents;
  28. }
  29. echo language ( 'China' );
  30. ?>

Portal:

Baidu Translator API  |  Baidu Developer Center


Official help:
http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6% 96%E9%A1%B5/%E7%99%BE%E5%BA%A6%E7%BF%BB%E8%AF%91/%E7%BF%BB%E8%AF%91API


==========================How to apply for api key? =======================

1. Open http://developer.baidu.com and log in

2. Click on Quick Create App 


3. Fill in the application name and confirm

4. Get the api key


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325482807&siteId=291194637