Php calls google online translation function _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.