The PHP code of Baidu Translation API can be tested, but the English-Chinese translation may require encoding conversion.
Copy code The code is as follows:
function language($value,$from="auto ",$to="auto")
{
$value_code=urlencode($value);
#First urlencode the text to be translated
$appid="YourApiKey";
#Your registered API Key
$languageurl = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=" . $appid ."&q=" .$value_code. "&from= ".$from."&to=".$to;
#Generate the URL GET address of the translation API
$text=json_decode(language_text($languageurl));
$text = $text-> trans_result;
return $text[0]->dst;
}
function language_text($url) #Get the content printed by the target 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');
?>
http://www.bkjia.com/PHPjc/728090.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/728090.htmlTechArticleThe PHP code of Baidu Translation API can be tested, but the English-Chinese translation may require conversion of encoding. Copy the code The code is as follows: ?php function language($value,$from="auto",$to="auto") { $v...