WeChat public platform development (6) translation function development _ PHP Tutorial

Source: Internet
Author: User
Public platform development (6) development of translation functions. The previous article introduced the development of the weather forecast function of the public platform and realized the first practical application of the public platform. in the next article, we will

The previous article introduced the development of the weather forecast function of the public platform and realized the first practical application of the public platform. in the next article, we will briefly develop the translation function, for your reference.

II. train of thought analysis

Similar to the weather query idea in the previous article, you must first judge the messages sent by the user to determine whether the messages contain the "translation" keyword. if so, extract the content to be translated, then, call the Open Translation API on the network for translation.

There are many translation APIs on the network, so you can choose based on your needs. Here we choose the Youdao translation API and Baidu translation API, which are widely used and have good translation functions. next we will analyze the information about these two APIs.

3.1 Youdao translation API

3.1.1 API address: http://fanyi.youdao.com/openapi

Note:The API interface provided by Youdao is incorrectly returned in the following json data format during the test. go to the internet to check the information. the correct translation address is http://fanyi.youdao.com/fanyiapi.

3.1.2 key application

Fill in relevant information as required, which will be used below, so please fill in carefully and truthfully.

After the application is completed, the API key and keyfrom will be generated below, which will be used when using the API.

3.1.3 API usage example

3.1.4 data format

A. xml format

Http://fanyi.youdao.com/openapi.do? Keyfrom = orchid & key = 1008797533 & type = data & doctype = & version = 1.1 & q = Here is an API

    0                        

Http://fanyi.youdao.com/openapi.do? Keyfrom = orchid & key = 1008797533 & type = data & doctype = & version = 1.1 & q = translation

 

3.2 Baidu translation API

3.2.1 API address: http://openapi.baidu.com/public/2.0/bmt/translate

3.2.2 obtain the api key

The developer registers the authorized API key on the Baidu connection platform. for details, see: http://developer.baidu.com/wiki/index.php? Title = % E5 % B8 % AE % E5 % 8A % A9 % E6 % 96% E6 % A1 % A3 % E9 % A6 % 87% E9 % A1 % B5/% E7 % BD % 91% E7 % AB % 99% E6 % 8E % A5 % E5 % 85% A5/% E5 % 85% A5 % E9 % 97% A8 % E6 % 8C % 87% E5 % 8D % 97

3.2.3 API usage example

The data format of Baidu translation API response is the UTF-8 encoded PHP array corresponding to the standard JSON string.

 

Trans_result is an array, where every {} is a paragraph. The structure is as follows:

 

The paragraph result is one of the trans_result arrays:

 

Description of paragraph results:

Form after json_decode:

 

The message to be translated is in the format of "translation + content to be translated". Therefore, the first two words are intercepted to determine whether the message is a "translation" keyword.

Use the php function mb_substr () to intercept the function. the usage of this function has been discussed in the previous article.

Start from the beginning of the message, intercept two characters, and then judge whether it is a "translation" keyword.

Determine whether to enter only the word "translation". if the input is blank, the entered message is incorrect.

Next, extract the content to be translated:

The content to be translated is captured starting with 3rd characters and then 202 characters.

Then, call the function for translation.

 = ->youdaoDic( = ->baiduDic();

5.1 Youdao translation API

Data interface:

Http://fanyi.youdao.com/openapi.do? Keyfrom = data = 1.1 = text to be translated

5.1.1 xml format

The key code is as follows:

YoudaoDic (= "orchid"; = "YourApiKey"; = 'http: // fanyi.youdao.com/fanyiapi.do? Keyfrom = '.. '& key = '.. '& type = data & doctype = xml & version = 1.1 & q = '. = (=-> translation-> (= 0 "no valid translation allowed "}

Note:

ErrorCode; // get the error code

Translation-> paragraph; // get the translation content

5.1.2 json format

The key code is as follows:

YoudaoDic (= "orchid"; = "YourApiKey"; = 'http: // fanyi.youdao.com/fanyiapi.do? Keyfrom = '.. '& key = '.. '& type = data & doctype = json & version = 1.1 & q = '. = (= json_decode (, = ['errorcode' = ''(0: = ['translation'] ['0' 20: = 'the text to be translated is too long. '30: =' no valid translation is allowed. '40: = 'unsupported language type '50: = 'invalid key ': = 'exception occurred'

Note:

5.2 Baidu translation API

API provides UTF-8 encoded PHP array corresponding to the standard JSON string, and provides medium-> English, medium-> Japanese, English-> in, japanese-> Four types of translation, one more than Youdao translation.

The key code is as follows:

          baiduDic(,="auto",="auto"        =(        =""         = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".."&q=".."&from=".."&to=".=json_decode(->language_text( = -> [0]->      language_text((!('file_get_contents' = (             = = 5            curl_setopt (, CURLOPT_URL,             curl_setopt (, CURLOPT_RETURNTRANSFER, 1            curl_setopt (, CURLOPT_CONNECTTIMEOUT,              = curl_exec(            curl_close( 

Note:

BaiduDic () function:

Language_text ($ baidu_url); // call the language_text () function to obtain the content printed by the target URL, and encode the string in JSON format.

Trans_result; // returns an array of translation results.

Dst; // Obtain the dst result of the first array.

Language_text () function:

Xml format:

Json format:

Baidu translation:

Note:This translation function works properly on SAEs, but it fails to run on BAE. you may want to test it on your own.

Go to QQ group 213260412 sharing to download and use it.

Please follow Zhuo Jin SuzhouPublic account, Zhuo Jin SuzhouDeveloped and tested based on the SAE platform for mainstream features.

You can follow Zhuo Jin SuzhouThe public account performs function tests and obtains new application development.

1. log on to the client, and choose "friends"> "add a friend"> "search for a number">Zhuojinsz.

2. scan the QR code:

Zhuo Jin SuzhouFunction list.

    We Believe, Great People Share Knowledge...

    ...

    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.