Search for song names and obtain Baidu MP3 Information

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to learn about song name search and obtain Baidu MP3 information,
The comments written by myself are comprehensive,
If you are interested, you can play with your own mp3 download site.
For more technical articles, see
Http://www.dahuzhi.com
Header ("Content-Type: text/html; charset = UTF-8 ");
Set_time_limit (0); // set the PHP timeout value.

$ Mp3 = new GetBaiduMp3 ();
$ Data = $ mp3-> GetMp3 ('old kid ');
Var_dump ($ data );


Class GetBaiduMp3
{
/**
* Search Baidu mp3
* Author: zhanya4548 email: 23585472@qq.com
* @ Param $ key song name
* @ Return array returns the list of all content
'Song _ id' => song Baidu id
'Songg' => song name
'Singer' => artist
'Alipay' => album
'Singerpicsmall' => thumbnail
'Singerpiclarge' => song big picture
'Albumpicsmall' => album thumbnail
'Albumpiclarge' => album big picture
'Lrc '=> Lyrics
'Mp3link' => mp3 address
*/
Public function GetMp3 ($ key)
{
/* Search for songs
Parameters:
Word: "song name", // song name encodeURI
Format: "json", // returned data format, xml | json, default xml
Callback: "Pub. music. searchResult", // fixed value, return the jsonp format
Return Value:
'Png' => string 'old boys' (length = 9)
& Apos; song _ id' = & apos; string & apos; 5830696 & apos; (length = 7)
'Singer' => string 'chopsticks brothers' (length = 12)
'Yum '=> string 'old boys' (length = 9)
'Singerpicsmall' => string 'HTTP: // qukufile2.qianqian.com/data2/pic/86604899/86604899.jpg' (length = 61)
'Singerpiclarge' => string 'HTTP: // qukufile2.qianqian.com/data2/pic/48691336/48691336.jpg' (length = 61)
'Albumpiclarge '=> string 'HTTP: // qukufile2.qianqian.com/data2/pic/38684666/38684666.jpg' (length = 61)
'Albumpicsmall' => string 'HTTP: // qukufile2.qianqian.com/data2/pic/38684670/38684670.jpg' (length = 61)
*/
$ Url = "http://mp3.baidu.com/dev/api? Tn = getinfo & ct = 0 & word = ". $ key." & ie = UTF-8 & format = json ";
$ Data = $ this-> GetCon ($ url );
$ Mp3_info = json_decode ($ data, true );

/* Get the specified song
Parameters:
SongIds: "38233821" // song id, which is obtained from the 1st list
Callback: "callback" // if not empty, jsonp format data is returned.
Return Value:
Array
'Errorcode' => int 22000
'Data' =>
Array
'Xcode '=> string '521d9a8c84a07810953366c56deab3f1' (length = 32)
'Songlist' =>
Array
0 =>
Array
'Queryid' => string '123' (length = 7)
'Songid' => int 5830696
'Songname' => string 'old boys' (length = 9)
'Artidid' => string '123' (length = 4)
'Artidname' => string 'chopsticks brothers' (length = 12)
'Albumid' => int 5830697
'Albumname' => string 'old boys' (length = 9)
'Songpicsmall' => string 'HTTP: // response (length = 86)
'Songpicbig '=> string 'HTTP: // response (length = 86)
'Songpicradio '=> string 'HTTP: // bytes (length = 86)
'Lrclink' => string '/data2/lrc/14883173/14883173. lrc' (length = 32)
'Version' => string ''(length = 0)
'Copytype' => int 1
& Apos; time = & apos; int 300 & apos;
& Apos; linkCode & apos; = & apos; int 22000
'String' http: // zhangmenshiting.baidu.com/data2/music/10957284/5830696230400? Xcode = 521d9a8c84a078109483b33ab4fa0d0cb094c8a407e2e06 '(length = 126)
'Showlink' => string 'HTTP: // zhangmenshiting.baidu.com/data2/music/10957284/5830696230400? Xcode = 521d9a8c84a078109483b33ab4fa0d0cb094c8a407e2e06 '(length = 126)
'Format' => string 'mp3' (length = 3)
'Rate' => int 128
'SIZE' => int 4801083
'Relatestatus' => string '0' (length = 1)
'Resourcetype '=> string '0' (length = 1)
*/

$ I = 0;
$ Mp3_info3 = array ();
Foreach ($ mp3_info as $ v)
{
$ Mp3_info3 [$ I] ['song _ id'] = $ v ['song _ id']; // song id
$ Mp3_info3 [$ I] ['bob'] = $ v ['bob']; // song name
$ Mp3_info3 [$ I] ['singer'] = $ v ['singer']; // artist
$ Mp3_info3 [$ I] ['alipay'] = $ v ['alipay']; // album
$ Mp3_info3 [$ I] ['singerpicsmall'] = $ v ['singerpicsmall']; // small artist Image
$ Mp3_info3 [$ I] ['singerpiclarge'] = $ v ['singerpiclarge']; // large artist Image
$ Mp3_info3 [$ I] ['albumpicsmall'] = $ v ['albumpicsmall']; // small album image size
$ Mp3_info3 [$ I] ['albumpiclarge'] = $ v ['albumpiclarge ']; // large album image size

$ Url2 = "http://ting.baidu.com/data/music/links? SongIds = ". $ v ['song _ id'];
$ Data2 = $ this-> GetCon ($ url2 );
$ Mp3_info2 = json_decode ($ data2, true );

// Lyrics download http://ting.baidu.com/data2/lrc/15265710/15265710.lrc
$ Mp3_info3 [$ I] ['lrc '] = 'HTTP: // ting.baidu.com /'. $ mp3_info2 ['data'] ['songlist'] [0] ['lrclink'];

// Mp3 http://zhangmenshiting.baidu.com/data2/music/38542270/382338211365422461.mp3? Xcode = 808f67065a7ea25b17e77954bed13215
$ Mp3_info3 [$ I] ['mp3link'] = $ mp3_info2 ['data'] ['songlist'] [0] ['songlink'];

$ I ++;
}
Return $ mp3_info3;
}


/**
* Access the website and obtain its content
* @ Param $ url String url
* @ Param $ postFields Array transmits the content in the Array to the URL in POST mode.
* @ Param $ cookie_file string cookie file
* @ Param $ r_or_w string write cookie or read cookie or both, r read, w write, a both, null no cookie
* @ Return String returns the URL content
*/
Public function GetCon ($ url, $ postFields = null, $ cookie_file = null, $ r_or_w = null)
{
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_USERAGENT, $ _ SERVER ['HTTP _ USER_AGENT ']); // simulate the browser used by the user
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
Curl_setopt ($ ch, CURLOPT_AUTOREFERER, 1); // automatically sets Referer

If ($ cookie_file & ($ r_or_w = 'A' | $ r_or_w = 'W '))
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); // name of the file that stores Cookie Information
If ($ cookie_file & ($ r_or_w = 'A' | $ r_or_w = 'R '))
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file); // read the Cookie information stored above

Curl_setopt ($ ch, CURLOPT_TIMEOUT, 30); // sets the timeout limit to prevent endless loops.
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // display the returned Header content.
Curl_setopt ($ ch, CURLOPT_FAILONERROR, false );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );

If (is_array ($ postFields) & 0 <count ($ postFields ))
{
$ PostBodyString = "";
Foreach ($ postFields as $ k => $ v)
{
$ PostBodyString. = "$ k =". urlencode ($ v )."&";
}
Unset ($ k, $ v );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, 0 );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, 0 );
Curl_setopt ($ ch, CURLOPT_POST, true );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, substr ($ postBodyString, 0,-1 ));
}

$ Reponse = curl_exec ($ ch );

If (curl_errno ($ ch ))
Throw new Exception (curl_error ($ ch), 0 );
Else
$ HttpStatusCode = curl_getinfo ($ ch, CURLINFO_HTTP_CODE );

Curl_close ($ ch );
Return $ reponse;
}
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.