Use Google TranslateAPI to create your own translation script _ PHP Tutorial

Source: Internet
Author: User
Use Google TranslateAPI to create your own translation scripts. PHP code: Copy the code as follows :#! Usrbinphp-q? Php *** PHPScriptForGoogleTranslate * @ author: YishanWang * @ version: 1.0.0 * classGoogle_API_translator {public PHP code:

The code is as follows:


#! /Usr/bin/php-q
/**
* PHP Script For Google Translate
* @ Author: Yishan Wang
* @ Version: 1.0.0
*/
Class Google_API_translator
{
Public $ url = "http://translate.google.com/translate_t ";
Public $ text = "";
Public $ out = "";
Public $ ip = '';
Function setText ($ text ){
$ This-> text = $ text;
}
Function translate ($ from = 'auto', $ to = 'zh-cn '){
$ This-> out = "";
$ Gphtml = $ this-> postPage ($ this-> url, $ this-> text, $ from, $ );
Preg_match_all ('/] +> ([^ <] +) /I ', $ gphtml, $ res );
$ This-> out = $ res [1] [0];
Return $ this-> out;
}
/*
$ From language to be translated
$ To translation language
*/
Function postPage ($ url, $ text, $ from = 'auto', $ to = 'zh-cn '){
$ Html = '';
If ($ url! = "" & $ Text! = ""){
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
If (! Empty ($ this-> ip) & is_string ($ this-> ip )){
Curl_setopt ($ ch, CURLOPT_INTERFACE, $ this-> ip );
}
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 15 );
/*
* Hl-interface language, which is useless here.
* Langpair-src lang to dest lang
* How is the ie-urlencode encoded?
* Text-the text to be translated
*/
$ Fields = array ('hl = zh-cn', 'langpair = '. $ from. '| '. $ to, 'ie = UTF-8 ', 'text = '. $ text );
$ Fields = implode ('&', $ fields );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );
$ Html = curl_exec ($ ch );
If (curl_errno ($ ch) $ html = "";
Curl_close ($ ch );
}
Return $ html;
}
}
$ From =! Empty ($ _ REQUEST ['fromla'])? $ _ REQUEST ['fromlanc']: 'en ';
$ To =! Empty ($ _ REQUEST ['Internet'])? $ _ REQUEST ['Internet']: 'zh-cn ';
$ Keywords = "";
For ($ I = 1; $ I <$ argc; $ I ++ ){
$ Keywords. = $ argv [$ I]. "";
}
$ Article =! Empty ($ _ REQUEST ['article'])? $ _ REQUEST ['article']: $ keywords;
$ G = new Google_API_translator ();
If (isset ($ _ REQUEST ['IP']) &! Empty ($ _ REQUEST ['IP'])
{
$ G-> ip =$ _ REQUEST ['IP'];
}
$ Article = iconv ('gbk', 'utf-8', $ article );
$ Article = str_replace ('{enter}', "/r/n", $ article );
$ G-> setText ($ article );
$ G-> translate ($ from, $ );
Echo "----------- translation result --------------/n ";
Echo iconv ('gbk', 'utf-8', $ g-> out );
Echo "/n ";
?>

2. Save the above content in a file named "gtranslate.

3. add execution permissions to gtranslate

Chmod a + x gtranslate

4. create a soft connection

Ln-s/yourpath/gtranslate/usr/bin/gtranslate

5. enter the test vocabulary:

Gtranslate Hello World


----------- Translation result --------------
Hello world

>>>

6. a Chinese/English version is provided.

Use gtranslate China, English to Chinese

Gtranslate-r China

>>>

The pipeline code is as follows :#! /Usr/bin/php-q? Php/*** PHP Script For Google Translate * @ author: Yishan Wang * @ version: 1.0.0 */class Google_API_translator {public...

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.