On-demand music on WeChat public platform (PHP version)

Source: Internet
Author: User

The first http://blog.csdn.net/tjpu_lin/article/details/21275619 building platform has been studying this subscription number can achieve the function, and then do the weather query, because the weather data acquisition interface is not very good, you can only find simple weather conditions in real time, so you will not write an article to describe it. Let's talk a little bit about it. We started to implement the on-demand music function.

Let's take a look at a great blog and learn more about the specific ideas.

Http://blog.csdn.net/lyq8479/article/details/17232631#comments

If your language uses java, you don't have to go down and read the blog on the link above. It is written in java and I use PHP, therefore, you can only use the ideas in it, and you have to write your own code or something.

After reading this article, I will give a general idea. First, I provide an interface. As long as the data format meets the XML format of the interface it gives, it can be sent to the corresponding music of the publisher.

1. Construct music messages in XML format

The XML format of music is as follows:

<xml>      <ToUserName><![CDATA[toUser]]></ToUserName>      <FromUserName><![CDATA[fromUser]]></FromUserName>      <CreateTime>12345678</CreateTime>      <MsgType><![CDATA[music]]></MsgType>      <Music>          <Title><![CDATA[TITLE]]></Title>          <Description><![CDATA[DESCRIPTION]]></Description>          <MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl>          <HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl>          <ThumbMediaId><![CDATA[media_id]]></ThumbMediaId>      </Music>  </xml> 

We write a function to convert the data into such a format. The main data here is the data in MusicUrl and HQMusicUrl. The former is normal quality music, and the latter is high quality HQ, HQ is preferentially played under wifi.

    private function transmitMusic($object, $musicArray, $flag = 0)    {        $itemTpl = "<Music>    <Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><MusicUrl><![CDATA[%s]]></MusicUrl><HQMusicUrl><![CDATA[%s]]></HQMusicUrl></Music>";        $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);        $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[music]]></MsgType>$item_str<FuncFlag>%d</FuncFlag></xml>";        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $flag);        return $resultStr;    }

2. Construct the parameter $ musicArray

$ MusicArray = array ("Title" => $ songstr, "Description" => "singer :". $ singerstr, "MusicUrl" => $ this-> getMusicUrl ($ this-> getMusic ($ songstr, $ singerstr )), "HQMusicUrl" =>$ this-> getHQMusicUrl ($ this-> getMusic ($ songstr, $ singerstr )));

Among them, the $ songstr and $ singerstr variables have to be taken from the text of the receiver. You should first remind the receiver to enter the text in a certain format, and then extract the song singer name in the fixed format. This is not hard to achieve

This is what I wrote, where $ keyword is the content entered by the user. The format I requested is as follows: Music + song name singer; for example, Qi Qin, music in winter, the song names and singers are separated by spaces.

$ Aa = substr ($ keyword, strpos ($ keyword, "Music") + strlen ("Music ")); // two locations for one Chinese Character $ song = substr ($ aa, 0, strpos ($ aa, ""); $ singer = substr ($ aa, strpos ($ aa, ""); $ songstr = trim (string) $ song); $ singerstr = trim (string) $ singer ); $ musicArray = array ("Title" => $ songstr, "Description" => "singer :". $ singerstr, "MusicUrl" => $ this-> getMusicUrl ($ this-> getMusic ($ songstr, $ singerstr )), "HQMusicUrl" =>$ this-> getHQMusicUrl ($ this-> getMusic ($ songstr, $ singerstr); $ resultStr = $ this-> transmitMusic ($ object, $ musicArray, $ funcFlag );
For the function usage of string truncation, go to w3c for reference and I will not elaborate on it. It is mainly about obtaining MusicUrl and HQMusicUrl content. As mentioned in the blog above,

Baidu music http://box.zhangmen.baidu.com/x? Op = 12 & count = 1 & title = TITLE $ AUTHOR $

Do you enter http://box.zhangmen.baidu.com/x in your browser? Op = 12 & count = 1 & title = about $ $1 $ check the XML data content provided by the interface, mainly for data in encode and decode, the two data are connected together. The content in the CDATA is obtained after xcode, that is, & mid = 0.74155984312224 is not required. I have tried it, this part of songs cannot be added. The spliced data is the required MusicUrl and HQMusicUrl.


3. The following is the focus. I spent a lot of time parsing XML data using PHP because I didn't understand the PHP syntax.

// Function read_child ($ node) {global $ musicstr; $ children = $ node-> childNodes; // obtain all the subnodes of $ node foreach ($ children as $ e) // read each subnode cyclically {/* if ($ e-> nodeType = XML_TEXT_NODE) // If the subnode is text, the output is {echo $ e-> nodeValue. "---------". "<BR>";} */if ($ e-> nodeType = XML_ELEMENT_NODE & $ e-> nodeName = 'encoding ') // If the subnode is text, the output is {$ musicstr. = $ e-> nodeValue;} if ($ e-> nodeType = XML_ELEMENT_NODE & $ e-> nodeName = 'cod E ') // If the subnode is text, the output is {$ musicstr. = $ e-> nodeValue. "|";} if ($ e-> nodeType = XML_ELEMENT_NODE) // if the child node is a Node object, call function processing {$ this-> read_child ($ e ); // note the $ this-> here because these methods are written in the wechatCallbackapiTest class, you must add them to call these functions.} Return $ musicstr;} function getMusic ($ song, $ singer) {$ dom = new DomDocument (); // create a DOM object $ dom-> load ('HTTP: // box.zhangmen.baidu.com/x? Op = 12 & count = 1 & title = '. $ song. '$ '. $ singer. '$'); // read the XML file $ root = $ dom-> documentElement; // get the root return $ this-> read_child ($ root ); // return $ B; // call the read_child function to read the root object} function getMusicUrl ($ url) {// echo strpos ($ url ,"&"). "musciURL"; return substr ($ url, 0, strpos ($ url, "&");} function getHQMusicUrl ($ url) {// echo strripos ($ url, "&"). "HQmusicURL"; return substr ($ url, strripos ($ url, "http"), strripos ($ url, "&")-strripos ($ url, "http "));}
I am using DOM. Baidu knows to ask others. Of course take MusicUrl in XML data there is a better way, someone on CSDN to answer the http://bbs.csdn.net/topics/390734766? Page = 1 # post-396973304

It is mainly used to intercept strings.

Final achievement:


The key content and knowledge points have been fully discussed. I may not be very clear about the narrative skills. If you have any questions, please give me a comment and try your best to answer them.


If you like java, please pay attention to my public platform jLoveInterview. Every day, you may post related knowledge. If you have a platform, you can also ask me to follow each other and jointly supervise and improve the platform.


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.