user2945758
user2945758

Reputation:

PHP Trying to get property of non-object

First of all i know stackoverflow is full from this kind of erros but none of them is like mine so im posting this.

Im trying to get a JSON response from the api and as im trying to echo it im getting the Trying to get property of non-object error.

$apicalldata = file_get_contents("https://api.digitalocean.com/v1/droplets/?client_id=6356465465363546&api_key=f9a702abe442198a4168346435366436cc4cd2138dfc");

$call = json_decode($apicalldata);

echo $call->droplets->id;

This is the code im using. From this i'm expecting a response like this:

{
"status": "OK",
"droplets": [
  {
  "id": 100823,
  "name": "test222",
  "image_id": 420,
  "size_id":33,
  "region_id": 1,
  "backups_active": false,
  "ip_address": "127.0.0.1",
  "private_ip_address": null,
  "locked": false,
  "status": "active",
  "created_at": "2013-01-01T09:30:00Z"
}
 ]
 }

Any ides why am i having this problem? Also is the $call->droplets->id correct? Thanks for your time

Upvotes: 2

Views: 1157

Answers (2)

Patrick Q
Patrick Q

Reputation: 6393

It looks like droplets is an array. Give this a try.

$droplets = $call->droplets;
$myDroplet = $droplets[0];
$myDropletID = $myDroplet->id;
echo $myDropletID;

* Update after your comment *

$droplets = $call->droplets;

foreach($droplets as $droplet)
{
    $dropletID = $droplet->id;
    echo $dropletID;
}

Upvotes: 2

azhar
azhar

Reputation: 1757

try this

$call = json_decode($apicalldata);
foreach($call->Droplet as $adm)
{
echo "ID ".$adm->id."<br/>";
}

Upvotes: 0

Related Questions