带有 PHP 的 Lametric 和 Spotify api

问题描述 投票:0回答:0

我想创建一个应用程序,可以将 spotify 上播放的曲目发送到我的 LAMETRIC 时钟 我在 Github 上找到了一些很好的代码,我尝试调试它们,但我卡住了……你能帮我解决吗???

代码有效,我被困在一个返回值(空)的循环中,但它没有返回那个值

https://github.com/WilliamVenner/LaMetric-Spotify 这里是代码...

我在 spotify web 上创建了我的应用程序,我得到了我的客户 ID 和我的秘密,并且我准备好使用...... 我在 lametric dev 上创建我的应用程序:它已启动并正在运行 我在这里部署我的应用程序:https://c2x-services.com/lametric/currently_playing.php?show-artist=0&show-paused=0 可以访问

<?php
    
    $frames = array(
        "frames" => array(
            array(
                "text" => "?",
                "icon" => "i647"
            ),
        )
    );
    
    function output($text) {
        
        global $frames;
        
        $frames["frames"][0]["text"] = $text;
        
        echo(json_encode($frames));
        
    }

    if (!function_exists("getallheaders")) {
        
        // getallheaders is required to get the Authorization header as it does not appear
        // in $_SERVER or anywhere else. Therefore, try updating all of your software -
        // web server, PHP, etc. and try again.
        
        // This works absolutely fine on Apache and PHP 7.
        
        http_response_code("501 Not Implemented");
        error_log("https://github.com/WilliamVenner/LaMetric-Spotify/issues/2#issuecomment-325140347");
        return;
        
    }
    $headers = getallheaders();
    
    if (!isset($headers["Authorization"])) {
        output("X"); return;
    }
    
    $auth = $headers["Authorization"];
    
    require("inc/api.php");
    $Spotify = new Spotify($auth);
    $result = $Spotify -> API("me/player/currently-playing", false, true);
    
    if (isset($result["error"])) {
        
        if ($result["error"]["message"] == "invalid id") {
            
            output("[Local File]"); return;
            
        }
        
        if ($result["error"]["message"] == "The access token expired") {
            
            header("HTTP/1.1 401 Unauthorized");
            
            output("[X]"); return;
            
        }
        
        if ($result["error"]["message"] == "API rate limit exceeded") {
            
            output("Too many requests"); return;
            
        }
        
        output("!"); return;
        
    }
    
    if (!isset($result["item"])) {
        output("[X]"); return;
    }
    
    $track_info = $result["item"];
    
    if ($result["is_playing"] != true && $_GET["show-paused"] == "false") {
        
        output("[II]"); return;
        
    }
    
    $s = "";
    
    if ($_GET["show-paused"] == "true" && $result["is_playing"] == false) $s .= "[II] ";
    
    if ($_GET["show-artists"] == "true" && (($_GET["show-paused"] == "true" && $result["is_playing"] == false) || $result["is_playing"] == true)) {
        
        foreach($track_info["artists"] as $i => $artist) {
            
            if ($i > 0 && count($track_info["artists"]) > 1) $s .= ", ";
            
            $s .= $artist["name"];
            
        }
        
        $s .= " - ";
        
    }
    
    $s .= $track_info["name"];
    
    output($s);
    
?>
if (!isset($result["item"])) {
        output("[X]"); return;
    }

这个返回“null”

<?php
    
    class Spotify {
        
        private $auth;
        
        public function __construct($auth) {
            $this -> auth = $auth;
        }
        
        public function API($url, $post = false, $json = false) {
            
            if (!isset($this -> auth)) {
                throw new Exception('No authentication provided.');
                return;
            }
            
            $ch = curl_init();
            
            curl_setopt($ch, CURLOPT_URL, "https://accounts.spotify.com/authorize/$url");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            
            if ($post)
                curl_setopt($ch, CURLOPT_POST, 1);
            else
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
            
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: ' . $this -> auth]);
            
            $result = curl_exec($ch);
            curl_close($ch);
            
            if ($result)
                if ($json)
                    return json_decode($result,true);
                else
                    return $result;
            
            
        }
        
    }
    

?>

我被困在这里....我的lametric返回[X],我不明白为什么...

这里是API

php spotify lametric
© www.soinside.com 2019 - 2024. All rights reserved.