dongqi9125
2017-03-11 05: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":""
},

图片转代码服务由CSDN问答提供 功能建议

我正在搞乱使用Collection + JSON的teamsnap API,这似乎让它变得有点困难 提取特定数据(至少对我而言)。

我需要做的是获取每个成员ID,以便我可以遍历这些,然后为每个成员的信息获取JSON响应。 到目前为止,我正在开始第一步。

到目前为止,这是我的代码:

 &lt;?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($结果为$ r){
echo $ r ['id']; 
} 
?&gt; 
   
 
 

更新: 我在没有json_decode的情况下使用了print_r,并逐行通过数千行resposne来查看我真正想要的信息究竟是如何回来的。 这有点相关部分。 很抱歉引导你进行疯狂追逐,但回复时反应相当复杂,其中一半是数据可能返回的示例....

  {
“collection”:{
“items”:[
 {
“href”:“https://api.teamsnap.com/v3/members/MEMBERID”,
“data”:[  
 “个名字”: “ID”,
 “个值”:MEMBERID \ N},\ N {
 “个名字”: “类型”,
 “个值”: “构件” \ N},\ N {  
 “个名字”: “ADDRESS_CITY” 
 “个值”: “” \ N},\ N {
 “个名字”: “ADDRESS_STATE” 
 “个值”: “” \ N},\ N {  
 “个名字”: “address_street1” 
 “个值”: “” \ N},\ N {
 “个名字”: “address_street2” 
 “个值”:空\ N},\ N {\  n“name”:“address_zip”,
“value”:“”
},
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dpafea04148 2017-03-11 12: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条)

相关推荐 更多相似问题