新しい ANSI の PHP 文片、その後作成された種類:
复制代代码如下:
header("Content-Type : 文字セット=utf-8");
class Google_API_translator{
public $opts = array("text" => "", " language_pair" => "en|it");
パブリック $out = "";
function setOpts($opts) {
if($opts["text"] != "") $this->opts["text"] = $opts["text"];
if($opts["言語ペア"] != "") $this->opts["言語ペア"] = $opts["言語ペア"];
}
function translation() {
$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);
$this-> を返す;
}
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);
}
$html を返す;
}
}
?>
复制代码代码如下:
$g = new Google_API_translator( );
$g->setOpts(array("text" => "Cjjer是天才", " language_pair" => "zh-CN|en"));
$g->translate();
echo $g->out;
?>