I'm messing with the teamsnap API which uses Collection + JSON which seems to be making it a bit more difficult to extract specific data (at least for me).
What I need to do is get each member id so I can loop through those to then get a JSON response for each member's info. So far I am working on the first step.
Here is my code so far:
<?php
$url = "https://api.teamsnap.com/v3/members/search?team_id=TEAMID";
$access_key = 'TOKEN';
$request = curl_init( $url );
curl_setopt( $request, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $access_key ) );
$response = (string)curl_exec( $request );
curl_close($request);
$result = json_decode($response, true);
foreach ($result as $r) {
echo $r['id'];
}
?>
UPDATE: I used print_r without json_decode and went line by line through thousands of lines of resposne to see how exactly the information I actually want is coming back. This is a bit the relevant portion. Sorry to have led you on a wild goose chase, but the response is quite complicated when it comes back and half of it is examples of how data COULD be returned....
{
"collection":{
"items":[
{
"href":"https://api.teamsnap.com/v3/members/MEMBERID",
"data":[
"name":"id",
"value":MEMBERID
},
{
"name":"type",
"value":"member"
},
{
"name":"address_city",
"value":""
},
{
"name":"address_state",
"value":""
},
{
"name":"address_street1",
"value":""
},
{
"name":"address_street2",
"value":null
},
{
"name":"address_zip",
"value":""
},
Assuming the data you provided is from $response
. The data structure does not provide an easy way to parse the data. In order to get to the 'name':'id'
value pair. Do this:
$result = json_decode($response);
foreach ($result->collection->items as $items=>$val) {
foreach ($val->data as $data=>$datasets) {
foreach ($datasets as $dataset=>$val) {
echo $dataset.': '.$val;
echo "<br>";
}
}
};
It will return the results as:
name: id
value: 3745306
name: type
value: user
Based on your edited data structure, the answer that I provided on my previous post no longer work. Here is the one based on your revised data structure:
$result=json_decode($response);
foreach($result->collection->template->data as $dataset) {
foreach($dataset as $key=>$val) {
echo $key.": ".$val;
echo "<br>";
}
}