PHP calls Google Translator features
From Chinese to English
classGoogle_api_translator { Public $out= ""; functiontranslate () {$this->out = ""; $text=UrlEncode("Computer");//the words to translate $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 ; } functionPostpage ($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=Newgoogle_api_translator ();$g->translate ();
From English to Chinese
classGoogle_api_translator { Public $out= ""; functiontranslate () {$this->out = ""; $text=UrlEncode("Computer");//the words to translate $google _translator_url= "http://translate.google.com/translate_a/t?client=t&text=".$text." &SL=EN&TL=ZH_CN "; //Patchwork API URLs for Google Translate $gphtml=$this->postpage (Array("url" =$google _translator_url)); $this->out =$gphtml; return $this-Out ; } functionPostpage ($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=Newgoogle_api_translator ();$g->translate ();
is perfect, there are no coding problems in the returned results. The Chinese encoding returned from Google is completely correct.
PHP calls Google Translator features