使用Meting项目在服务器搭建后台api和使用api创建自己的音乐播放器

api的创建

基于开源项目Meting ,下载这个类到php object中, 这个项目已经很成熟了, 只要实例化然后调用里面的方法就可以获取到数据,使用php把获取到的数据进行提取, 获取到需要内容再根据收到的请求返回不同的数据。

此api参考了MKOlineMusic的api

同时此api对外开放, 只要给 http://api.sixjiang.cn/api发送get请求就可以直接调用

2019/8/14 更新, 新增了jsonp的支持, 直接调用callback就可以规避同源策略拦截

2019/9/2更新, 新增了外文歌拼接翻译歌词

<?php
require_once('Meting.php');

use Metowolf\Meting;

$api = new Meting("netease");
$type = $_GET["type"];
$id = $_GET["id"];
$aName = array();
$aid = array();
$aUrl = array();
$aPic = array();
$aLrc = array();
$aArtist = array();
/*$data = json_decode($data);*/

# $data = $api->format(true)->playlist(2922685890);

$data = json_decode($data);

switch($type){
    case "name":
        $dat = $api->format(true)->song($id);
        $dat = json_decode($dat);
        $data = get_dat($dat);
        echo $data[0]->name;
        break;
    case "artist":
        break;
    case "cover":
        $dat = $api->format(true)->song($id);
        $dat = json_decode($dat);
        $data = get_dat($dat);
        echo "<script>window.location.href='".$data[0]->cover."'</script>";
        break;
    case "lrc":

        $dat = $dat->lrc;
        echo "<pre>".$dat->lyric."</pre>";*/
        $dat = json_decode($api->lyric($id));

        $trans_dat = $dat->tlyric;
        $dat = $dat->lrc;

        $aOriginal_lrc = explode("\n",$dat->lyric);
        $aTrans_lrc = explode("\n",$trans_dat->lyric);
        // print_r($aOriginal_lrc);

        foreach ($aOriginal_lrc as $eachLine){
            // 右括号做分隔符

            $aSplit_lrc = explode("]",$eachLine);
array($aSplit_lrc[0]."]"=>$aSplit_lrc[1]));
            $aLrc_summary[$aSplit_lrc[0]."]"] = $aSplit_lrc[1];

        }

        if($aTrans_lrc){
            // 如果有翻译歌词, 就遍历翻译歌词并且把翻译歌词和原版歌词进行拼接
            // 时间戳为键
            $aTestTrans = Array();
            foreach ($aTrans_lrc as $eachLine){

                $aSplit_lrc = explode("]",$eachLine);

                if($aSplit_lrc[1]){

                    $aLrc_summary[$aSplit_lrc[0]."]"] = $aLrc_summary[$aSplit_lrc[0]."]"]." / ".$aSplit_lrc[1];

                }


            }
        }
        echo "<pre>";
        foreach ($aLrc_summary as $key=>$val){
            echo $key.$val."\n";
        }
        echo "</pre>";

        break;
    case "url":
        $dat = json_decode($api->url($id));
        $url = $dat->data[0]->url;
        echo "<script>window.location.href='$url'</script>";
        break;
    case "playlist":
        $dat = $api->format(true)->playlist($id);
        $dat = json_decode($dat);
        $data = get_dat($dat);
        echojson(json_encode($data));
        break;
    case "single":
        $dat = $api->format(true)->song($id);
        $dat = json_decode($dat);
        $data = get_dat($dat);
        echojson(json_encode($data));
}


function get_dat($dat){
    global $aName,$aid,$aPic,$api,$aArtist,$aUrl;

    $aRes = array();

    foreach ($dat as $datum){
        $flag = array_search($datum, $dat);

        array_push($aName,$datum->name);
        array_push($aArtist,implode("/",$datum->artist));
        array_push($aid,$datum->id);
        array_push($aPic,$datum->pic_id);
    }
    foreach ($aid as $id){
        $flag = array_search($id,$aid);

        $pic = $api->pic($aPic[$flag]);
        $url = $api->url($id);

        $url = json_decode($url, true);
        $pic = json_decode($pic, true);

        $aPic[$flag] = $pic["url"];
        array_push($aUrl,$url["url"]);
    }
    for($i=0;$i<sizeof($aid);$i++){
        # echo $i."<br>";
        array_push(
            $aRes,
            (object)array(
                "name"=>$aName[$i],
                "artist"=>$aArtist[$i],
                "url"=>$aUrl[$i],
                "lrc"=>"https://www.sixjiang.cn/api/163/?type=lrc&id=".$aid[$i],
                "cover"=>$aPic[$i])
        );

    }
    return $aRes;
}


function getParam($key, $default='')
{
    return trim($key && is_string($key) ? (isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : $default)) : $default);
}


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);
    }
}
if(!($type&$id)){
    ?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Netease</title>
    <!--favicon.png-->
    <link href="./favicon.png" rel="icon" type="image/x-icon" />
</head>
<h1>参数说明</h1>
type: 类型<br>
     name 歌曲名<br>
     artist 歌手<br>
     url 链接<br>
     cover 封面<br>
     lrc 歌词<br>
     single 获取以上所有信息(单曲)<br>
     playlist 获取以上所有信息(歌单)<br><br>
id: 单曲ID或歌单ID<br><br>
此API基于 <a href="https://github.com/metowolf/Meting" target="_blank">Meting</a> 构建。<br><br>
例:<a href="https://api.sixjiang.cn/api/163/?type=url&id=431791474" target="_blank">https://api.sixjiang.cn/api/163/?type=url&id=431791474</a><br>
<a href="https://api.sixjiang.cn/api/163/?type=single&id=1363408250" target="_blank" style="padding-left:48px">https://api.sixjiang.cn/api/163/?type=single&id=1363408250</a><br>
<a href="https://api.sixjiang.cn/api/163/?type=playlist&id=2922685890" target="_blank" style="padding-left:48px">https://api.sixjiang.cn/api/163/?type=playlist&id=2922685890</a>
<div id="extwaiimpotscp" style="display:none" v="{aa90" f="ZTJGaE9UQTVNekkwTFRjMU1qQXROR1JqWkMwNVpXSXdMVGxtTUdFNVpXTXpZekF3TTMwPQ==" q="d830a7e4" c="128.5" i="131.6" u="15.81" s="19080820" w="false" m="BMe=" vn="yotcn"></div>
</html>
<?php
}
?>

使用api获取到数据给aplayer框架

(function() {
    var song_list = Array();
    // $(".aplayer").css("z-index",99999);
    $(function () {
        $("#aplayer").css("z-index",99999)
    });

    $.ajax({
        url:"https://api.sixjiang.cn/api/163/?type=playlist&id=2922685890",
        type: "get",
        dataType: "json"
    }).done(function (dat) {
        // console.log(dat);
        for(var i=0;i<dat.length;i++){
            song_list.push(
                {
                    "name":dat[i].name,
                    "artist":dat[i].artist,
                    "url":dat[i].url,
                    "cover":dat[i].cover,
                    "lrc":dat[i].lrc
                }
            );
        }
    });

    setTimeout( function () {
        console.log(song_list.length);
        const ap = new APlayer({
            container: document.getElementById('aplayer'),
            fixed:true,
            listFolded: false,
            listMaxHeight: 10,
            lrcType: 3,
            theme:"#66ccff",
            audio: song_list
        });
    },2000);
})();

刷新网页后就可以看到左下角多了一个播放器了

以上