php在线翻译类,基于Google翻译API开发(Google Translate API For PHP)

by 尘缘 on 三月 17th, 2010 // Filed Under → php, Snoopy

Google Translate API For PHP是一个基于Google Translate开发的翻译类,可以帮助PHP程序员实现多国文字的翻译。
本类使用到了Google的AJAX Translate API和Snoopy类,以模拟HTTP请求的方式实现数据的翻译。

以下是更新日志,如果您的接口不是最新版,请下载更新

  1. 2010-03,发布基本功能版
  2. 2010-06,1.0.0,封装成类咯
  3. 2010-10,1.0.1,更换翻译服务器,数据返回处理由JSON改成数组

以下是使用方法:

<?php
//demo 1
$Google = new GoogleTranslate();
$Google->text = '这是一个基于Google在线翻译的工具';
$Google->from = 'zh-CN';
$Google->to = 'en';
$Google->translate();
echo $Google->result;

//demo 2
$Google = new GoogleTranslate('这是一个基于Google在线翻译的工具', 'zh-CN', 'en');
echo $Google->result;

//demo 3
$Google = new GoogleTranslate();
$Google->translate('这是一个基于Google在线翻译的工具', 'zh-CN', 'en');
echo $Google->result;
?>

部份同学不知道这个类是做什么用的,以下给出一份实例运用代码,用户可以保存为query.php跑一跑。

<?php
if($_POST['text'])
{
	include('GoogleTranslate.class.php');

	$Google = new GoogleTranslate();
	$Google->text = $_POST['text'];
	$Google->from = 'zh-CN';
	$Google->to = 'en';
	$Google->translate();
	$content = $Google->result;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>采集进程控制台</title>
</head>
<body>
<form method="post" action="">
	<textarea name="text" rows="5" cols="40"><?php
		echo $content ? $content : '请在这里输入要翻译的内容!';
	?></textarea><br />
	<input type="submit" />
</form>

</body>
</html>

以下是完整程序包:
本地下载:PHP翻译类
站长下载:PHP翻译类
A5下载:PHP翻译类

你可以任意转摘“php在线翻译类,基于Google翻译API开发(Google Translate API For PHP)”,但请保留本文出处和版权信息。
作者:尘缘,QQ:130775,来源:http://www.4wei.cn/archives/1000487

有 12 条评论啦

Posted on 2010年06月22号 at 15:04:34

万众网址导航 :

走过路过,千万不要飞过

Posted on 2010年06月23号 at 21:33:09

匆匆 :

不能用啊 显示的是空白

Posted on 2010年06月30号 at 16:00:18

kui :

....为什么显示是空白

Posted on 2010年07月8号 at 11:59:54

Silence :

听说Google的翻译接口有 字符限制?

Posted on 2010年07月10号 at 11:27:16

game :

还是不行啊。。显示出来是空白!

Posted on 2010年07月12号 at 11:44:55

accessories :

干嘛用Snoopy呢?你用cURL去get就好啦。cURl就是个浏览器

Posted on 2010年08月8号 at 10:55:33

79861548 :

了解一下

Posted on 2010年09月24号 at 13:42:20

不懂啊 :

请问 为什么显示出来空白啊
急啊

Posted on 2010年10月21号 at 04:38:36

Gideon :

蒙人贴

可能是Google的服务器已经封掉了这个HTTP接口,稍后会更新此功能。

Posted on 2010年10月22号 at 16:18:07

白领 :

顶起

Posted on 2010年10月23号 at 14:49:49

第一书聚岛 :

那如果放置在小说网站可以翻译的吗??

可以。

Posted on 2010年12月19号 at 00:52:05

zhangxin :

ǫԚբ?ʤȫҪ??ҫ?Ąڈݣ? 打开是这个乱码那
输入 语句查询也是乱码ǫԚբ?ʤȫҪ??ҫ?Ąڈݣ?
能告诉我为什么吗?

文件请使用UTF8编码。



走过路过,千万不要飞过,那是鸟人: