PHP implementation of WeChat public platform music on Demand _php tutorial

Source: Internet
Author: User
Tags cdata
1. Construct a music message in the provided XML format
The XML format for music is this:
Copy CodeThe 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 is Musicurl and hqmusicurl inside the data, the former general quality of music, the rear HQ is high quality, WiFi will be the priority to play HQ.
Copy CodeThe 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. Construction parameter $musicarray
Copy CodeThe code is as follows: $musicArray = Array ("Title" and "$songstr", "Description" and "singer:". $singerstr, "musicurl" = $this Getmusicurl ($this->getmusic ($songstr, $singerstr)), "hqmusicurl" = $this->gethqmusicurl ($this Getmusic ($songstr, $singerstr)));
Where the $SONGSTR and $SINGERSTR variables are to be received from the text of the followers, you first remind the followers to enter text in a certain format, Enron in this fixed format to intercept the song singer name. It's not hard to achieve
I wrote that, the $keyword is the user input content, I request the format is this: the format for music + song name Singers; for example: Music in the Winter Qi Qin, song names and singers in the middle of a space-delimited.
Copy CodeThe code is as follows:
$AA = substr ($keyword, Strpos ($keyword, "Music") +strlen ("Music"));//a Kanji 2 positions
$song = substr ($aa, 0,strpos ($aa, ""));
$singer = substr ($aa, Strpos ($aa, ""));
$songstr = Trim ((string) $song);
$singerstr = Trim ((string) $singer);
$musicArray = Array ("Title" + $songstr, "Description" and "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 website to look at me not to detail, mainly is musicurl and hqmusicurl content acquisition. Baidu Music Interface Http://box.zhangmen.baidu.com/x?op = 12&count=1&title=title $ $AUTHOR $
You enter http://box.zhangmen.baidu.com/x?op=12&count=1&title= in the browser about in the winter $$ Qi Qin to see the contents of the XML data provided by the interface, The main is to encode and decode in the data, the two data is linked together, the contents of the CDATA inside of Xcode to take to the back, that is, &mid=0.74155984312224 these content do not, I tried, plus this part of the song is not put out. Splicing good data is the desired musicurl and Hqmusicurl.
3. The following is the focus, using PHP to parse XML data

I spent a lot of time because I didn't understand PHP syntax.

Copy CodeThe 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)//looping through each child node
{
/*if ($e->nodetype = = xml_text_node)//output if child nodes are text type
{
echo $e->nodevalue. " ---------"."
";
}*/
if ($e->nodetype = = xml_element_node&& $e->nodename== ' encode ')//output if child nodes are text type
{
$musicstr. = $e->nodevalue;
}
if ($e->nodetype = = xml_element_node&& $e->nodename== ' decode ')//output if child nodes are text type
{
$musicstr. = $e->nodevalue. "|";
}
if ($e->nodetype = = Xml_element_node)//If the child node is a node object, the calling function handles
{
$this->read_child ($e); Note that the $this-> here is because these methods are all written in the Wechatcallbackapitest class, so the add-on is called to these functions.
}
}
return $musicstr;
}

function Getmusic ($song, $singer)
{
$dom = new DomDocument (); Creating DOM objects
$dom->load (' http://box.zhangmen.baidu.com/x?op=12&count=1&title= ' $song. ' $$ '. $singer. ' $$$$'); Reading an XML file
$root = $dom->documentelement; Gets the root of the 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 do it in DOM, and Baidu knows what to ask others. Of course, there are better ways to take musicurl in XML data:
copy code The code is as follows: XML = file_get_contents (' http://box.zhangmen.baidu.com/x?op=12&count=1&title= in the winter $$ Qi Qin $ $ ');
$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

Finally achieve the results:


The key content and the knowledge point have already finished, the narrative ability is clumsy, may not speak very clearly, has what does not understand the welcome comment, tries to answer.

http://www.bkjia.com/PHPjc/743928.html www.bkjia.com true http://www.bkjia.com/PHPjc/743928.html techarticle 1. Construct the provided XML format for music messages in the XML format of music: The copy Code code is as follows: XML tousername! [Cdata[touser]]/tousername fromusername! [Cdata[fromuser] ...

  • 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.