Baidu Translation API PHP code, testing can be achieved, but the English translation may need to convert the code.
Copy Code code as follows:
<?php
function language ($value, $from = "Auto", $to = "Auto")
{
$value _code=urlencode ($value);
#首先对要翻译的文字进行 UrlEncode Processing
$appid = "Yourapikey";
#您注册的API Key
$languageurl = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=". $appid. " &q= ". $value _code. "&from=". $from. " &to= ". $to;
#生成翻译API的URL Get Address
$text =json_decode (Language_text ($languageurl));
$text = $text->trans_result;
return $text [0]->dst;
}
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 (' China ');
?>