This article mainly introduces the example of using Baidu translation api in php. For more information, see The PHP code of Baidu translation API.
The code is as follows:
Function language ($ value, $ from = "auto", $ to = "auto ")
{
$ Value_code = urlencode ($ value );
# Urlencode the text to be translated
$ Appid = "YourApiKey ";
# The API Key you registered
$ Languageurl = "http://openapi.baidu.com/public/2.0/bmt/translate? Client_id = ". $ appid." & q = ". $ value_code." & from = ". $ from." & 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) # obtain 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 ');
?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.