【问题标题】:Fatal Error: Cannot use object of type stdClass as array?致命错误:不能将 stdClass 类型的对象用作数组?
【发布时间】:2019-10-28 16:09:28
【问题描述】:

我对 PhP 很陌生;我的网站今晚突然无缘无故停止工作,一旦我调试了我的 PDO 脚本,它就会返回此错误:

不能在 /var/www/html/v1/framework/api/stats.php:5 中使用 stdClass 类型的对象作为数组 堆栈跟踪:#0 /var/www/html/v1/framework/radio_stats.php( 5): include() #1 /var/www/html/v1/index.php(5): include('/var/www/html/v...') #2 {main}

我不知道我是不是做错了什么,但似乎一切都坏了。

$api = json_decode(file_get_contents('https://guardi.wearebounce.net/api/nowplaying'));
    if($api[0]->live->is_live == "true"){
        $return["presenter"] = $api[0]->live->streamer_name;
        $stmt = $pdo->query("SELECT * FROM `users` WHERE `username` = '".$return["presenter"]."'"); 
        $presenter = $stmt->fetch(PDO::FETCH_ASSOC);
        $presentera = $presenter["likes"];
    }else{
        $return["presenter"] = "AutoDJ";
        $presentera = -1;
        $presenter["avatar"] = "https://wearebounce.net/branding/LogoIconColour.png";
    }

【问题讨论】:

  • My website suddenly stopped working tonight
  • 我没有更改任何代码,所以我不知道。
  • 检查您使用的数据以了解发生了什么总是值得的。 print_r($api); 并使用那里的数据。看起来您之后的数据(计算第一级的数字索引)是$api->{'1'}->live->is_live
  • 您的代码易受 SQL 注入攻击。您应该使用准备好的语句。
  • try{ //在这里做点什么 }catch(Exception $e){ // 打印这个 $e 然后 die() :p }

标签: php stdclass


【解决方案1】:

他们一定改变了他们的 API,因为他们的响应如下所示:

{
    "1": {
        "station": {
            "id": 2,
            "name": "test",
            "shortcode": "test",
            "description": "",
            "frontend": "shoutcast2",
            "backend": "liquidsoap",
            "listen_url": "https:\\/\\/guardi.wearebounce.net:8010\\/radio.mp3?1572279272",
            "is_public": true,
            "mounts": [{
                "path": "\\/radio.mp3",
                "is_default": true,
                "id": 4,
                "name": "128kbps MP3",
                "url": "https:\\/\\/guardi.wearebounce.net:8010\\/radio.mp3?1572279272",
                "bitrate": 128,
                "format": "mp3",
                "listeners": {
                    "current": 0,
                    "unique": 0,
                    "total": 0
                }
            }],
            "remotes": []
        },
        "listeners": {
            "current": 0,
            "unique": 0,
            "total": 0
        },
        "live": {
            "is_live": false,
            "streamer_name": ""
        },
        "now_playing": {
            "elapsed": 2085856,
            "remaining": 0,
            "sh_id": 16422,
            "played_at": 1570193422,
            "duration": 0,
            "playlist": "",
            "is_request": false,
            "song": {
                "id": "ab574fe34f592faf5c3f27dbc52088a4",
                "text": "AzuraCast.com - AzuraCast is Live!",
                "artist": "AzuraCast.com",
                "title": "AzuraCast is Live!",
                "album": "",
                "lyrics": "",
                "art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
                "custom_fields": []
            }
        },
        "playing_next": null,
        "song_history": [{
            "sh_id": 16421,
            "played_at": 1570193242,
            "duration": 0,
            "playlist": "",
            "is_request": false,
            "song": {
                "id": "a723e3dfb497b24c5f730bad44328916",
                "text": "Animals",
                "artist": "",
                "title": "Animals",
                "album": "",
                "lyrics": "",
                "art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
                "custom_fields": []
            }
        }, {
            "sh_id": 16419,
            "played_at": 1570193227,
            "duration": 0,
            "playlist": "",
            "is_request": false,
            "song": {
                "id": "de05db4a94a8e9b547504e9e51682bfd",
                "text": "Bounce_ - _Bed_6",
                "artist": "Bounce_",
                "title": "_Bed_6",
                "album": "",
                "lyrics": "",
                "art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
                "custom_fields": []
            }
        }, {
            "sh_id": 16417,
            "played_at": 1570193032,
            "duration": 0,
            "playlist": "",
            "is_request": false,
            "song": {
                "id": "ee0707f2222751ce78d5f2f358c50312",
                "text": "Gangaman",
                "artist": "",
                "title": "Gangaman",
                "album": "",
                "lyrics": "",
                "art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
                "custom_fields": []
            }
        }, {
            "sh_id": 16415,
            "played_at": 1570192881,
            "duration": 0,
            "playlist": "",
            "is_request": false,
            "song": {
                "id": "ad3cbc1061248ee1f594ca5932b35bbb",
                "text": "Brave - Don Diablo",
                "artist": "Brave",
                "title": "Don Diablo",
                "album": "",
                "lyrics": "",
                "art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
                "custom_fields": []
            }
        }, {
            "sh_id": 16414,
            "played_at": 1570192852,
            "duration": 0,
            "playlist": "",
            "is_request": false,
            "song": {
                "id": "ab574fe34f592faf5c3f27dbc52088a4",
                "text": "AzuraCast.com - AzuraCast is Live!",
                "artist": "AzuraCast.com",
                "title": "AzuraCast is Live!",
                "album": "",
                "lyrics": "",
                "art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
                "custom_fields": []
            }
        }],
        "cache": "station"
    }
}

所以他们有一个键为 1 的对象,而不是一个对象数组。由于它是一个对象,因此您必须通过密钥访问它。而且由于key是一个数字,所以需要用

$response = $api->{1};

不幸的是,链接不能很好地工作(至少对我来说),所以你必须将它分配给上面的变量。然后你可以使用

$response->live->is_live;

【讨论】:

    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    相关资源
    最近更新 更多