Home  >  Article  >  Backend Development  >  Make your own translation script using Google Translate API_PHP tutorial

Make your own translation script using Google Translate API_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:581190browse

PHP code:

Copy 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,$to);
preg_match_all('/]+>([^<]+) /i',$gphtml,$res);
$this->out = $res[1][0];
return $this->out;
}
/*
$from Language to be translated
$to Language to be translated
*/
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, useless here.
*langpair - src lang to dest lang
*ie - The encoding method of urlencode?
*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['fromlan' ])?$_REQUEST['fromlan']:'en';
$to = !empty($_REQUEST['tolan'])?$_REQUEST['tolan']:'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,$to);
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 soft connection

ln -s /yourpath/gtranslate /usr/bin/gtranslate

5. Enter test vocabulary:

gtranslate Hello World


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

>>>

6. Made a Chinese-English translation version.

Use gtranslate China to translate English into Chinese

Use gtranslate -r Chinese, Chinese to English

>>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/779569.htmlTechArticlePHP code: Copy the code as follows: #!/usr/bin/php -q ?php /*** PHP Script For Google Translate * @author:Yishan Wang * @version:1.0.0*/ class Google_API_translator { public...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn