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

没事写了一个基于有道的翻译程序,有需要的拿走,主要是想学习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]