我正试图从中获取数据:
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
但如果我想用这段代码获取player_name变量:
<?
$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'});
?>
我收到错误:
注意:尝试在** \ htdocs \ index.php中获取非对象的属性 第9行+ var_dump()返回:NULL
var_dump($pjs)
返回:
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" } } }
答案 0 :(得分:37)
这是因为$pjs
是一个单元素的对象数组,所以首先你应该访问数组元素,这是一个对象,然后访问它的属性。
echo $pjs[0]->player_name;
实际上你粘贴的转储结果非常清楚。
答案 1 :(得分:1)
响应是一个数组。
var_dump($pjs[0]->{'player_name'});
答案 2 :(得分:0)
@Balamanigandan您的原始帖子: - PHP Notice: Trying to get property of non-object error
您正在尝试访问Null对象。从AngularJS你没有传递任何对象,而是传递$ _GET元素。请尝试使用$_GET['uid']
代替$objData->token