This is somewhat related to my earlier problem, “Trying to get property of non-object” error
Just to summarize:
I’ve got this route
array(
"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 generates this json
{
"views": {
"main": "37519",
"services": "7208",
"total": 44727
},
"videos": {
"main": "48",
"services": "22",
"total": 70
},
"subscribers": {
"main": "184",
"services": "35",
"total": 219
}
}
and is called with this
$api = json_decode(file_get_contents($site->url()."/api/yt"), TRUE);
It was working fine when I tested it, but I don’t think I edited anything related to that, but it then started to show blank, so I tried to just echo the file contents, without decoding the json, and I then got this error up (both dev and production server)
Notice: Undefined index: HTTP_USER_AGENT in /home/nginx/gsponsor.com/public/kirby/toolkit/lib/s.php on line 113
// and the json response
To solve this I had to add a UA string to to file_get_contents()
So now to call the api out I have to do this
$context = stream_context_create(['http' => ['user_agent' => 'PHP 7.0.2 file_get_contents()']]);
$api = json_decode(file_get_contents($site->url()."/api/yt", false, $context), TRUE);
This is the snippet it’s complaining about, $_SERVER[“HTTP_USER_AGENT”] is the undefined index
public static function fingerprint() {
if(!r::cli()) {
return sha1($_SERVER['HTTP_USER_AGENT'] . (ip2long($_SERVER['REMOTE_ADDR']) & ip2long('255.255.0.0')));
} else {
return '';
}
}
I know it has worked before, it worked before I turned on debug mode, which threw me the “Trying to get property of non-object” error, and when I solved that thanks to @jbeyerstedt, it was working.
All I did was install the Pear Mail function, but I removed the function, and it was still not working. Also tried with a backup server without the Pear Mail, and it too was reporting the undefined index.
I tried to go back to the functions, which reported the “Trying to get property of non-object” error, and it’s working again. With debug turned off.