First of all, the micro-letter provides an interface that can be sent to the followers as long as the data format satisfies the XML format of the interface it gives
1. Constructs the XML format of the micro-letter to provide the music message the format of the music is this: code 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 that converts data into such a format, The main data is Musicurl and hqmusicurl inside the data, the former normal quality of music, the rear HQ is high quality, WiFi will give priority to play HQ. Code as follows: 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 > &NBSp </xml> "; $RESULTSTR = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time (), $flag); return $RESULTSTR; } 2. Construction parameters $musicarray code is as follows: $musicArray = Array ("Title" => $songstr, Description "=>" singer: ". $singerstr," Musicurl "=> $this->getmusicurl ($this->getmusic ($songstr, $singerstr))," Hqmusicurl "=> $this->gethqmusicurl ($this->getmusic ($songstr, $singerstr))); Where the $SONGSTR and $SINGERSTR variables are derived from the recipient's text, you first remind the followers to enter the text in a certain format and safely intercept the song singer name in this fixed format. This is not difficult to realize I wrote this, the $keyword is the user input content, I requested the format is this: the format for music + song singer; for example: Music is about winter chin, and the song name is separated from the singer by a space. Code as follows: $aa = substr ($keyword, Strpos ($keyword, "Music") +strlen ("Music"));//One Kanji 2 position $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); The function usage of the specific string interception can go to the consortium. I will not detail, mainly Musicurl and Hqmusicurl content acquisition. Baidu Music Interface Http://box.zhangmen.baidu.com/x?op = 12&count=1&title=title $ $AUTHOR $$$$ you enter http://in the browser Box.zhangmen.baidu.com/x?op=12&count=1&title= about in the winter $$ chin $ $ Check the content of the XML data provided by the interface, mainly to encode and decode data, The two numbers are connected, and the contents of the CDATA inside are taken to the back of the Xcode, that is, &mid=0.74155984312224 the content, I tried, plus this part of the song is not put out. Stitching good data is the desired musicurl and Hqmusicurl. 3. The following is the key to parsing XML data with PHP because I don't know PHP syntax, I spent a lot of time code as follows://Music On Demand function Read_child ($node) { Global $musicstr; $children = $node->childnodes; Get $nOde all child Nodes foreach ($children as $e)//loop read per child node { /*if ($e->nodety PE = = xml_text_node//If child node is text type output { Echo $e->node Value. " ---------"." <BR> "; }*/ if ($e->nodetype = = xml_element_node&& $e-> nodename== ' encode ')////If the child node is a text type then output { $musicstr. = $e-&G t;nodevalue; } if ($e->nodetype = = xml_element_node&& $e- >nodename== ' decode ')////If the child node is a text type then output { $MUSICSTR. =$ E->nodevalue. "|"; } if ($e->nodetype = = Xml_element_node)//If the child node is a node object, call function processing &nbs P { $this->read_child ($e);//Note the $this-> here is because these methods are written inIn the Wechatcallbackapitest class of the micro-letters, so you have to add to these functions. } } return $MUSICSTR; } function Getmusic ($song, $singer) { $dom = new DOMDocument ();//Create DOM object $dom->load (' http:/ /box.zhangmen.baidu.com/x?op=12&count=1&title= '. $song. ' $$ '. $singer. ' $$$$'); Read XML file $root = $dom->documentelement; Gets the root return $this->read_child ($root) of the XML data; //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, "&")); The function Gethqmusicurl ($url) { //echo strripos ($url, "&"). " Hqmusicurl "; return substr ($url, Strripos ($url, "http"), Strripos ($url, "&")-strripos ($url, "http")); I do it with DOM, Baidu knows to ask others. Of course, there is a better way to musicurl the XML data: The code is as follows: XML = file_get_contents (' Http://box.zhangmen.baidu.com/x?op=12&count=1 &title= about in the winter $$ chin$$$$'); $result = simplexml_load_string ($xml); Echo strval ($result->url->encode); Echo strval ($result->url->decode); Output: HTTP://ZHANGMENSHITING.BAIDU.COM/DATA2/MUSIC/64380827/Z2ZMBGVUAW9FN6NNDK6AP5WXCGVPZPUAZJDRBGTQM m1my2lolppnmpsxzwzubzyclmlmmzaxa3ctzjezmw1ncgvll1qin5t1ywbmbgxsamxlagvrbg9tate$ 64380827.mp3?xcode= 140db3e8864c55164ad0bce2297ede73eaa48b2ded70843d&mid=0.38763446277961