Notice: Trying to get property of non-object error

2020-01-23 04:46发布

i am trying to get data from:

http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson

but if i want to get player_name variable with this code:

<?  
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
  $pjs = json_decode($js);
  var_dump($pjs->{'player_name'});
?>

i get error:

Notice: Trying to get property of non-object in **\htdocs\index.php on line 9 + var_dump() returns: NULL

var_dump($pjs) returns:

array(1) { [0]=> object(stdClass)#52 (15) { ["player_name"]=> string(11) "Mick_Gibson" ["player_id"]=> int(88) ["rank"]=> string(12) "FIRE TURTLEE" ["lastseen"]=> int(1393797692) ["registration_date"]=> string(19) "2012-08-10 17:01:34" ["last_mission_date"]=> string(19) "2014-03-02 21:41:50" ["time_offset"]=> int(1) ["house_id"]=> int(611) ["fines"]=> int(0) ["wanted"]=> int(0) ["police_badge"]=> bool(true) ["vip"]=> bool(false) ["staff"]=> NULL ["stats"]=> object(stdClass)#53 (23) { ["score"]=> int(2941) ["convoy_score"]=> int(818) ["ARTIC"]=> int(515) ["DUMPER"]=> int(565) ["TANKER"]=> int(56) ["CEMENT"]=> int(163) ["TRASH"]=> int(7) ["ARMORED"]=> int(9) ["VAN"]=> int(501) ["TOW"]=> int(502) ["COACH"]=> int(4) ["LIMO"]=> int(97) ["ARRESTS"]=> int(272) ["GTA"]=> int(67) ["BURGLAR"]=> int(122) ["HEIST"]=> int(1) ["PLANE"]=> int(48) ["HELI"]=> int(12) ["FAILED"]=> int(312) ["OVERLOADS"]=> int(160) ["TRUCK_LOADS"]=> int(1275) ["ODOMETER"]=> int(28320798) ["TIME"]=> int(2078450) } ["achievements"]=> array(4) { [0]=> string(20) "Professional Trucker" [1]=> string(13) "Gravel Hauler" [2]=> string(12) "Delivery Boy" [3]=> string(7) "Wrecker" } } }

3条回答
爷的心禁止访问
2楼-- · 2020-01-23 04:56

This is because $pjs is an one-element-array of objects, so first you should access the array element, which is an object and then access its attributes.

echo $pjs[0]->player_name;

Actually dump result that you pasted tells it very clearly.

查看更多
叛逆
3楼-- · 2020-01-23 04:59

@Balamanigandan your Original Post :- PHP Notice: Trying to get property of non-object error

Your are trying to access the Null Object. From AngularJS your are not passing any Objects instead you are passing the $_GET element. Try by using $_GET['uid'] instead of $objData->token

查看更多
Melony?
4楼-- · 2020-01-23 05:02

The response is an array.

var_dump($pjs[0]->{'player_name'});
查看更多
登录 后发表回答