我对PhP很陌生;我的网站今晚突然无故停止工作,一旦我调试了PDO脚本,它就会返回此错误:
在/var/www/html/v1/framework/api/stats.php:5中无法将stdClass类型的对象用作数组:5堆栈跟踪:#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";
}
答案 0 :(得分: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的对象,而不是对象数组。由于它是一个对象,因此您必须通过密钥来访问它。由于密钥是数字,因此需要使用
$response = $api->{1};
不幸的是,链接不能很好地工作(至少对我而言),因此您必须如上所述将其分配给变量。然后您可以使用
$response->live->is_live;