命令行版本的中英互译,调用了百度翻译API。
$en how are you
how -> 怎么用
are -> 是
you -> 你
$en "how are you"
how are you -> 好好学习,天天向上
$en 好好学习,天天向上
好好学习,天天向上 -> Study hard and make progress every day
需要php-cli支持:
sudo yum install php-cli
需要赋予执行权限:
chmod a+x en
#!/bin/php <?php $url = 'http://openapi.baidu.com/public/2.0/bmt/translate?client_id=T8Qlsxu4VmLt6qWbqqjtMdLo&q='; isset($argv[1]) or die('至少指定一个词作为参数'.PHP_EOL); $q = $argv; array_shift($q); foreach($q as $k=>$v){ $q[$k]= urlencode($v); } $json = file_get_contents($url.implode('%0A',$q)); $arr=json_decode($json,true); $arr = $arr['trans_result']; foreach ($arr as $key) { $cmd='echo -ne"e[44;37;5m '.$key['dst'].' 33[0m"'; echo"${key['src']} ->".exec($cmd); echo PHP_EOL; } ?>