php 在线翻译函数代码
测试开源使用,使用说明: a href=http://www.tongqiong.com/read.php?tid-99.html rel=nofollowhttp://www.tongqiong.com/read.php?tid-99.html/a
测试开源使用,使用说明:
http://www.tongqiong.com/read.php?tid-99.html
<?php
//作者:http://www.tongqiong.com
//api申请:https://ssl.bing.com/webmaster/developers/appids.aspx?rfp=7
function language($value,$from="zh-CHS",$to="en")
{
$value_code=urlencode($value);
#首先对要翻译的文字进行 urlencode 处理
$appid="2AD6CC0AEFCEFB68D3BAECB511F6B639A17B065A";
#您注册的API Key
$languageurl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" . $appid ."&text=" .$value_code. "&from=". $from ."&to=" . $to;
#生成翻译API的URL GET地址
$text=language_text($languageurl);
#调用函数 获取URL打印的值
preg_match_all("/>(.+)</i",$text,$text_ok,PREG_SET_ORDER);
#有多种方法,获取翻译结果,我这里直接用正则过滤。
$ru=$text_ok[0][1];
#赋予变量
return $ru;
}
function language_text($url) #获取目标URL所打印的内容
{
if(!function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
echo language('中国');//调用函数进行翻译
//作者:http://www.tongqiong.com
?>
- 上一篇:实现单操作符简单公式解析
- 下一篇:php文件上传。
精彩图集
精彩文章






