1. Construct the music message in XML format provided by WeChat
The XML format of music is like this:
Copy code The code is as follows:
12345678
We write a function to convert the data into In this format, the main data here is the data in MusicUrl and HQMusicUrl. The former is ordinary quality music, and the latter HQ is high quality. HQ will be played first under wifi.
Copy code The code is as follows:
private function transmitMusic($object, $musicArray, $flag = 0)
{
$itemTpl = "
" "Title>
"
";
$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
$textTpl = "
%s
%d
";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $flag);
return $resultStr;
}
2. Construction parameter $musicArray
Copy code
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))));The $songstr and $singerstr variables must be received from followers Text, you first remind followers to enter text according to a certain format, and An Ran intercepts the name of the song artist according to this fixed format. This is not difficult to achieve
I wrote it like this, where $keyword is the content input by the user. The format I require is this: the format is music + song title and singer; for example: the music is about Qi Qin in winter, the song title and Singers are separated by spaces.
Copy code
The code is as follows:
$aa = substr($keyword,strpos($keyword,"Music")+strlen("Music"));//One Chinese character has 2 positions
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);
You can go to w3c to check the specific function usage of string interception. I won’t go into details. The main thing is to obtain the contents of MusicUrl and HQMusicUrl. Baidu music interface http://box.zhangmen.baidu.com/x?op = 12&count=1&title=TITLE $$AUTHOR$$$$
You enter http://box.zhangmen.baidu in the browser. com/x?op=12&count=1&title=About winter $$Qiqin$$$$ observe the content of the XML data provided by the interface, mainly the data in encode and decode, these two data are connected together , the contents of the CDATA inside are taken up to the end of xcode, that is, &mid=0.74155984312224. These contents are not required. I have tried it, but it cannot be played if this part of the song is added. The spliced data is the required MusicUrl and HQMusicUrl.
3. The following is the key point, use PHP to parse XML data
Because I don’t understand PHP syntax, I spent a lot of time
Copy code The code is as follows:
//Music on demand
function read_child($node)
{
global $musicstr ;
$children = $node->childNodes; //Get all child nodes of $node
foreach($children as $e) //Loop to read each child node
{
/*if($e->nodeType == XML_TEXT_NODE) //If the child node is text type, output
-"."
";
}*/
if($e->nodeType == XML_ELEMENT_NODE&&$e->nodeName=='encode') //If the child node is text type Then output
{
$ Musicstr. = $ E-& gt; nodeValue;
}
IF ($ E-& GT; de ') //If the child node is text type, output
) //If the child node is a node object, call the function processing
{
$e) $this->read_child($e); //Note that $this-> is because these methods are written It is in the wechatCallbackapiTest class of WeChat, so you need to add it to call 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; //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 use DOM to do it Yes, Baidu knows and asks others. Of course, there is a better way to get the MusicUrl in the XML data:
Copy the code
The code is as follows:
xml = file_get_contents('http://box.zhangmen.baidu.com/x?op=12&count=1&title=About Winter$$Qiqin$$$$');
$result = simplexml_load_string( $xml);
echo strval($result->url->encode);
echo strval($result->url->decode);
//Output: http:// /zhangmenshiting.baidu.com/data2/music/64380827/Z2ZmbGVuaW9fn6NndK6ap5WXcGVpZpuaZJdrbGtqmm1mY2lolppnmpSXZWZubZyclmlmmZaXa3CTZJeZmW1ncGVll1qin5t1YWBmbGxsamxlaGVrbG9taTE $
64380827.mp3?xcode=140db3e8864c55164ad0bce2297ede73eaa48b2ded70843d&mid=0.38763446277961
Screenshot of the final achievement:
The key content and knowledge points have been explained. I may not be able to explain clearly because of my poor narrative skills. If you have anything you don’t understand, please comment and try your best to answer.
http://www.bkjia.com/PHPjc/743928.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/743928.htmlTechArticle1. Construct the music message in XML format provided by WeChat. The XML format of the music is as follows: Copy the code as follows: xml ToUserName![CDATA[toUser]]/ToUserName FromUserName![CDATA[fromUser]...