网易音乐接口

function curl_get($url)
{
    $refer = "http://music.163.com/";
    $header[] = "Cookie: " . "usertrack=ezq0alUnqxQMompcCoDMAg==";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_REFERER, $refer);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function music_search($word){

        
        $curlPost="csrf_token=09a1ba39d0a58019d4fba3a59dc1acef&s=".urlencode($word)."&limit=8";
   
        $ch=curl_init();
        
        curl_setopt($ch,CURLOPT_URL,'http://music.163.com/api/search/suggest/web');
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53');
        curl_setopt($ch,CURLOPT_REFERER,'http://music.163.com/msg/');
     	curl_setopt($ch,CURLOPT_HOST,'music.163.com');
    //	curl_setopt($ch,CURLOPT_ORIGIN,'http://m.qzone.com');
    
        $data=curl_exec($ch);
        
    //    echo '结果:'.$data;
    //	$data = iconv('gbk', 'utf-8', $data);
    	$data_json = json_decode($data); 
     	$result=$data_json->result;
    	$json_songs=$result->songs;
    	$size=count($json_songs);
    
        for($i=0;$i<$size;$i++){
            $song_id=$json_songs[$i]->id;//歌曲id
            $song_name=$json_songs[$i]->name;//歌名
            //echo '<br/>歌曲ID:'.$song_id;
            $song_artists_json=$json_songs[$i]->artists;
            $song_artists_name=$song_artists_json[0]->name;//歌手
            //歌曲信息
            $song_detail=get_music_info($song_id);
            $song_detail_json = json_decode($song_detail);
            $song_json=$song_detail_json->songs;
           
            $album_json=$song_json[0]->album;
            $song_pic=$album_json->picUrl;
            //       echo 'picUrl:'.$song_pic;
            
            $arr[$i]=array('song_id'=>$song_id,'song_name'=>$song_name,'song_artists_name'=>$song_artists_name,'song_pic'=>$song_pic);
        }

    
        curl_close($ch);
    	return $arr;
    
}

function get_music_info($music_id)
{
    $url = "http://music.163.com/api/song/detail/?id=" . $music_id . "&ids=%5B" . $music_id . "%5D";
    // echo $url;
    return curl_get($url);
}

$arr= music_search("亲密爱人");
echo $arr[0]['song_artists_name'];

你可能感兴趣的:(网易音乐接口)