Php use Baidu translation api example sharing _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.