First of all, you need to go to the Youdao Translation API website to apply for Key:http://fanyi.youdao.com/openapi?path=data-mode
Once you get the key, you can start to get the query data from the API (back to JSON or XML, for personal preference, here I'm using JSON)
Below I direct the Responsemsg method, realizes the translation code to give. If you first contact the API, do not understand what this method is doing, please click on the PHP development of the text automatically reply
Public Function responsemsg () {//get post data, May is due to the different environments $postStr = $GLOBALS ["Http_raw_ Post_data "]; Receives the outgoing XML data//extract the post data if (!empty ($POSTSTR)) {//parses the post to XML as an object $postobj $POSTOBJ = Simplexml_load_strin G ($postStr, ' simplexmlelement ', libxml_nocdata); $fromUsername = $POSTOBJ->fromusername; User $toUsername for Request message = $POSTOBJ->tousername; "I" (subscription number) of the public number id $keyword = Trim ($postObj->content); The message content that the user sent to the subscription number $time = time (); Timestamp $msgtype = ' text '; Message type: Text $TEXTTPL = "
%s
%s
%s
%s
%s
"; if ($postObj->msgtype = = ' event ') {//If the message type in the XML message is event if ($postObj->event = = ' Subscribe ') {//If it is a subscription event $content STR = "Welcome to subscribe Misaka last summer!" \ n More exciting content: Http://blog.csdn.net/misakaqunianxiatian "; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $CONTENTSTR); Echo $resultStr; Exit (); }} $which = Mb_substr ($keyword, 0, 2, ' UTF-8 ');//Get what information to return if ($which = = "Translate") {//If you want to translate $fanyi = $which; $search = Str_replace ($fanyi, ", $keyword); keywords to translate $key = ""; In Youdao API Application key $keyfrom = ""; Corresponds to key $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); /** * The following extracts the translated results from the returned data */$contentStr = ' query '. $res [' query ']. "\ n"; $contentStr. = "" To translate "\ n". $res [' translation '][0]. "\ n"; $str = "; foreach$res [' Basic '] [' explains '] as $v) {$str. = $v. "\ n"; } $contentStr. = "Basic interpretation" \ n ". $STR; if (Isset ($res [' web '])) {foreach ($res [' web '] as $kk = + $vv) {sort ($VV); $res [' web '] [$kk] = $VV; Adjust the order of the fields} $str = '; foreach ($res [' web '] as $v) {foreach ($v as $k 2=> $v 2) {if ($k 2 = = 0) {$str. = "" ". $v 2." "\ n"; }else{foreach ($v 2 as $v 3) {$str. = $v 3. " \ n "; }}} $contentStr. = "Network explanation" \ n ". $STR; } $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $CONTENTSTR); Echo $resultStr; Exit (); }
Note: The information returned by the following subscription number is to be wrapped, using "\ n". In the space of your website, you can change your code to send a subscription number such as "Translate Hamburger", the subscription number will call the Youdao API to return the translation results, can be translated in English, or can be a sentence.
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support this site.
The above describes the PHP Development tools PHP Development of Youdao translation, including the PHP development tools, I hope the PHP tutorial interested in a friend helpful.