-1

I'm quite new to PhP; My website suddenly stopped working tonight for no reason and once I have debugged my PDO script, it returns this error:

Cannot use object of type stdClass as array in /var/www/html/v1/framework/api/stats.php:5 Stack trace: #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}

I don't know if I'm doing something wrong, but everything seems to have broken.

$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";
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jaiden
  • 3
  • 2
  • 2
    `My website suddenly stopped working tonight` <- nothing ever suddenly stops working, a change happened - find the culprit! :) – treyBake Oct 28 '19 at 16:11
  • I haven't changed any of the code, so I don't know. – Jaiden Oct 28 '19 at 16:11
  • 1
    It's always worth checking the data your working with to see what is happening. `print_r($api);` and work with the data from there. Looks like the data your after (counting the numeric index at the first level) is `$api->{'1'}->live->is_live` – Nigel Ren Oct 28 '19 at 16:17
  • Your code is vulnerable to SQL injection. You should use prepared statements. – Dharman Oct 28 '19 at 16:20
  • try{ //Do stuff here }catch(Exception $e){ // print this $e and then die() :p } – Vipertecpro Oct 28 '19 at 18:17

1 Answers1

0

They must have changed their API, as their response looks like this:

{
    "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"
    }
}

So they have an object with a key of 1, instead an array of objects. Since it's an object, you'll have to access it via the key. And since the key is a number, you need to get it with

$response = $api->{1};

Unfortunately, chaining doesn't work well (at least for me), so you'll have to assign it to a variable as above. Then you can use

$response->live->is_live;
aynber
  • 22,380
  • 8
  • 50
  • 63