arrays – JSON Decode issue with PHP (Pixabay API)-ThrowExceptions

Exception or error:

I’m trying to get the image URL of the first object from the Pixabay API in PHP. Unfortunately with my attempt I get


an empty string back.

"total": 4692,
"totalHits": 500,
"hits": [
        "id": 195893,
        "pageURL": "",
        "largeImageURL": "",
        "userImageURL": "",
        "id": 73424,
How to solve:

Problem here is bad json. Extra comma on the end makes the problem. Thats why you should use something like to test JSON.

Before this I tried in localhost to get json data from api.
After json_decode function I got NULL value.

And after deleting extra commas on the end I get following response from json_decode()

object(stdClass)#1 (3) { ["total"]=> int(4692) ["totalHits"]=> int(500) ["hits"]=> array(2) { [0]=> object(stdClass)#2 (4) { ["id"]=> int(195893) ["pageURL"]=> string(51) "" ["largeImageURL"]=> string(48) "" ["userImageURL"]=> string(64) "" } [1]=> object(stdClass)#3 (1) { ["id"]=> int(73424) } } }

Full code:


$test = file_get_contents('');

$data = json_decode($test);

echo $data->hits[0]->largeImageURL;


And testing your example

Leave a Reply

Your email address will not be published. Required fields are marked *