您的位置:首页 > 编程语言 > PHP开发

php curl请求谷歌翻译

2013-04-26 13:10 155 查看
英文翻译为中文

/**
*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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: