网易云音乐获取


    //获取音乐链接
    public function GetMusic($music_name)
    {
     
        $music = ['title' => '', 'file_path' => '', 'song_id' => ''];
        $url = 'http://musicapi.leanapp.cn/search?keywords=' . urlencode($music_name);
        $song_list = json_decode(file_get_contents($url), true);
        for ($i = 0; $i < count($song_list['result']['songs']); $i++) {
     

            $song_id = $song_list['result']['songs'][$i]['id'];
            $check_song_id = Db::table('music')->where('song_id', $song_id)->find();
            if ($check_song_id['path']) {
     
                $music = ['title' => $check_song_id['title'], 'file_path' => $check_song_id['path'], 'song_id' => $song_id];
                return $music;
            } else {
     
                $song_info = json_decode(file_get_contents('https://api.imjad.cn/cloudmusic/?type=song&id=' . $song_id), true);

                if (isset($song_info['data'][0]['url']) && !empty($song_info['data'][0]['url'])) {
     
                    $music = ['title' => $song_list['result']['songs'][$i]['name'], 'file_path' => $song_info['data'][0]['url'], 'song_id' => $song_id];
                    if (!$check_song_id) {
     
                        Db::table('music')->insert(
                            [
                                'title' => $music['title'],
                                'song_id' => $song_id,
                                'path' => '',
                                'update_time' => date('Y-m-d H:i:s'),
                                'create_time' => date('Y-m-d H:i:s')
                            ]);
                    }
                    return $music;
                }
            }
        }
        return $music;
    }

    //网易云音乐上传
    public function OSSupload($song_id, $file_path)
    {
     

        $OSS_config = [
            'KeyId' => '',
            'KeySecret' => '',
            'Endpoint' => '',
            'Bucket' => ''
        ];

        //先下载
        $pwd = shell_exec('pwd');
        $pwd = substr($pwd, 0, strlen($pwd) - 1) . '/music/' . $song_id . '.mp3';
        $cmd = "wget -O $pwd $file_path";
        shell_exec($cmd);

        try {
     
            $ossClient = new OssClient($OSS_config['KeyId'], $OSS_config['KeySecret'], $OSS_config['Endpoint']);
            $fileName = 'music/' . $song_id . '.mp3';
            $result = $ossClient->uploadFile($OSS_config['Bucket'], $fileName, $pwd);
            $arr = [
                'code' => '0',
                'filePath' => $result['info']['url'],
                'fileName' => $fileName,
                'file_name' => $song_id . '.mp3',
            ];

            Db::table('music')->where('song_id', $song_id)->update(['path' => $result['info']['url']]);
        } catch (OssException $e) {
     
            return $e->getMessage();
        }
        return $arr;
    }

你可能感兴趣的:(网易云音乐获取)