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)

Recommended Answers

All 6 Replies

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.

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;
    }
}

Yep $alert must be $obj :)

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

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.

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')

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.