Google翻译接口应用

      最近做一翻译公司网站,要求加一在线翻译。之前他们要的是一个外链接,我在想要是有现成的翻译网站接口可以调用的话,这个链接也就没必要了。在网上查了下,google的在线翻译可以用,找到了相关的代码,我自己添加了一些,测试可以用。相关代码如下:

  1. <?php    
  2.     /* Google翻译PHP接口 
  3.    / * 注意:如果翻译文本为UTF-8编码,则要删去mb_convert_encoding函数 
  4.      */   
  5.          
  6.     class Google_API_translator {     
  7.         public$url = "http://translate.google.com/translate_t";    
  8.         public$text = "";//翻译文本    
  9.          public$out = ""; //翻译输出  
  10.          public$lang_src = "";//要翻译的语言 
  11.     public$lang_des = "";//翻译成的语言 
  12.             
  13.         function setText($text){    
  14.             $this->text = $text;    
  15.         }    
  16.          
  17.         function setLang($lang_src,$lang_des){ 
  18.             $this->lang_src = $lang_src
  19.             $this->lang_des = $lang_des
  20.         } 
  21.             
  22.         function translate() {     
  23.             $this->out = "";     
  24.                 
  25.             $gphtml = $this->postPage($this->url, $this->text,$this->lang_src,$this->lang_des);     
  26.             $out = substr($gphtml, strpos($gphtml, "<div id=result_box dir=/"ltr/">"));    
  27.             $out = substr($out, 29);    
  28.             $out = substr($out, 0, strpos($out, "</div>"));    
  29.                 
  30.             $this->out = $out;    
  31.             return$this->out;     
  32.         }     
  33.             
  34.         function postPage($url, $text,$lang_src,$lang_des) {     
  35.             $html ='';     
  36.                 
  37.             if($url != "" && $text != ""&&$lang_src != ""&&$lang_des != "") {     
  38.                 $ch = curl_init($url);     
  39.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     
  40.                 curl_setopt($ch, CURLOPT_HEADER, 1);     
  41.                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);     
  42.                 curl_setopt($ch, CURLOPT_TIMEOUT, 15);     
  43.                     
  44.                 $fields = array('hl=zh-CN', 'langpair='.$lang_src.'|'.$lang_des, 'ie=UTF-8','text='.urlencode(mb_convert_encoding($text, 'UTF-8', 'GB2312')));    
  45.                 curl_setopt($ch, CURLOPT_POST, 1);    
  46.                 curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $fields));                                                         
  47.                     
  48.                 $html = curl_exec($ch);     
  49.                 if(curl_errno($ch)) $html = "";     
  50.                 curl_close ($ch);     
  51.             }     
  52.             return$html;     
  53.         }     
  54.     }     
  55.         
  56.     
  57.    ?> 

<?php /* Google翻译PHP接口 / * 注意:如果翻译文本为UTF-8编码,则要删去mb_convert_encoding函数 */ class Google_API_translator { public $url = "http://translate.google.com/translate_t"; public $text = "";//翻译文本 public $out = ""; //翻译输出 public $lang_src = "";//要翻译的语言 public $lang_des = "";//翻译成的语言 function setText($text){ $this->text = $text; } function setLang($lang_src,$lang_des){ $this->lang_src = $lang_src; $this->lang_des = $lang_des; } function translate() { $this->out = ""; $gphtml = $this->postPage($this->url, $this->text,$this->lang_src,$this->lang_des); $out = substr($gphtml, strpos($gphtml, "<div id=result_box dir=/"ltr/">")); $out = substr($out, 29); $out = substr($out, 0, strpos($out, "</div>")); $this->out = $out; return $this->out; } function postPage($url, $text,$lang_src,$lang_des) { $html =''; if($url != "" && $text != ""&&$lang_src != ""&&$lang_des != "") { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 15); $fields = array('hl=zh-CN', 'langpair='.$lang_src.'|'.$lang_des, 'ie=UTF-8','text='.urlencode(mb_convert_encoding($text, 'UTF-8', 'GB2312'))); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $fields)); $html = curl_exec($ch); if(curl_errno($ch)) $html = ""; curl_close ($ch); } return $html; } } ?>

 

原始代码地址:http://blog.csdn.net/aprin

测试结果:

Google翻译接口应用

显示结果

Google翻译接口应用

 

转自: http://blog.csdn.net/weifangan/article/details/4542237

 

上一篇:Redis (error) NOAUTH Authentication required解决方案


下一篇:错误:‘uuid_t’在此作用域中尚未声明