请问这样的API返回结果我使用PHP该怎么获得其中的数据?

新手上路,请多包涵

因为我需要获取这个API的返回结果,写入数据库中.但是因为我是小白.不会取josn中需要的值,请各位大佬帮忙!
接口地址:https://api.oioweb.cn/api/ran...
返回结果:1. {

  1. "code": 1,
  2. "msg": "查询成功!",
  3. "data": {
  4. "360": {
  5. "img": "https://statics.aizhan.com/images/360/10.png",
  6. "weight": "10"
  7. },
  8. "br": {
  9. "img": "https://statics.aizhan.com/images/br/10.png",
  10. "weight": "10"
  11. },
  12. "mbr": {
  13. "img": "https://statics.aizhan.com/images/mbr/10.png",
  14. "weight": "10"
  15. },
  16. "sr": {
  17. "img": "https://statics.aizhan.com/images/sr/10.png",
  18. "weight": "10"
  19. },
  20. "msr": {
  21. "img": "https://statics.aizhan.com/images/msr/10.png",
  22. "weight": "10"
  23. },
  24. "m360": {
  25. "img": "https://statics.aizhan.com/images/m360/10.png",
  26. "weight": "10"
  27. },
  28. "sm": {
  29. "img": "https://statics.aizhan.com/images/sm/8.png",
  30. "weight": "8"
  31. }
  32. }
  33. }

我需要新建一个PHP文件获取其中的360、br、mbr、sr、msr、m360、sm中的weight显示的值。
这样该怎么写。谢谢!

阅读 2.3k
2 个回答
<?php
$url = '这里填写你的url';
// 注意. 建议你用curl来请求. 我这里只是为了图快.
$response_json = file_get_contents($url);
$response = @json_decode($response_json, true);

if(!$response || $response['code'] != 1) {
    exit(0);
} 

$channels = [];

foreach($response['data'] as $channel => $info) {
    $channels[$channel] = $info['weight'];
}

var_dump($channels);
var_dump($channels['mbr']);

未经过任何测试的代码,具体逻辑就是这个样子.

用PHP cURL去请求API,然后拿到返回结果按照楼上说的处理

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题