折雨的天空
一个google翻译的php调用方式
2012-7-24 我好笨

直接看代码

<?php
/**
* translate()
*
* 翻译函数
*
* @param mixed $text 待翻译的内容
* @param string $language 翻译的语言,格式为 当前语言|目标语言
* @return
*/
function translate($text,$language='zh-cn|en')
{
if(empty($text))
{
return false;
}
@set_time_limit(0);
$html = "";
$ch=curl_init("http://google.com/translate_t?langpair=".urlencode($language)."&text=".urlencode($text));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
$html=curl_exec($ch);
if(curl_errno($ch))
{
$html = "";
}
curl_close($ch);
if(!empty($html))
{
$x=explode("</span></span></div></div>",$html);
$x=explode("onmouseout=\"this.style.backgroundColor='#fff'\">",$x[0]);
return $x[1];
}
else
{
return false;
}
}
echo translate('优乐笑话网');
?>

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容