php调用google在线翻译功能
2013-09-05 19:24
239 查看
php调用google在线翻译功能从中文到英语
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返回的中文编码完全正确。
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 调用google在线翻译功能
- PHP调用谷歌翻译实现翻译功能
- 怎样使用PHP调用功能强大的JAVA类库
- php调用c++程序实现php功能扩展
- C# 调用百度翻译API实现翻译功能
- PHP实现翻译功能
- PHP一行代码集成雅虎宝贝鱼在线翻译功能 -- JAVAEYE不支持IE8
- 微信公众号开发教程[020]-wxsdk4php:微信公众号和微信支付开发的所有API功能点的封装及调用实例
- Java 实现在线翻译功能 调用微软Bing API
- 调用Google的API实现简单的翻译功能
- PHP调用API接口实现天气查询功能的示例
- PHP编写登录验证码功能 附调用方法
- Python通过调用有道翻译api实现翻译功能示例
- 微信公众平台开发(PHP)(六) 翻译功能开发
- 使用PHP调用功能强大的JAVA 类库
- C#调用微软在线翻译API实现简单的翻译功能
- Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结
- 利用php调用C语言 扩展PHP的功能
- PHP 调用python 脚本实现python功能
- 利用php调用C语言 扩展PHP的功能