QQ音乐搜索及解析接口

KongHen02
2024-01-10 / 1 评论 / 377 阅读 / 正在检测是否收录...

抓的QQ音乐网站接口,自己写了一个QQ音乐搜索及解析接口
需要会员账号的key,从cookie里取qq_keyst,修改QqJx.php内的qq_keyst

index.php


// 接口入口

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

require "QqJx.php";

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

exit($result);

QqJx.php


/**
 * QQ音乐解析
 * 作者:KongHen02
 */
class QqJx
{
    
    /**
     * $qq  会员QQ账号
     * $qq_keyst  会员QQ账号cookie中的qq_keyst
     * $params  请求参数,无需修改
     */
     
    // 修改
    private static $qq = "";
    private static $qq_keyst = "";
    private static $params;
    
    
    /**
     * 音乐搜索
     * @param $name  歌曲名称
     * @param $page  页码,每页20条
     * return 返回歌曲搜索结果
     */
    public static function searchSong($name, $page) {
        
        if(empty($name)) {
            
            return self::result(400, "name不可为空,搜索时name为歌曲名称");
            
        }
        
        // QQ音乐搜索接口
        $params = '{"req_0":{"method":"DoSearchForQQMusicDesktop","module":"music.search.SearchCgiService","param":{"remoteplace":"txt.mqq.all","searchid":"","search_type":0,"query":"'.$name.'","page_num":'.$page.',"num_per_page":20}}}';
        
        $result = self::httpRequest("https://u.y.qq.com/cgi-bin/musicu.fcg?_webcgikey=DoSearchForQQMusicDesktop", $params);
        
        // 处理搜索结果
        $result = json_decode($result, true);
        $songData = $result['req_0']['data']['body']['song']['list'];  // 歌曲数据
        
        // 返回数据
        if(empty($songData)) {
            
            return self::result(400, "搜索接口失效或不存在当前名称歌曲,请联系管理员");
            
        }
            
        // 处理歌曲信息
        $songList = [];
        foreach ($songData as $key=> $value) {
            $songList[$key]['id'] = $value['mid'];
            $songList[$key]['name'] = $value['title'];
            $singer = "";
            foreach ($value['singer'] as $values) {
                if($singer!=NULL) {
                    $singer = $singer . '、' . $values['name'];
                }else {
                    $singer = $values['name'];
                }
            }
            $songList[$key]['singer'] = $singer;
        }
        
        return self::result(200, "搜索成功", $songList);
            
    }
    
    
    /**
     * 音乐解析
     * @param $id  歌曲id
     * $params  请求体
     * return 返回歌曲解析结果
     */
    public static function getUrl($id) {
        
        $params = '{"comm":{"cv":4747474,"ct":24,"format":"json","inCharset":"utf-8","outCharset":"utf-8","notice":0,"platform":"yqq.json","needNewCode":1,"uin":'.self::$qq.'},"song":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"6772351499","songmid":["'.$id.'"],"songtype":[0],"uin":"'.self::$qq.'","loginflag":1,"platform":"20"}}}';
        
        $headers = [
            'Cookie: uin='.self::$qq.'; qm_keyst='.self::$qq_keyst
        ];
        
        // 签名字符串
        $signData = 'zza' . self::uuidGenerate() . md5('CJBPACrRuNy7' . $params);
        
        $response = self::httpRequest('https://u.y.qq.com/cgi-bin/musics.fcg?sign='.$signData, $params, $headers);
        
        // 处理QQ音乐返回的数据
        $resuilt = json_decode($response, true)['song']['data']['midurlinfo'][0];
        $songId = $resuilt['songmid'];  // 歌曲ID
        $playUrl = $resuilt['purl'];  // 播放链接
        
        if(empty($playUrl)) {
            
            $code = 400;
            $msg = "解析失败,请检查歌曲id,或联系管理员";
            
        }else {
            
            // 拼接播放链接
            $playUrl = "https://dl.stream.qqmusic.qq.com/$playUrl";
            $code = 200;
            $msg = "解析成功";
            $data = ["id"=> $songId, "purl"=> $playUrl];
            
        }
        
        return self::result($code, $msg, $data);
        
    }
    
    
    /**
     * signData  签名字符串 加密的方式为 zza + 一个10-16位的随机字符串 + (固定字串 CJBPACrRuNy7 + 请求数据)拼接的 md5 值
     * return  返回歌曲信息
     */
    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;
    }
    
    
    /**
    * return  输出10-26位随机字符串
    */
    private static function uuidGenerate() {
        
        $length = rand(10, 16);
        $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        
        return $randomString;
    }
    
    
    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

评论 (1)

取消
  1. 头像
    小杨
    Windows X64 · Google Chrome

    会员会过期,有解决办法么

    回复