format(true); // 启用格式化功能 if($source == 'kugou' || $source == 'baidu') { define('NO_HTTPS', true); // 酷狗和百度音乐源暂不支持 https } elseif(($source == 'netease') && $netease_cookie) { $API->cookie($netease_cookie); // 解决网易云 Cookie 失效 } // 没有缓存文件夹则创建 if(defined('CACHE_PATH') && !is_dir(CACHE_PATH)) createFolders(CACHE_PATH); $types = getParam('types'); switch($types) // 根据请求的 Api,执行相应操作 { case 'url': // 获取歌曲链接 $id = getParam('id'); // 歌曲ID $data = $API->url($id); echojson($data); break; case 'pic': // 获取歌曲链接 $id = getParam('id'); // 歌曲ID $data = $API->pic($id); echojson($data); break; case 'lyric': // 获取歌词 $id = getParam('id'); // 歌曲ID if(($source == 'netease') && defined('CACHE_PATH')) { $cache = CACHE_PATH.$source.'_'.$types.'_'.$id.'.json'; if(file_exists($cache)) { // 缓存存在,则读取缓存 $data = file_get_contents($cache); } else { $data = $API->lyric($id); // 只缓存链接获取成功的歌曲 if(json_decode($data)->lyric !== '') { file_put_contents($cache, $data); } } } else { $data = $API->lyric($id); } echojson($data); break; case 'download': // 下载歌曲(弃用) $fileurl = getParam('url'); // 链接 header('location:$fileurl'); exit(); break; case 'userlist': // 获取用户歌单列表 $uid = getParam('uid'); // 用户ID $url= 'http://music.163.com/api/user/playlist/?offset=0&limit=1001&uid='.$uid; $data = file_get_contents($url); echojson($data); break; case 'playlist': // 获取歌单中的歌曲 $id = getParam('id'); // 歌单ID if(($source == 'netease') && defined('CACHE_PATH')) { $cache = CACHE_PATH.$source.'_'.$types.'_'.$id.'.json'; if(file_exists($cache) && (date("Ymd", filemtime($cache)) == date("Ymd"))) { // 缓存存在,则读取缓存 $data = file_get_contents($cache); } else { $data = $API->format(false)->playlist($id); // 只缓存链接获取成功的歌曲 if(isset(json_decode($data)->playlist->tracks)) { file_put_contents($cache, $data); } } } else { $data = $API->format(false)->playlist($id); } echojson($data); break; case 'search': // 搜索歌曲 $s = getParam('name'); // 歌名 $limit = getParam('count', 20); // 每页显示数量 $pages = getParam('pages', 1); // 页码 $data = $API->search($s, [ 'page' => $pages, 'limit' => $limit ]); echojson($data); break; default: echo '信息

MKOnlinePlayer

Github: https://github.com/mengkunsoft/MKOnlineMusicPlayer


'; if(!defined('DEBUG') || DEBUG !== true) { // 非调试模式 echo '

Api 调试模式已关闭

'; } else { echo '

您已开启 Api 调试功能,正常使用时请在 api.php 中关闭该选项!


'; echo '

PHP 版本:'.phpversion().' (本程序要求 PHP 5.4+)


'; echo '

服务器函数检查

'; echo '

curl_exec: '.checkfunc('curl_exec',true).' (用于获取音乐数据)

'; echo '

file_get_contents: '.checkfunc('file_get_contents',true).' (用于获取音乐数据)

'; echo '

json_decode: '.checkfunc('json_decode',true).' (用于后台数据格式化)

'; echo '

hex2bin: '.checkfunc('hex2bin',true).' (用于数据解析)

'; echo '

openssl_encrypt: '.checkfunc('openssl_encrypt',true).' (用于数据解析)

'; } echo ''; } /** * 创建多层文件夹 * @param $dir 路径 */ function createFolders($dir) { return is_dir($dir) or (createFolders(dirname($dir)) and mkdir($dir, 0755)); } /** * 检测服务器函数支持情况 * @param $f 函数名 * @param $m 是否为必须函数 * @return */ function checkfunc($f,$m = false) { if (function_exists($f)) { return '可用'; } else { if ($m == false) { return '不支持'; } else { return '不支持'; } } } /** * 获取GET或POST过来的参数 * @param $key 键值 * @param $default 默认值 * @return 获取到的内容(没有则为默认值) */ function getParam($key, $default='') { return trim($key && is_string($key) ? (isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : $default)) : $default); } /** * 输出一个json或jsonp格式的内容 * @param $data 数组内容 */ function echojson($data) //json和jsonp通用 { header('Content-type: application/json'); $callback = getParam('callback'); if(defined('HTTPS') && HTTPS === true && !defined('NO_HTTPS')) { // 替换链接为 https $data = str_replace('http:\/\/', 'https:\/\/', $data); $data = str_replace('http://', 'https://', $data); } if($callback) //输出jsonp格式 { die(htmlspecialchars($callback).'('.$data.')'); } else { die($data); } }