PHP Google的translate API代码


Posted in PHP onDecember 10, 2008

新建一个ANSI的PHP文件,然后创建一个类:

header("Content-Type: text/html; charset=utf-8"); 
class Google_API_translator{ 
public $opts = array("text" => "", "language_pair" => "en|it"); 
public $out = ""; 
function setOpts($opts) { 
if($opts["text"] != "") $this->opts["text"] = $opts["text"]; 
if($opts["language_pair"] != "") $this->opts["language_pair"] = $opts["language_pair"]; 
} 
function translate() { 
$this->out = ""; 
$google_translator_url = "http://translate.google.com/translate_t?langpair=".urlencode($this->opts["language_pair"])."&;"; 
$google_translator_data .= "text=".urlencode($this->opts["text"]); 
$gphtml = $this->postPage(array("url" => $google_translator_url, "data" => $google_translator_data)); 
$out = substr($gphtml, strpos($gphtml, " 
")); 
$out = substr($out, 29); 
$out = substr($out, 0, strpos($out, " 
")); 
$this->out = utf8_encode($out); 
return $this->out; 
} 
function postPage($opts) { 
$html =''; 
if($opts["url"] != "" && $opts["data"] != "") { 
$ch = curl_init($opts["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); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $opts["data"]); 
$html = curl_exec($ch); 
if(curl_errno($ch)) $html = ""; 
curl_close ($ch); 
} 
return $html; 
} 
} 
?>

使用的时候
$g = new Google_API_translator(); 
$g->setOpts(array("text" => "Cjjer是天才", "language_pair" => "zh-CN|en")); 
$g->translate(); 
echo $g->out; 
?>

这样就可以了,输出:Cjjer is genius
PHP的就这里,参见了部分同学的部分代码。具体忘了。
这段代码不好。。。但可以用,懒得管啦。。
PHP 相关文章推荐
提升PHP执行速度全攻略
Oct 09 PHP
PHP中将数组转成XML格式的实现代码
Aug 08 PHP
第五章 php数组操作
Dec 30 PHP
如何判断php数组的维度
Jun 10 PHP
关于url地址传参数时字符串有回车造成页面脚本赋值失败的解决方法
Jun 28 PHP
thinkphp跨库操作的简单代码实例
Sep 22 PHP
php获取当前月与上个月月初及月末时间戳的方法
Dec 05 PHP
php版微信自定义回复功能示例
Dec 05 PHP
php封装的验证码类分享
Feb 26 PHP
实例讲解PHP页面静态化
Feb 05 PHP
php实现微信和支付宝支付的示例代码
Aug 11 PHP
PHP sdk实现在线打包代码示例
Dec 09 PHP
php escape URL编码
Dec 10 #PHP
php 破解防盗链图片函数
Dec 09 #PHP
中英文字符串翻转函数
Dec 09 #PHP
php2html php生成静态页函数
Dec 08 #PHP
php 远程图片保存到本地的函数类
Dec 08 #PHP
PHP 出现乱码和Sessions验证问题的解决方法!
Dec 06 #PHP
Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
Dec 06 #PHP
You might like
PHP之数组学习
2011/05/29 PHP
浅析php中常量,变量的作用域和生存周期
2013/08/10 PHP
php+ajax注册实时验证功能
2016/07/20 PHP
laravel dingo API返回自定义错误信息的实例
2019/09/29 PHP
javascript OFFICE控件测试代码
2009/12/08 Javascript
javascript同步服务器时间和同步倒计时小技巧
2015/09/24 Javascript
JS简单实现DIV相对于浏览器固定位置不变的方法
2016/06/17 Javascript
JavaScript中最容易混淆的作用域、提升、闭包知识详解(推荐)
2016/09/05 Javascript
js实现随机抽选效果、随机抽选红色球效果
2017/01/13 Javascript
详解React-Native全球化多语言切换工具库react-native-i18n
2017/11/03 Javascript
Vue批量图片显示时遇到的路径被解析问题
2019/03/28 Javascript
解决使用layui的时候form表单中的select等不能渲染的问题
2019/09/18 Javascript
vue 解决异步数据更新问题
2019/10/29 Javascript
vue video和vue-video-player实现视频铺满教程
2020/10/30 Javascript
Python实现3行代码解简单的一元一次方程
2014/08/18 Python
基于python的七种经典排序算法(推荐)
2016/12/08 Python
Python基于time模块求程序运行时间的方法
2017/09/18 Python
Android基于TCP和URL协议的网络编程示例【附demo源码下载】
2018/01/23 Python
selenium+python 去除启动的黑色cmd窗口方法
2018/05/22 Python
python实现ID3决策树算法
2018/08/29 Python
python/Matplotlib绘制复变函数图像教程
2019/11/21 Python
TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法
2020/04/19 Python
HTML5中如何显示视频呢 HTML5视频播放demo
2013/06/08 HTML / CSS
amazeui页面分析之登录页面的示例代码
2020/08/25 HTML / CSS
香港个人化生活购物网站:Ballyhoo Limited
2016/09/10 全球购物
个人自我评价分享
2013/12/20 职场文书
歌唱比赛主持词
2014/03/18 职场文书
房屋买卖协议书
2014/04/10 职场文书
文明礼仪标语
2014/06/13 职场文书
临时租车协议范本
2014/09/23 职场文书
工作检讨书怎么写
2014/10/10 职场文书
2014小学二年级班主任工作总结
2014/12/05 职场文书
2015年林业工作总结
2015/05/14 职场文书
用python开发一款操作MySQL的小工具
2021/05/12 Python
Python机器学习之基础概述
2021/05/19 Python
Python matplotlib安装以及实现简单曲线的绘制
2022/04/26 Python