Google翻译接口应用

Google翻译API的PHP实现

Google翻译接口应用

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

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

测试结果:

显示结果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google AJAX Language API - Basic Translation</title>
</head>
<body>


<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">

google.load("language", "1");

function initialize() {
google.language.translate("dddd", "zh-CN", "en", function(result) {
if (!result.error) {
var container = document.getElementById("translation");
container.innerHTML = result.translation;
}
});
}
google.setOnLoadCallback(initialize);

</script>

<div id="translation"></div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值