英文翻译为中文
/**
*Google_API_Translator
*请求谷歌翻译接口
*/
class Google_API_Translator{
public $outHTML; //返回的HTML
/**
*翻译
*/
function translate(){
$text = urlencode('computer'); //要翻译的词
$google_translator_url = "http://translate.google.com/translate_a/t?client=t&text=".$text."&sl=en&tl=zh_CN"; //拼凑google翻译的api url
$html = $this->doPOST($google_translator_url);
$this->outHTML = $html;
return $this->outHTML;
}
/**
*发送请求
*/
function doPost($postUrl){
if($postUrl){
$curl = curl_init(); //开启curl
curl_setopt($curl,CURLOPT_URL,$postUrl); //设置请求路径
$html = curl_exec($curl); //执行
if(curl_errno($curl)) $html = '请求错误';
}else{
//路径不能空
$html = '路径不能空';
}
return $html;
}
}
$gg = new Google_API_Translator();
$gg->translate();