資料來源:http://www.kalvin.cn/study/php-api-call-bing-translation-results.html
<?php echo bingtrans('大家好,欢迎光临我的博客!'); //return: Hello, and welcome to my blog!
function bingtrans($str = '', $in = 'zh-CHS', $to = 'en', $api = 'C2D5477B17DD7DCB1391E6B4D3222F2DB8AFAE7D') {
if (empty($str)) {
return false;
}
$url = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" . $api . "&text=" . urlencode($str) . "&from=" . $in . "&to=" . $to;
if (function_exists('curl_init')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
} else {
$res = @file_get_contents($url);
}
preg_match("~<string([^><]*?)>([\s\S]*?)<\/string>~i", $res, $ostr);
if (empty($ostr[2])) {
return false;
} else {
return htmlspecialchars_decode($ostr[2]);
}
} ?>