Home >php教程 >php手册 >歌名搜索,获取百度MP3信息

歌名搜索,获取百度MP3信息

WBOY
WBOYOriginal
2016-06-07 11:43:541155browse

歌名搜索,获取百度MP3信息,
自己写的注释比较全,
有兴趣的朋友可以玩玩,自己搞个mp3下载站
更多技术文章请移步
http://www.dahuzhi.com
<?php <br /> header("Content-Type:text/html; charset=utf-8");<br> set_time_limit(0);//设置PHP超时时间<br> <br> $mp3  = new GetBaiduMp3();<br> $data = $mp3->GetMp3('老男孩');<br> var_dump($data);<br> <br> <br> class GetBaiduMp3<br> {<br>     /**<br>     * 搜索百度mp3<br>     * 作者: zhanya4548   email: 23585472@qq.com<br>     * @param $key   歌曲名<br>     * @return array 返回所有内容列表<br>           'song_id'         => 歌曲百度id<br>           'song'             => 歌曲名称<br>           'singer'             => 歌手<br>           'album'             => 专辑<br>           'singerPicSmall'     => 歌曲小图<br>           'singerPicLarge'     => 歌曲大图<br>           'albumPicSmall'     => 专辑小图<br>           'albumPicLarge'     => 专辑大图<br>           'lrc'             => 歌词<br>           'mp3Link'         => mp3地址<br>     */<br>     public function GetMp3($key)<br>     {<br>         /* 搜索歌曲<br>         参数:<br>             word: "歌曲名", //歌曲名 encodeURI<br>             format: "json", //返回数据格式,xml | json,默认xml<br>             callback: "Pub.music.searchResult", //固定值,返回jsonp格式    <br>         返回:<br>             'song'              => string '老男孩' (length=9)<br>             'song_id'        => string '5830696' (length=7)<br>             'singer'         => string '筷子兄弟' (length=12)<br>             'album'          => string '老男孩' (length=9)<br>             'singerPicSmall' => string 'http://qukufile2.qianqian.com/data2/pic/86604899/86604899.jpg' (length=61)<br>             'singerPicLarge' => string 'http://qukufile2.qianqian.com/data2/pic/48691336/48691336.jpg' (length=61)<br>             'albumPicLarge'  => string 'http://qukufile2.qianqian.com/data2/pic/38684666/38684666.jpg' (length=61)<br>             'albumPicSmall'  => string 'http://qukufile2.qianqian.com/data2/pic/38684670/38684670.jpg' (length=61) <br>         */<br>         $url = "http://mp3.baidu.com/dev/api/?tn=getinfo&ct=0&word=" . $key . "&ie=utf-8&format=json";<br>         $data = $this->GetCon($url);<br>         $mp3_info = json_decode($data, true);<br>          <br>         /* 获取指定歌曲<br>         参数:<br>             songIds: "38233821" //歌曲id,从第1个列表中得到的歌曲id  <br>             callback: "callback" //不为空时,返回jsonp格式数据    <br>         返回:<br>             array<br>               'errorCode' => int 22000<br>               'data' => <br>                 array<br>                   'xcode' => string '521d9a8c84a07842553396c56deab3f1' (length=32)<br>                   'songList' => <br>                     array<br>                       0 => <br>                         array<br>                           'queryId' => string '5830696' (length=7)<br>                           'songId' => int 5830696<br>                           'songName' => string '老男孩' (length=9)<br>                           'artistId' => string '9295' (length=4)<br>                           'artistName' => string '筷子兄弟' (length=12)<br>                           'albumId' => int 5830697<br>                           'albumName' => string '老男孩' (length=9)<br>                           'songPicSmall' => string 'http://a.hiphotos.baidu.com/ting/pic/item/3b87e950352ac65c434a5feefaf2b21192138adb.jpg' (length=86)<br>                           'songPicBig' => string 'http://c.hiphotos.baidu.com/ting/pic/item/b7003af33a87e9506458caf111385343faf2b4db.jpg' (length=86)<br>                           'songPicRadio' => string 'http://a.hiphotos.baidu.com/ting/pic/item/37d3d539b6003af375f5e5fd342ac65c1138b6db.jpg' (length=86)<br>                           'lrcLink' => string '/data2/lrc/14883173/14883173.lrc' (length=32)<br>                           'version' => string '' (length=0)<br>                           'copyType' => int 1<br>                           'time' => int 300<br>                           'linkCode' => int 22000<br>                           'songLink' => string 'http://zhangmenshiting.baidu.com/data2/music/42657284/5830696230400.mp3?xcode=521d9a8c84a0784260483b33ab4fa0d0cb094c8a407e2e06' (length=126)<br>                           'showLink' => string 'http://zhangmenshiting.baidu.com/data2/music/42657284/5830696230400.mp3?xcode=521d9a8c84a0784260483b33ab4fa0d0cb094c8a407e2e06' (length=126)<br>                           'format' => string 'mp3' (length=3)<br>                           'rate' => int 128<br>                           'size' => int 4801083<br>                           'relateStatus' => string '0' (length=1)<br>                           'resourceType' => string '0' (length=1)<br>         */<br> <br>         $i = 0;<br>         $mp3_info3 = array();<br>         foreach ($mp3_info as $v)<br>         {<br>             $mp3_info3[$i]['song_id'] = $v['song_id'];        //歌曲id<br>             $mp3_info3[$i]['song']    = $v['song'];            //歌名<br>             $mp3_info3[$i]['singer']  = $v['singer'];        //歌手<br>             $mp3_info3[$i]['album']   = $v['album'];        //专辑<br>             $mp3_info3[$i]['singerPicSmall']   = $v['singerPicSmall'];        //歌手图片小<br>             $mp3_info3[$i]['singerPicLarge']   = $v['singerPicLarge'];        //歌手图片大<br>             $mp3_info3[$i]['albumPicSmall']    = $v['albumPicSmall'];        //专辑图片小<br>             $mp3_info3[$i]['albumPicLarge']    = $v['albumPicLarge'];        //专辑图片大<br>             <br>             $url2      = "http://ting.baidu.com/data/music/links?songIds=" . $v['song_id'];<br>             $data2     = $this->GetCon($url2);<br>             $mp3_info2 = json_decode($data2, true);<br> <br>             //歌词下载        http://ting.baidu.com/data2/lrc/15265710/15265710.lrc<br>             $mp3_info3[$i]['lrc']     = 'http://ting.baidu.com/' . $mp3_info2['data']['songList'][0]['lrcLink'];<br> <br>             //mp3下载地址    http://zhangmenshiting.baidu.com/data2/music/38542270/382338211365422461.mp3?xcode=808f67065a7ea25b17e77954bed13215<br>             $mp3_info3[$i]['mp3Link'] = $mp3_info2['data']['songList'][0]['songLink'];<br>             <br>             $i++;<br>         }<br>         return $mp3_info3;<br>     }<br> <br>         <br>     /**<br>     * 访问网址并取得其内容<br>     * @param $url String 网址<br>     * @param $postFields Array 将该数组中的内容用POST方式传递给网址中<br>     * @param $cookie_file string cookie文件<br>     * @param $r_or_w string 写cookie还是读cookie或是两都都有,r读,w写,a两者,null没有cookie<br>     * @return String 返回网址内容<br>     */<br>     public function GetCon($url, $postFields = null, $cookie_file = null, $r_or_w = null)<br>     {<br>         $ch = curl_init();<br>         curl_setopt($ch, CURLOPT_URL, $url);<br>         curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);     // 模拟用户使用的浏览器  <br>         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);                         // 使用自动跳转   <br>         curl_setopt($ch, CURLOPT_AUTOREFERER, 1);                             // 自动设置Referer<br>         <br>         if ($cookie_file && ($r_or_w == 'a' || $r_or_w == 'w')) <br>             curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);                 // 存放Cookie信息的文件名称<br>         if ($cookie_file && ($r_or_w == 'a' || $r_or_w == 'r')) <br>             curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);             // 读取上面所储存的Cookie信息<br>             <br>         curl_setopt($ch, CURLOPT_TIMEOUT, 30);                                 // 设置超时限制防止死循环<br>         curl_setopt($ch, CURLOPT_HEADER, 0);                                 // 显示返回的Header区域内容<br>         curl_setopt($ch, CURLOPT_FAILONERROR, false);<br>         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br>      <br>         if (is_array($postFields) && 0          {<br>             $postBodyString = "";<br>             foreach ($postFields as $k => $v)<br>             {<br>                 $postBodyString .= "$k=" . urlencode($v) . "&";<br>             }<br>             unset($k, $v);<br>             curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);<br>             curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);<br>             curl_setopt($ch, CURLOPT_POST, true);<br>             curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));<br>         }<br>      <br>         $reponse = curl_exec($ch);<br>         <br>         if (curl_errno($ch))<br>             throw new Exception(curl_error($ch),0);<br>         else<br>             $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);<br>             <br>         curl_close($ch);<br>         return $reponse;<br>     }<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn