Jump to content

API help


Go to solution Solved by Ch0cu3r,

Recommended Posts

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

Edited by timmah1
Link to comment
https://forums.phpfreaks.com/topic/297186-api-help/
Share on other sites

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

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);
}
Edited by timmah1
Link to comment
https://forums.phpfreaks.com/topic/297186-api-help/#findComment-1515635
Share on other sites

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. Edited by scootstah
Link to comment
https://forums.phpfreaks.com/topic/297186-api-help/#findComment-1515653
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.