PHP code:
#! /Usr/bin/PHP-q <br/> <? Php <br/>/** <br/> * PHP script for Google Translate <br/> * @ Author: Yishan Wang <br/> * @ version: 1.0.0 <br/> */<br/> class google_api_translator <br/> {<br/> Public $ url = "http://translate.google.com/translate_t "; <br/> Public $ text = ""; <br/> Public $ out = ""; <br/> Public $ IP = ''; <br/> function settext ($ text) {<br/> $ this-> text = $ text; <br/>}< br/> function translate ($ from = 'auto', $ to = 'zh-cn') {<br /> $ This-> out = ""; <br/> $ gphtml = $ this-> postpage ($ this-> URL, $ this-> text, $ from, $ to); <br/> preg_match_all ('/<SPAN/S + title/= "[^>] +> ([^ <] +) <// span>/I ', $ gphtml, $ res); <br/> $ this-> out = $ res [1] [0]; <br/> return $ this-> out; <br/>}< br/>/* <br/> $ from language to be translated <br/> $ to language <br/> */<br/> function postpage ($ URL, $ text, $ from = 'auto', $ to = 'zh-cn') {<br/> $ html = ''; <br/> if ($ URL! = "" & $ Text! = "") {<Br/> $ CH = curl_init ($ URL); <br/> curl_setopt ($ ch, curlopt_returntransfer, 1); <br/> If (! Empty ($ this-> ip) & is_string ($ this-> ip) {<br/> curl_setopt ($ ch, curlopt_interface, $ this-> ip ); <br/>}< br/> curl_setopt ($ ch, curlopt_header, 1); <br/> curl_setopt ($ ch, curlopt_followlocation, 1 ); <br/> curl_setopt ($ ch, curlopt_timeout, 15); <br/>/* <br/> * HL-interface language, which is useless here. <Br/> * langpair-Src Lang to DEST Lang <br/> * How to encode ie-urlencode? <Br/> * Text-Text to be translated <br/> */<br/> $ fields = array ('hl = ZH-cn', 'langpair = '. $ from. '| '. $ to, 'ie = UTF-8 ', 'text = '. $ text); <br/> $ fields = implode ('&', $ fields); <br/> curl_setopt ($ ch, curlopt_post, 1 ); <br/> curl_setopt ($ ch, curlopt_postfields, $ fields); <br/> $ html = curl_exec ($ ch); <br/> If (curl_errno ($ ch )) $ html = ""; <br/> curl_close ($ ch); <br/>}< br/> return $ HTML; <br/>}< br/> $ from =! Empty ($ _ request ['fromla'])? $ _ Request ['fromla']: 'en'; <br/> $ to =! Empty ($ _ request ['internet'])? $ _ Request ['internet']: 'zh-cn'; <br/> $ KEYWORDS = ""; <br/> for ($ I = 1; $ I <$ argc; $ I ++) {<br/> $ keywords. = $ argv [$ I]. ""; <br/>}< br/> $ article =! Empty ($ _ request ['Article'])? $ _ Request ['Article']: $ keywords; <br/> $ G = new google_api_translator (); <br/> If (isset ($ _ request ['IP']) &! Empty ($ _ request ['IP']) <br/>{< br/> $ G-> ip =$ _ request ['IP']; <br/>}< br/> $ article = iconv ('gbk', 'utf-8', $ article ); <br/> $ article = str_replace ('{enter}', "/R/N", $ article); <br/> $ G-> settext ($ article ); <br/> $ G-> translate ($ from, $ to); <br/> echo "----------- translation result ------------/N "; <br/> echo iconv ('gbk', 'utf-8', $ G-> out); <br/> echo "/N"; <br/>?>
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
Use gtranslate-R China
, Chinese-English
>>>