- 论坛徽章:
- 0
|
测试代码如下:- <?php
- class Google_API_translator {
- public $opts = array("text" => "", "language_pair" => "en|zh-CN");
- public $out = "";
- function __construct() {
- echo "Google Translator API\n(c) 2007 Involutive snc [url]http://www.involutive.com[/url]\n";
- echo "Author: Paolo Ardoino < [email]paolo@involutive.com[/email] >";
- }
- 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, "<div id=result_box dir=\"ltr\">"));
- $out = substr($out, 29);
- $out = substr($out, 0, strpos($out, "</div>"));
- $this->out = utf8_encode($out);
- return $this->out;
- }
- // post form data to a given url using curl libs
- 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
- $g = new Google_API_translator();
- $g->setOpts(array("text" => "hello,world", "language_pair" => "en|zh-CN"));
- $g->translate();
- echo $g->out;
- ?>
复制代码
运行后输出:- Google Translator API
- (c) 2007 Involutive snc [url]http://www.involutive.com[/url]
- Author: Paolo Ardoino < [email]paolo@involutive.com[/email] >脛茫潞脙拢卢脢脌陆莽
复制代码
怎样修改编码设置才能输出简体中文?有类似的经历的朋友不吝赐教,多谢!
试过 iconv('UTF-8','ASCII//TRANSLIT',$out),依然输出乱码
[ 本帖最后由 alexru 于 2008-3-19 10:37 编辑 ] |
|