PHP uses Baidu translation api example sharing_PHP tutorial

WBOY
Release: 2016-07-13 10:39:51
Original
1268 people have browsed it

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');
?>

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!