PHP enables on-demand music on the public platform. 1. construct an XML format provided for music message music. the XML format is as follows: Copy the code as follows: xmlToUserName! [CDATA [toUser] ToUserNameFromUserName! [CDATA [fromUser]
1. construct music messages in XML format
The XML format of music is as follows:
The code is as follows:
toUser
fromUser
12345678
music
<! [CDATA [TITLE]>
DESCRIPTION
MUSIC_Url
HQ_MUSIC_Url
media_id
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.
The code is as follows:
Private function transmitMusic ($ object, $ musicArray, $ flag = 0)
{
$ ItemTpl ="
<! [CDATA [% s]>
%s
%s
%s
";
$ Item_str = sprintf ($ itemTpl, $ musicArray ['title'], $ musicArray ['description'], $ musicArray ['musicurl'], $ musicArray ['hqmusicurl']);
$ TextTpl ="
%s
%s
% S
music
$ Item_str
% D
";
$ ResultStr = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ flag );
Return $ resultStr;
}
2. construct the parameter $ musicArray
The 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 )));
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.
The code is as follows:
$ Aa = substr ($ keyword, strpos ($ keyword, "music") + strlen ("music"); // A Chinese character in two locations
$ 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. 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 focuses on parsing XML data using PHP:
Because I don't understand PHP syntax, I spent a lot of time
The code is as follows:
// VOD
Function read_child ($ node)
{
Global $ musicstr;
$ Children = $ node-> childNodes; // Obtain all child nodes 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 ."---------"."
";
}*/
If ($ e-> nodeType = XML_ELEMENT_NODE & $ e-> nodeName = 'encoding') // if the child node is text-type, the output is
{
$ Musicstr. = $ e-> nodeValue;
}
If ($ e-> nodeType = XML_ELEMENT_NODE & $ e-> nodeName = 'decode') // if the child node is text-type, the output is
{
$ Musicstr. = $ e-> nodeValue. "| ";
}
If ($ e-> nodeType = XML_ELEMENT_NODE) // if the child node is a node object, call the function for processing.
{
$ This-> read_child ($ e); // Note $ 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 of XML data
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, there is a better way to retrieve the MusicUrl in XML data:
The code is as follows:
Xml = file_get_contents ('http: // box.zhangmen.baidu.com/x? Op = 12 & count = 1 & title = About $ Qizhen in Winter $ ');
$ Result = simplexml_load_string ($ xml );
Echo strval ($ result-> url-> encode );
Echo strval ($ result-> url-> decode );
// Output: Success
64213827133? Xcode = 140db3e8864c55164ad0bce2297ede73eaa48b2ded70843d & mid = 0.38763446277961
Final achievement:
All the key content and knowledge points have been completed, and the narrative skills may not be clear. if you have any questions, please comment and try your best to answer them.
The XML format of ghost music is as follows: Code: xml ToUserName! [CDATA [toUser]/ToUserName FromUserName! [CDATA [fromUser]...