Php uses Baidu translation api for example sharing. The PHP code of Baidu translation API can be implemented through testing, but encoding may need to be converted. Copy the code as follows :? Phpfunctionlanguage ($ value, $ fromauto, $ toauto) {$ v Baidu translates the PHP code of the API, which can be implemented in testing, but the encoding may need to be converted.
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 ');
?>
Bytes. The code is as follows :? Php function language ($ value, $ from = "auto", $ to = "auto") {$ v...