利用Google Translate API製作自己的翻譯指令碼_php執行個體

來源:互聯網
上載者:User

PHP代碼:

複製代碼 代碼如下:

#!/usr/bin/php -q
<?php
/**
 * PHP Script For Google Translate
 * @author:Yishan Wang
 * @version:1.0.0
 */
class Google_API_translator
{
 public $url = "http://translate.google.com/translate_t";
 public $text = "";
 public $out = "";
 public $ip = '';
 function setText($text){
  $this->text = $text;
 }
 function translate($from='auto',$to='zh-CN'){
  $this->out = "";
  $gphtml = $this->postPage($this->url, $this->text,$from,$to);
  preg_match_all('/<span/s+title/="[^>]+>([^<]+)<//span>/i',$gphtml,$res);
  $this->out = $res[1][0];
  return $this->out;
 }
 /*
 $from  需要翻譯的語言
 $to    翻譯的語言
 */
 function postPage($url, $text,$from='auto',$to='zh-CN'){
  $html ='';
  if($url != "" && $text != "") {
   $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   if(!empty($this->ip) && is_string($this->ip)){
    curl_setopt($ch, CURLOPT_INTERFACE,$this->ip);
   }
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_TIMEOUT, 15);
   /*
   *hl - 介面語言,此處無用。
   *langpair - src lang to dest lang
   *ie - urlencode的編碼方式?
   *text - 要翻譯的文本
   */
   $fields = array('hl=zh-CN', 'langpair='.$from.'|'.$to, 'ie=UTF-8','text='.$text);
   $fields = implode('&', $fields);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
   $html = curl_exec($ch);
   if(curl_errno($ch)) $html = "";
   curl_close ($ch);
  }
  return $html;
 }
}
 $from = !empty($_REQUEST['fromlan'])?$_REQUEST['fromlan']:'en';
 $to = !empty($_REQUEST['tolan'])?$_REQUEST['tolan']:'zh-CN';
 $keywords  = "";
 for($i=1;$i<$argc;$i++){
  $keywords .= $argv[$i]." "; 
 }
 $article = !empty($_REQUEST['article'])?$_REQUEST['article']:$keywords;
 $g = new Google_API_translator();
 if(isset($_REQUEST['ip']) && !empty($_REQUEST['ip']))
 {
 $g -> ip = $_REQUEST['ip'];
 }
 $article = iconv('GBK','UTF-8',$article);
 $article = str_replace('{enter}',"/r/n",$article);
 $g->setText($article);
 $g->translate($from,$to);
 echo "-----------翻譯結果--------------/n";
 echo iconv('GBK','UTF-8',$g->out);
 echo "/n";
?>

2、將以上內容儲存名為“gtranslate”的檔案中。

3、給gtranslate添加執行許可權

    chmod a+x gtranslate

4、建立軟串連

    ln -s /yourpath/gtranslate /usr/bin/gtranslate

5、輸入測試詞彙:

    gtranslate Hello World


    -----------翻譯結果--------------
    世界您好

>>>

6、做了個中英文互譯的版本。

用 gtranslate China ,英譯漢

用 gtranslate -r 中國 ,漢譯英

>>>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.