0

Link for API: Click Here

I want to get datetime and type parameters i try to but i get:

Notice: Trying to get property of non-object

Here is my try t

$api_key = "apikey here";
$server = "http://api.uptimerobot.com/getMonitors?apiKey=$api_key&logs=1&alertContacts=1&responseTimes=1&responseTimesAverage=180&format=json";
$server2 = file_get_contents($server);
$obj = @json_decode($server2);
$cc =  $alert->datetime;
echo $cc;

its not working maybe because its an arrayed json.
Can someone give me whole (right) code to get it work ?
I need only type and datetime parameters in while cycle (prefer)

Edited by grakovski

3
Contributors
6
Replies
50
Views
3 Years
Discussion Span
Last Post by Isaac_4
0

This isn't all of your code is it? Where are you defining $alert and what is it?
If $obj is an array then you need to delve into it to get the data you want.
If you're unsure of the format of the array use var_dump($obj) to view it on screen.

0

The problem is that the return result is not a valid JSON string:

jsonUptimeRobotApi({"stat": "fail","id":"100","message":"apiKey not mentioned or in a wrong format"})

This is, rather, a JavaScript function call. This type of "JSON" return is meant for consumption on the front-end of a website, inside the browser.

The easiest fix is probably to just parse out the JS function call with some string functions:

$api_key = "apikey here";
$server = "http://api.uptimerobot.com/getMonitors?apiKey=" . $api_key . "&logs=1&alertContacts=1&responseTimes=1&responseTimesAverage=180&format=json";
$server2 = file_get_contents($server);

if(substr($server2, 0, 19) == 'jsonUptimeRobotApi(' && substr($server2, -1, 1) == ')')
{
    $json = substr($server2, 19, strlen($server2) - 20);
    if(!is_null($json)) {
        $obj = json_decode($json);
        var_dump($obj);

        $cc =  $obj->datetime;// I assume that $alert was meant to be $obj
        echo $cc;
    }
}

Edited by Isaac_4

0

Yep $alert must be $obj :)

@Isaac 4 - your example not working for me. i give only information in varm_dump, but $cc is empty.

0

Well without a valid API key the call returns an error object which does not have a datetime property.

Obviously you don't want to post your API key but can you post what object you do get from the last var_dump? It needs to actually have a datetime property or you will get an error (if errors are enabled) and $cc will remain unset.

0

I get only this:
http://i.imgur.com/NpLYtuy.png
The api key is valid (that is my api key)
u118216-ae6b502f1b3f6d272d0f9437

And i dont see a while cycle or foreach (i want all information with 'datetime' and all information in 'type')

Edited by grakovski

1

Well it's actully returning some data. The three ... in bold mean that the output is being truncated because it's too deep. Try using print_r() instead of var_dump():

echo '<pre>';
print_r($obj);
echo '</pre>';

That should show you all the nested objects and arrays. You have to dig deeper into the returned data.

You probably have to do something like

foreach($obj->monitors->monitor as $monitor) {
    var_dump($monitor);
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.