没事写了一个基于有道的翻译程序,有需求的拿走

没事写了一个基于有道的翻译程序,有需要的拿走,主要是想学习file_get_contents的参数

[php]
<?php
echo youdao('done');

function Post($url, $post = null)
{
$context = array();

if (is_array($post)) {
ksort($post);

$content = http_build_query($post, '', '&');
$context['http'] = array (
'protocol_version' =>'1.1',
'timeout' => 10,
'method' => 'POST',
'content' => $content,
'header' => "Accept:application/json, text/javascript, */*; q=0.01\r\n".
"Content-length:".strlen($content)."\r\n".
"Referer:http://fanyi.youdao.com/\r\n".
"X-Requested-With:XMLHttpRequest\r\n".
"Host:fanyi.youdao.com\r\n".
"Origin:http://fanyi.youdao.com\r\n".
"Content-type: application/x-www-form-urlencoded\r\n".
"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36"

);

}

return file_get_contents($url, false, stream_context_create($context));
}

function youdao($data)
{
$data = array('type'=>'EN2ZH_CN', 'doctype'=>'json', 'xmlVersion'=>1.6, 'keyfrom'=> 'fanyi.web', 'ue'=>'UTF-8', 'typoResult'=>'true', 'i'=>$data);
$translate = Post("http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null", $data);
preg_match_all('@"tgt":"([\w\W]*?)"\}@is', $translate, $match);

return join("\n", $match[1]);
}
[/php]

发表评论

评论列表(1)

  • gravel crusher

    2013.8.30 14:08

    要是有谷歌的就好了,翻译的质量也高点,之前博客打不开,我还以为关掉了╮(╯_╰)╭

    博客服务器性能不好,内存耗光了

    回复