php调用google在线翻译功能_PHP教程
从中文到英语
class Google_API_translator {
public $out = "";
function translate() {
$this->out = "";
$text = urlencode("计算机");//要翻译的单词
$google_translator_url = "http://translate.google.com/translate_a/t?client=t&text=".$text."&sl=zh_CN&tl=en";
echo $google_translator_url;
$gphtml = $this->postPage(array("url" => $google_translator_url));
$this->out = $gphtml;
return $this->out;
}
function postPage($opts) {
$html = "";
if($opts["url"] != "") {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $opts["url"]);
$html = curl_exec($ch);
if(curl_errno($ch)) $html = "";
curl_close ($ch);
}
return $html;
}
}
$g = new Google_API_translator();
$g->translate();
从英语到中文
class Google_API_translator {
public $out = "";
function translate() {
$this->out = "";
$text = urlencode("computer");//要翻译的单词
$google_translator_url = "http://translate.google.com/translate_a/t?client=t&text=".$text."&sl=en&tl=zh_CN";
//拼凑google翻译的api url
$gphtml = $this->postPage(array("url" => $google_translator_url));
$this->out = $gphtml;
return $this->out;
}
function postPage($opts) {
$html = "";
if($opts["url"] != "") {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $opts["url"]);
$html = curl_exec($ch);
if(curl_errno($ch)) $html = "";
curl_close ($ch);
}
return $html;
}
}
$g = new Google_API_translator();
$g->translate();
很完美,返回的结果中没有任何编码问题。从google返回的中文编码完全正确。
上一篇: php点击验证码实时刷新的实现代码
推荐阅读
-
PHP调用VC编写的COM组件实例_PHP教程
-
typecho插件编写教程(六):调用接口_PHP教程
-
php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法,api卖家_PHP教程
-
PHP error_reporting() 错误控制函数功能详解_PHP教程
-
AMFPHP php远程调用(RPC, Remote Procedure Call)工具 快速入门教程_php实例
-
为IP查询添加GOOGLE地图功能的代码_php技巧
-
php获取Google机器人访问足迹的方法,google机器人_PHP教程
-
ajax + php + Controller 控制所有后台函数调用,ajaxcontroller_PHP教程
-
php递归调用删除数组空值元素的方法_PHP教程
-
PHP调用Twitter的RSS的实现代码_PHP教程