dongqi9125 2017-03-10 21:44
浏览 56
已采纳

PHP Curl,Collection + JSON特定字段的响应

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":""
},

展开全部

  • 写回答

2条回答 默认 最新

  • dpafea04148 2017-03-11 04:22
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部