timmah1 Posted July 5, 2015 Share Posted July 5, 2015 I'm trying to show information from an API with PHP. I can get the info, but how do I break the results down for display purposes? $json_url = "http://socialclub.rockstargames.com/crewapi/crew_name/hierarchy/100/false"; $json = file_get_contents($json_url); $json=str_replace('}, ]',"} ]",$json); $data = json_decode($json); echo "<pre>"; print_r($data); echo "</pre>"; All the data is shown like this stdClass Object ( [data] => stdClass Object ( [hierarchy] => stdClass Object ( [Data] => stdClass Object ( [Total] => 35 [CrewId] => 0159 [CrewName] => Crew Name [CrewTag] => Dead [CrewColor] => #03f2ff [IsLeader] => [MyRank] => -1 [Ranks] => Array ( [0] => stdClass Object ( [RankName] => leader [RankNamePlural] => leader [Description] => The Crew founder and head honcho. Their power is absolute. [RankOrder] => 0 [RankMemberCount] => 1 [SystemFlags] => 9223372036854775807 [Permissions] => Array ( [0] => Kick members [1] => Invite new members [2] => Promote members [3] => Demote members [4] => Manage role permissions [5] => Publish multiple emblems [6] => Write on wall [7] => Delete emblems and wall posts [8] => Edit settings [9] => View settings page [10] => Post in-game messages [11] => Update status ) Thanks in advacne Link to comment https://forums.phpfreaks.com/topic/297186-api-help/ Share on other sites More sharing options...
scootstah Posted July 5, 2015 Share Posted July 5, 2015 You just call the properties like any other PHP object. echo $data->Total; echo $data->CrewId;If you pass true as the second argument to json_decode(), you will get an associative array instead. $data = json_decode($json, true); echo $data['Total']; echo $data['CrewId']; Link to comment https://forums.phpfreaks.com/topic/297186-api-help/#findComment-1515621 Share on other sites More sharing options...
timmah1 Posted July 5, 2015 Author Share Posted July 5, 2015 Thank you. But neither is showing up, I get a blank page Link to comment https://forums.phpfreaks.com/topic/297186-api-help/#findComment-1515624 Share on other sites More sharing options...
Ch0cu3r Posted July 5, 2015 Share Posted July 5, 2015 Try using $data->data->hierarchy->Data->Total $data->data->hierarchy->Data->CrewId etc Link to comment https://forums.phpfreaks.com/topic/297186-api-help/#findComment-1515629 Share on other sites More sharing options...
timmah1 Posted July 5, 2015 Author Share Posted July 5, 2015 Thank You!!! Works perfect!! Link to comment https://forums.phpfreaks.com/topic/297186-api-help/#findComment-1515630 Share on other sites More sharing options...
timmah1 Posted July 5, 2015 Author Share Posted July 5, 2015 Sorry to bring this topic back up, but I'm stuck once again To loop through members I have this but it's obviously not right because I keep getting invalid argument Plus, it's only showing 1 for the crew members, $i = 0; $crew_comm = $data->data->hierarchy->Data->Ranks[1]->Members[$i]->Name; foreach($crew_comm as $result){ $i++; echo($result->Name); } Link to comment https://forums.phpfreaks.com/topic/297186-api-help/#findComment-1515635 Share on other sites More sharing options...
scootstah Posted July 5, 2015 Share Posted July 5, 2015 1. The "rank" that you're using does not have any "members". 2. You're looping over a scalar in foreach(), not an array. Your foreach() should look like this (notice I changed the "ranks" index to 0, as that is the only one that has members in the data URL you provided): foreach ($data->data->hierarchy->Data->Ranks[0]->Members as $member) { echo $member->Name; echo '<br />'; }3. There is only one "member" for the 0th "rank" index. But, there are many ranks, and it looks like each one may have a member, so you might want to adjust your logic to get those as well. Link to comment https://forums.phpfreaks.com/topic/297186-api-help/#findComment-1515653 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.