PHP development
First of all, you need to go to the official website to apply for a key: http://fanyi.youdao.com/openapi? Path = data-mode
After obtaining the key, you can start to obtain the queried data from this API (return json or XML, depending on your preferences, here I use json)
Next I will give the translation implementation code in the responseMsg method directly. If you are not familiar with the API for the first time, click the PHP development text to automatically reply.
</Pre> <a target = _ blank href = "http://blog.csdn.net/misakaqunianxiatian/article/details/49401759" target = "_ blank"> </a> </p> <pre name = "code" class = "php"> public function responseMsg () {// get post data, May be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // receives XML data from the queue // extract post data if (! Empty ($ postStr) {// parse the XML from post as an object $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA ); $ fromUsername = $ postObj-> FromUserName; // The user requesting the message $ toUsername = $ postObj-> ToUserName; // "I" (subscription number) public id $ keyword = trim ($ postObj-> Content); // The message Content sent by the user to the subscription number $ time = time (); // timestamp $ msgtype = 'text'; // Message Type: text $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> </xml> "; if ($ postObj-> MsgType = 'event ') {// if the Message Type in XML is event if ($ postObj-> Event = 'subscribe ') {// if it is a subscription event $ contentStr = "Welcome to subscribe to misaka last summer! \ N more highlights: http://blog.csdn.net/misakaqunianxiatian "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit () ;}$ which = mb_substr ($ keyword, 0, 2, 'utf-8 '); // obtain the information to be returned if ($ which = "") {// if You Want To translate $ fanyi = $ which; $ search = str_replace ($ fanyi, '', $ keyword); // keyword to be translated $ key =" "; // key $ keyfrom =" "applied for in youdao API ""; // corresponds to $ url = 'HTTP: // fanyi. Youdao.com/openapi.do? Keyfrom = '. $ keyfrom. '& key = '. $ key. '& type = data & doctype = json & version = 1.1 & q = '. urlencode ($ search); // call youdao translation API $ json = file_get_contents ($ url); // you can also use curl to get $ res = json_decode ($ json, true ); /*** extract the translation result from the returned data below */$ contentStr = '[query ]'. $ res ['query']. "\ n"; $ contentStr. = "[translation] \ n ". $ res ['translation'] [0]. "\ n"; $ str = ''; foreach ($ res ['basic '] ['explains'] as $ v) {$ str. = $ v. "\ n" ;}$ contentStr. = "[basic definition] \ n ". $ str; if (isset ($ res ['web']) {foreach ($ res ['web'] as $ kk => $ vv) {sort ($ vv); $ res ['web'] [$ kk] = $ vv; // adjust the field order} $ str = ''; foreach ($ res ['web'] as $ v) {foreach ($ v as $ k2 => $ v2) {if ($ k2 = 0) {$ str. = "【". $ v2. "] \ n";} else {foreach ($ v2 as $ v3) {$ str. = $ v3. "\ n" ;}}}$ contentStr. = "[network definition] \ n ". $ str ;}$ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();}
Note: The message returned by the subscription number to be followed should be a line break, and "\ n" should be used ". In your website space, modify your code to send a subscription number, for example, "Translation hamburger". The subscription number will call youdao API to return the translation result, it can be an English or a sentence.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.