PHP通过新API解析网易云音乐

刘瑞琦

how_to_reg

•

2 个月前

•

3 评论

•

256 浏览


新的API很多歌曲都解析不了320kbps的,但是能听个响,付费歌曲不能解析。一言不合就放代码:

<?php
class MusicJson{
    private static $_PUBKEY = "65537";
    private static $_NONCE = "0CoJUm6Qyw8W8jud";
    private static $_MODULUS = "157794750267131502212476817800345498121872783333389747424011531025366277535262539913701806290766479189477533597854989606803194253978660329941980786072432806427833685472618792592200595694346872951301770580765135349259590167490536138082469680638514416594216629258349130257685001248172188325316586707301643237607";
    public function netease_song($music_id){
        $response = $this->netease_new_api($music_id);
        if ($response){
            $result = array(
                "song_id" => $music_id,
                "song_src" => $response['url']
            );
            return $result;
        }
        return false;
    }
    private function netease_http($url, $post=null){
        $refer = "http://music.163.com/";
        $header = array(
            'X-Real-IP: 118.88.88.88',
            'Cookie: appver=2.0.2',
            'Accept-Language: zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
        );
        $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);
        if ($post){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        $cexecute = curl_exec($ch);
        curl_close($ch);
        if ($cexecute) {
            $result = json_decode($cexecute, true);
            return $result;
        }else{
            return false;
        }
    }
    private function netease_new_api($song_id, $bit_rate=320000){
        $url = 'http://music.163.com/weapi/song/enhance/player/url?csrf_token=';
        $data = "{'ids': [$song_id], 'br': $bit_rate, 'csrf_token': ''}";
        $data = self::encrypted_request($data);
        $result = $this->netease_http($url, http_build_query($data));
        if (isset($result['data'][0])) return $result['data'][0];
        return false;
    }
    public function encrypted_request($data){
        $secKey = self::randString(16);
        $encText = self::aesEncrypt( self::aesEncrypt($data, self::$_NONCE), $secKey );
        $pow = self::bchexdec( bin2hex( strrev($secKey) ) );
        $encKeyMod = bcpowmod($pow, self::$_PUBKEY, self::$_MODULUS);
        $encSecKey = self::bcdechex($encKeyMod);
        $data = array(
            'params' => $encText,
            'encSecKey' => $encSecKey
        );
        return $data;
    }
    private function randString($length){
        $chars = 'qiaobil0123456789';
        $result = '';
        $max = strlen($chars) - 1;
        for ($i = 0; $i < $length; $i++){
            $result .= $chars[rand(0, $max)];
        }
        return $result;
    }
    private function aesEncrypt($data, $secKey){
        if (function_exists('openssl_encrypt')) {
            $cip = openssl_encrypt($data, 'aes-128-cbc', pack('H*', bin2hex($secKey)), OPENSSL_RAW_DATA, "0102030405060708");
        } else {
            $pad = 16 - strlen($data) % 16;
            $data = $data . str_repeat(chr($pad), $pad);
            $cip = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secKey, $data, MCRYPT_MODE_CBC, "0102030405060708");
        }
        $cip = base64_encode($cip);
        return $cip;
    }
    private function bcdechex($dec){
        $hex = '';
        do {
            $last = bcmod($dec, 16);
            $hex = dechex($last).$hex;
            $dec = bcdiv(bcsub($dec, $last), 16);
        } while($dec>0);
        return $hex;
    }
    private function bchexdec($hex){
        if(strlen($hex) == 1) {
            return hexdec($hex);
        } else {
            $remain = substr($hex, 0, -1);
            $last = substr($hex, -1);
            return bcadd(bcmul(16, self::bchexdec($remain)), hexdec($last));
        }
    }
}
$get_netease = new MusicJson;

function get_music($url){
    $music = '';
    if (preg_match('#https://music.163.com/\#/song\?id=(\d+)#i', $url, $matches)) {
        global $get_netease;
        $song_id = $matches[1];
        $netease_json = $get_netease->netease_song($song_id);
        $music = $netease_json['song_src'];
    }
    return $music;
}
echo get_music("https://music.163.com/#/song?id=1357825630");
?>

3 条评论

一起聊聊这个话题

评论一下

捷径盒让生活更简单

基于

arrow_upward