Service Development-reads Baidu music interface and returns music and Baidu music
Recently, I used a lot of interfaces on the Internet to break my tears ~
Two days ago, I made an interface to return music. Let's take a look at the effect:
You can play music on your phone.
The technology is very simple. First, access the Baidu music interface and input the music name and singer name. Then Baidu will return you an XML format containing the music URL and other information. However, the link address here is written in two nodes. I need to get the first half of the segment from an encode, and then get the second half of the segment from deconde, after getting the URL, you need to put the information in the music information that is returned to the user.
First, let's take a look at the XML format of the music message returned to the user:
Here, both MusicUrl and HQMusicUrl are written as the music address obtained from Baidu. For common public accounts, thumbnails are not required.
In fact, the information returned to the user is relatively poor. Except that the music link address is true, the music name and artist are displayed based on what the user inputs.
When @ Singer, the above XML description is written as @ singer.
The code below is as follows:
Let's take a look at the XML returned by calling the Baidu music interface.
The red part is the song name and singer name, as follows:
Http://box.zhangmen.baidu.com/x? Op = 12 & count = 1 & title = Dongfeng broken $ Jay Chou $
After the call is complete, the returned XML is:
<result><count>1</count><url><encode>http://zhangmenshiting.baidu.com/data2/music/120892918/YmRjbG5ocGlpYKCkaHWvm6CWmHFub5qbaJeWlWponG9lmGVkmWppcGZiaGpomphrYWZqlmhuam9la2uWbG6acWpklJZbo6CcbmJhbGxubm5ha2xka2dpcGcy</encode><decode>120892918.mp3?xcode=99cc7eca52e74f20d42850563da3047b3837498b78c992ab&mid=0.87876099061286</decode><type>8</type><lrcid>29026</lrcid><flag>1</flag></url><durl><encode>http://zhangmenshiting2.baidu.com/data2/music/120892919/YmRjbG5ocGlqYKCkaHWvm6CWmHFub5qbaJeWlWponG9lmGVkmWppcGZiaGpomphrYWZqlm6bb5xhlWiXZ2ybbJJkY5Zbo6CcbmJhbGxubm5ha2xka2dpcGcy</encode><decode>120892919.mp3?xcode=99cc7eca52e74f20d42850563da3047b9e8d0c5c26d4a20b&mid=0.87876099061286</decode><type>8</type><lrcid>29026</lrcid><flag>1</flag></durl><p2p>
After, get the splicing address: http://zhangmenshiting.baidu.com/data2/music/120892918/120892918.mp3? Xcode = 99cc7eca52e74f20d42850563da3047b3837498b78c992ab
Note: Remove & mid = 0.87876099061286
The Code is as follows: (c ):
Private string strSonger = null; // artist private string strSongName = null; // song name // <summary> // obtain music // </summary> /// <param name = "strType"> service type </param>/ // <param name = "strOpenId"> User OpenId </param> // <param name = "strWxAccount"> Public Account </param> /// <param name = "strParams"> input parameters: </param> // <param name = "strXML"> returned XML </param> public void GetMusic (string strType, string strOpenId, string strWxA Ccount, string strParams, out string strXML) {strXML = null; try {ComponentServiceMessge comServiceMessage = ComponentServiceMessgeAdapter. instance. getUrlByComType (strType); if (comServiceMessage = null) // The Service {return;} else // finds the Service {// send it to the user strXML = this. returnWxMusic (strOpenId, strWxAccount, this. getMusicFromBaiDu (comServiceMessage. componentURL, strParams) ;}} catch {strXML = null ;}} /// <Summary> /// obtain the music information from the Baidu interface /// </summary> /// <param name = "strParams"> input parameter </param> /// <returns> </returns> public string GetMusicFromBaiDu (string strUrl, string strParams) {string strMusicXml = null; // split the parameter int posOne = strParams. indexOf ("@"); if (posOne> = 0) // There are singers {strSongName = strParams. substring (0, posOne); // song name strSonger = strParams. substring (posOne + 1, strParams. length-posOne-1 ); // Artist strUrl = strUrl + strSongName + "$" + strSonger + "$"; strMusicXml = base. requestMyWebClient (EnumSubmitMethod. get, "", strUrl);} else // only the song name {strSongName = strParams; strUrl = strUrl + strParams + "$"; strMusicXml = base. requestMyWebClient (EnumSubmitMethod. get, "", strUrl);} return strMusicXml ;} /// <summary> /// get the sent XML /// </summary> /// <param name = "strXml"> import the XML obtained from Baidu </param> /// <Returns> </returns> private string ReturnWxMusic (string strOpenId, string strWxAccount, string strXml) {string strWxMusicXml = null; // returns strWxMusicXml = string to the user. format (@ "<xml> <ToUserName> <! [CDATA [{0}]> </ToUserName> <FromUserName> <! [CDATA [{1}]> </FromUserName> <CreateTime> {2} </CreateTime> <MsgType> <! [CDATA [music]> </MsgType> <Music> <Title> <! [CDATA [{3}]> </Title> <Description> <! [CDATA [{4}]> </Description> <MusicUrl> <! [CDATA [{5}]> </MusicUrl> <HQMusicUrl> <! [CDATA [{6}]> </HQMusicUrl> </Music> </xml> ", strOpenId, strWxAccount, this. dateTimeToStamp (DateTime. now ). toString (), strSongName, strSonger, this. getMusicUrl (strXml), this. getMusicUrl (strXml); return strWxMusicXml ;} /// <summary> /// obtain the URL of the Baidu music link /// </summary> /// <param name = "strXml"> access the XML obtained from Baidu music </param> // <returns> </returns> private string GetMusicUrl (string strXml) {string strMusicUrl = null; // read xml XDocument doc = XDocument. parse (strXml); XElement RootEle = doc. root; List <XElement> XEleColl = RootEle. elements (). toList (); strMusicUrl = HttpUtility. urlDecode (XEleColl. elementAt (1 ). element ("encode "). value); // splicing address: Get the previous segment address + decode Node Address int posFlag = strMusicUrl. lastIndexOf ("/"); strMusicUrl = strMusicUrl. substring (0, posFlag + 1); string strValue = XEleColl. elementAt (1 ). element ("decode "). value; strValue = strValue. substring (0, strValue. indexOf ("&"); strMusicUrl + = strValue; // Add the following link to return strMusicUrl ;} /// <summary> // convert the DateTime format to the Unix timestamp format /// </summary> /// <param name = "time"> </param> /// <returns> </returns> private int DateTimeToStamp (System. dateTime time) {System. dateTime startTime = TimeZone. currentTimeZone. toLocalTime (new System. dateTime (1970, 1, 1); return (int) (time-startTime ). totalSeconds ;}
Then, in the configured server address
The processing user message page in to process music messages, and then the server processes the music messages and returns them to the user.