dongxian6715 2015-12-15 21:46
浏览 80
已采纳

PHP中带有JSON对象的输出问题(可能是数组?)

I'm having troubles getting a JSON object (possibly an array?) to output using PHP. My question is: How do I get only a displayName from the following (shortened) JSON object using PHP?

{"Response":
  {"results":
    [{"user":
      {"membershipId":"6343960","displayName":"J Raider"},"hasPendingApplication":false},
     {"user":
      {"membershipId":"4479502","displayName":"T Ellis"},"hasPendingApplication":false}]
  }
}

What I've tried so far:

var_dump($json); 
echo $json; 
echo $json->Response->results->user->displayName;
echo $json->Response->results[0]->user->displayName; 
echo $json->Response->results[0]->user[0]->displayName; 
echo json_encode($json, JSON_PRETTY_PRINT);
print_r($json)

My results have either been "null" "NULL" or simply no output. I am pulling from the Bungie API using the following code:

<?php 
    $apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.bungie.net/Platform/Group/1179713/Members/?lc=en&fmt=true&currentPage=1&platformType=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-Key: ' . $apiKey));

$json = json_decode(curl_exec($ch)); // edited to correct curl_init to  curl_exec

var_dump($json);

?>

I am aiming to do this as a web service using PHP. The Bungie API documentation says a JavaScript GET request will not work, so I'd like to stick with PHP for the request and the output.

To clarify my question: how do I use PHP to output the displayName from this JSON object?

Thanks in advance for your help and please let me know if I need to clarify anything.

  • 写回答

2条回答 默认 最新

  • dongyi6845 2015-12-15 22:38
    关注

    Im pretty sure this is failing because you arent executing your curl post:

    $json = json_decode(curl_init($ch));

    should be

    $json = curl_exec($ch);
    $json = json_decode($json,true);
    var_dump($json); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题