您的位置:首页 > 编程语言 > PHP开发

PHP Google翻译功能的 API代码

2009-03-09 19:15 597 查看
GOOGLE的翻译功能大家都使用过吧,你有想过在自己的网站上调用GOOGLE的翻译功能吗?下面我就给大家发一个简单调用GOOGLE翻译功能的代码供大家学习。
<?php header("Content-Type: text/html; charset=utf-8"); class
  
GOOGLE的翻译功能大家都使用过吧,你有想过在自己的网站上调用GOOGLE的翻译功能吗?
下面我就给大家发一个简单调用GOOGLE翻译功能的代码供大家学习。
<?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 = "opts["language_pair'>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;
}
}
?>
<?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 = "opts["language_pair'>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;
?>
$g = new Google_API_translator();
$g->setOpts(array("text" => "Cjjer是天才", "language_pair" => "zh-CN|en"));
$g->translate();
echo $g->out;
?>
这样就可以了,输出结果:Cjjer is genius
这段代码比较粗糙,因为只是简单展示本地PHP如何调用GOOGLE翻译功能的!
如果有什么不明白的地方可以EMAIL给我 yxyzyy@vip.qq.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: