Home > Backend Development > PHP Tutorial > Baidu Translate interface usage examples

Baidu Translate interface usage examples

WBOY
Release: 2016-07-23 08:54:47
Original
1709 people have browsed it
Baidu translation interface class
  1. /**
  2. * Baidu Developer Center: http://developer.baidu.com/
  3. * Baidu Translation API: http://developer.baidu.com/wiki/index.php?title=docs
  4. */
  5. class baiduAPI{
  6. /**
  7. * $from : Source language: language
  8. * $to : Target language: language code or auto
  9. */
  10. static public function fanyi($value, $from="auto" , $to="auto")
  11. {
  12. $value_code=urlencode($value);
  13. #First perform urlencode processing on the text to be translated
  14. $appid="Ow83extUdl2zLm94s7ldkw5D";
  15. #Your registered API Key
  16. $languageurl = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=" . $appid ."&q=" .$value_code. "&from=".$from."&to=".$to;
  17. #Generate the URL GET address of the translation API
  18. $text=json_decode(self::language_text($languageurl));
  19. $text = isset($text->trans_result) ? $text->trans_result : '';
  20. return isset($text[0]->dst) ? $text[0]->dst : '';
  21. }
  22. #Get the content printed by the target URL
  23. static function language_text($url)
  24. {
  25. if(!function_exists('file_get_contents')) {
  26. $file_contents = file_get_contents($url);
  27. } else {
  28. $ch = curl_init();
  29. $timeout = 5;
  30. curl_setopt ($ch, CURLOPT_URL, $url) ;
  31. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  32. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  33. $file_contents = curl_exec($ch);
  34. curl_close($ch);
  35. }
  36. return $file_contents;
  37. }
  38. }
Copy code
Example 1, (Chinese to English)
  1. # Define the content that needs to be translated
  2. $title = 'Hello';
  3. # Verify whether it is Chinese characters (compatible with gb2312, utf-8 )
  4. if (preg_match("/[x7f-xff]/", $title)) {
  5. $title = baiduAPI::fanyi($title, $from="zh", $to="en");
  6. } else {
  7. $title = baiduAPI::fanyi($title, $from="en", $to="zh");
  8. $title = iconv('utf-8', 'gbk', $title);
  9. }
  10. # Result output Hello
  11. echo $title;
  12. exit;
Copy code
Example 2, (English to Chinese)
  1. # Define the content that needs to be translated
  2. $title = 'Hello';
  3. # Verify whether it is Chinese characters (compatible with gb2312, utf-8)
  4. if (preg_match("/[x7f-xff]/", $title)) {
  5. $title = baiduAPI::fanyi($title, $from="zh ", $to="en");
  6. } else {
  7. $title = baiduAPI::fanyi($title, $from="en", $to="zh");
  8. $title = iconv('utf- 8', 'gbk', $title);
  9. }
  10. # Result output Hello
  11. echo $title;
  12. exit;
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template