Php calls google online translation. Php calls google online translation from Chinese to English classGoogle_API_translator {public $ out; functiontranslate () {$ this-out; $ texturlencode (computer ); words to be translated: php calls google online translation
From Chinese to English
Class Google_API_translator {
Public $ out = "";
Function translate (){
$ This-> out = "";
$ Text = urlencode ("computer"); // The word to be translated.
$ 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 ();
From English to Chinese
Class Google_API_translator {
Public $ out = "";
Function translate (){
$ This-> out = "";
$ Text = urlencode ("computer"); // The word to be translated.
$ Google_translator_url = "http://translate.google.com/translate_a/t? Client = t & text = ". $ text." & sl = en & tl = zh_CN ";
// Splice the google translation 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 ();
Perfect. there is no encoding problem in the returned results. The Chinese code returned from google is completely correct.
Extends from Chinese to English class Google_API_translator {public $ out =; function translate () {$ this-out =; $ text = urlencode (computer ); // the word to be translated...