PHP WeChat development Example code

Source: Internet
Author: User
This article detailed PHP Development examples of code first, you need to go to the official API to apply for 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.

 

Public function responseMsg () {// get post data, May be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // receive XML data // 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 ="
   
  %s
    
  %s
    
  
   
% S
    
  %s
    
  %s
    
 "; 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 = "translation ") {// If you want to translate $ fanyi = $ which; $ search = str_replace ($ fanyi, '', $ keyword); // The keyword $ key =" "to translate ""; // key $ keyfrom = "" applied for by 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 the detailed content of the sample code translated by Youdao in PHP Development. For more information, see other related articles on php Chinese network!

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.