简体   繁体   English

PHP试图获取非对象的属性

[英]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. 首先,我知道stackoverflow已经从这种错误中充满了,但是没有一个像我这样,所以即时消息发布了。

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. 我试图从api获取JSON响应,并试图回显它,从而获得了Trying to get property of non-object错误的Trying to get property of non-object

$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? $call->droplets->id正确吗? Thanks for your time 谢谢你的时间

It looks like droplets is an array. 看起来droplets是一个数组。 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;
}

try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM