"Trying to get property of non-object" error

So I’ve set debug to true, and it is throwin me some strange errors that doesn’t appear without it, and is breaking the site.

I’ve got this route

    "pattern" => "api/yt",
    "method" => "GET",
    "action" => function() {
        $api_src        = c::get("api.youtube.channel.src") . c::get("api.youtube.key");
        $api_src_alt    = c::get("api.youtube.channel2.src") . c::get("api.youtube.key");

        $api_alt        = json_decode(file_get_contents($api_src_alt));
        $api            = json_decode(file_get_contents($api_src));

        $views          = $api->items[0]->statistics->viewCount + $api_alt->items[0]->statistics->viewCount;
        $videos         = $api->items[0]->statistics->videoCount + $api_alt->items[0]->statistics->videoCount;
        $subscribers    = $api->items[0]->statistics->subscriberCount + $api_alt->items[0]->statistics->subscriberCount;
        return response::json(array(
            "views" => array(
                "main" => $api->items[0]->statistics->viewCount,
                "services" => $api_alt->items[0]->statistics->viewCount,
                "total" => $api->items[0]->statistics->viewCount + $api_alt->items[0]->statistics->viewCount
            "videos" => array(
                "main" => $api->items[0]->statistics->videoCount,
                "services" => $api_alt->items[0]->statistics->videoCount,
                "total" => $api->items[0]->statistics->videoCount + $api_alt->items[0]->statistics->videoCount
            "subscribers" => array(
                "main" => $api->items[0]->statistics->subscriberCount,
                "services" => $api_alt->items[0]->statistics->subscriberCount,
                "total" => $api->items[0]->statistics->subscriberCount + $api_alt->items[0]->statistics->subscriberCount

which gives me this json response as I want
debug = off

    "views": {
        "main": "37519",
        "services": "7163",
        "total": 44682
    "videos": {
        "main": "48",
        "services": "22",
        "total": 70
    "subscribers": {
        "main": "184",
        "services": "35",
        "total": 219

And I pull that json response with file_get_contents() and then json_decode() it, which works fine, and gives me the outputs I want
debug = off

$api = json_decode(file_get_contents($site->url()."/api/yt"));
echo $api->views->total; //44682
echo $api->videos->total; //70

but when I turn on debug mode, I get this
debug = on

Notice: Trying to get property of non-object in /Volumes/TheWiseGrey/WebServer/gsponsor.dev/site/templates/home.php on line 35

I’m using Kirby 2.2.3

What’s on site/templates/home.php on line 35 ?

I’ll definitely help figure out where the error is really coming from.

It’s what I said earlier with $api
But I’ll post the same thing here

<?=str_replace(".", ",", round(($api->views->total/1000), 1))?>k

This would output 44,7k when debug is turned of.
And I’ve got it once more calling the total videos


Which would output 70
I’ve tried using <?php echo too, still the same error.
But as I said, the error is gone when I turn off debug

Your json file, you load to the $api variable is a nested array, not an object. So you have to use the array notation to get the elements:


I’m surprised, that your code seems to work anyway, if error reporting is turned off.

And of course the error messages are only displayed, if the debug option is turned on. Otherwise they are ignored.


I changed $api->views->total for $api['views']['total']. No luck there, now it stops rendering there.

I’m sorry, I forgot some little detail, I used in another project. You should use

$api = json_decode(file_get_contents($site->url()."/api/yt"), TRUE);

The second parameter means When TRUE, returned objects will be converted into associative arrays. Then it would work.

Yeah, that works, thank you