我搞乱了使用Collection + JSON的teamsnap API,这似乎使得提取特定数据变得更加困难(至少对我而言)。
我需要做的是获取每个成员ID,以便我可以遍历这些,然后为每个成员的信息获取JSON响应。到目前为止,我正在迈出第一步。
到目前为止,这是我的代码:
<?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:我使用了print_r而没有json_decode,并逐行通过数千行resposne来查看我真正想要的信息是如何回来的。这有点相关部分。很抱歉引导你进行疯狂追逐,但回复时反应相当复杂,其中一半是数据可能返回的例子....
{
"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":""
},
答案 0 :(得分:1)
假设您提供的数据来自$response
。数据结构不提供解析数据的简便方法。为了获得'name':'id'
值对。这样做:
$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>";
}
}
};
它会将结果返回为:
name: id
value: 3745306
name: type
value: user
答案 1 :(得分:0)
根据您编辑的数据结构,我在上一篇文章中提供的答案不再有效。这是基于您修订的数据结构的那个:
$result=json_decode($response);
foreach($result->collection->template->data as $dataset) {
foreach($dataset as $key=>$val) {
echo $key.": ".$val;
echo "<br>";
}
}