网易云免费音乐解析

网易云免费音乐解析

KongHen02
2024-03-24 / 0 评论 / 836 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年03月24日,已超过89天没有更新,若内容或图片失效,请留言反馈。

调用的时网易的免费音乐接口,VIP音乐自动剔除。

搜索方式: wy.php?name=关键词
解析方式: wy.php?type=song&id=音乐ID

入口文件 wy.php

<?php

// 允许跨域
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization');

$type = @$_GET['type']??'search';
$page = @$_GET['page']??1;

require "WyJx.php";

// 解析歌曲
if ($type == "song") {
    
    $result = \WyJx::getUrl($_GET['id']);
    
}
// 搜索歌曲
else {
    
    $result = \WyJx::searchSong($_GET['name'], $page);
  
}

exit($result);

?>

类文件 WyJx.php

<?php

/**
 * 网易云音乐解析
 * 作者:KongHen02
 */
class WyJx
{
    
    /**
     * $name  歌曲名称
     * return 返回歌曲搜索结果
     */
    public static function searchSong($name, $page) {
        
        if(empty($name)) {
            
            return self::result(400, "音乐名name不可为空");
            
        }
        
        // 网易搜索接口
        $result = self::httpRequest("http://cloud-music.pl-fe.cn/cloudsearch?keywords=".urlencode($name)."&limit=20&offset=".(($page-1) * 20)."&type=1");
        
        // 处理搜索结果
        $result = json_decode($result, true);
        $songData = $result['result']['songs'];  // 歌曲数据
        
        // 返回数据
        if(empty($songData)) {
            
            return self::result(500, "没有歌曲了");
            
        }
            
        // 处理歌曲信息
        $songList = [];
        $index = 0;
        foreach ($songData as $key=> $value) {
            
            // 判断是否为VIP专享歌曲
            $vip = $value['privilege']['freeTrialPrivilege']['resConsumable'];
            
            if(!$vip) {
                $songList[$index]['type'] = 'wy';
                $songList[$index]['id'] = $value['id'];
                $songList[$index]['name'] = $value['name'];
                $songList[$index]['pic'] = $value['al']['picUrl'];
                $singer = "";
                foreach ($value['ar'] as $values) {
                    if($singer!=NULL) {
                        $singer = $singer . '、' . $values['name'];
                    }else {
                        $singer = $values['name'];
                    }
                }
                $songList[$index]['singer'] = $singer;
                
                $index++;
            }
            
        }
        
        return self::result(200, "搜索成功", $songList);
            
    }
    
    
    /**
     * $id  歌曲id
     * return 返回歌曲解析结果
     */
    public static function getUrl($id) {
        
        $playUrl = self::getFinalURL("http://music.163.com/song/media/outer/url?id=".$id.".mp3");
        $lyric = self::getLyr($id);
        
        if(empty($playUrl)) {
            
            $code = 400;
            $msg = "解析失败,请检查歌曲id,或联系管理员";
            
        }else {
            
            $code = 200;
            $msg = "解析成功";
            $data = ["id"=> $id, "purl"=> $playUrl, "lyric"=> $lyric];
            
        }
        
        return self::result($code, $msg, $data);
        
    }
    
    
    /**
     * 获取链接跳转最终地址
     * 
     */
    private static function getFinalURL($url) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_exec($ch);
        $redirectUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        curl_close($ch);
        return $redirectUrl;
    }
    
    
    /**
     * 获取歌词
     * @$param $id 歌曲id
     * return 歌词
     * 
     */
    
    private static function getLyr($id) {
        
        $result = self::httpRequest("http://music.163.com/api/song/lyric?lv=1&id=$id");
        
        $result = json_decode($result, true);
        
        return $result['lrc']['lyric']??'';
        
    }
    
    /**
     * CURL
     * 
     */
    public static function httpRequest($url, $params="", $headers = []) {
        
        // 获取歌曲的数据
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($headers, ['Content-Type: application/json']));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        $response = curl_exec($ch);
        curl_close($ch);
        
        return $response;
    }
    
    
    public static function result($code = 400, $msg, $data = array()) {
        
        header("Content-type: application/json;charset=utf-8");
        $code = intval($code);
        $result = ["code"=> $code, "msg"=> $msg, "data"=> $data, "author"=> "KongHen02"];
        return json_encode($result, 320);
        
    }
    
}

?>
3

评论 (0)

取消