在PHP

时间:2016-02-06 03:46:50

标签: php html json api rest

我正在尝试使用从REST API检索的数据打印包含监控组列表的表。结果以JSON的形式返回。以下是JSON数据:

{
    "code": 0,
    "message": "success",
    "data":
    [
        {
            "group_id": "169839000000116001",
            "display_name": "MTI Servers",
            "description": "",
            "monitors":
            [
            ]
        },
        {
            "group_id": "169839000000180001",
            "display_name": "PRB Servers",
            "description": "",
            "monitors":
            [
                "169839000000179003",
                "169839000000176013",
                "169839000000175003",
                "169839000000176007"
            ]
        },
        {
            "group_id": "169839000000046270",
            "display_name": "DB Servers",
            "description": "",
            "monitors":
            [
                "169839000000051011",
                "169839000000047023",
                "169839000000078001"
            ]
        },
        {
            "group_id": "169839000000025200",
            "display_name": "EXT Apps",
            "description": "External Monitoring of Applications",
            "monitors":
            [
                "169839000000025274",
                "169839000000025377",
                "169839000000025359",
                "169839000000025369",
                "169839000000025385",
                "169839000000025226"
            ]
        },
        {
            "group_id": "169839000000025109",
            "display_name": "EXT Services",
            "description": "External monitoring of services.",
            "monitors":
            [
                "169839000000046165",
                "169839000000025256",
                "169839000000025168",
                "169839000000025202",
                "169839000000025189",
                "169839000000025217",
                "169839000000025265"
            ]
        },
        {
            "group_id": "169839000000046015",
            "display_name": "ZMB Servers",
            "description": "",
            "monitors":
            [
                "169839000000050017",
                "169839000000050025",
                "169839000000049001",
                "169839000000050001",
                "169839000000053019",
                "169839000000051003",
                "169839000000050009"
            ]
        },
        {
            "group_id": "169839000000046282",
            "display_name": "NWK Devices",
            "description": "",
            "monitors":
            [
                "169839000000082009",
                "169839000000084077",
                "169839000000084001",
                "169839000000082229"
            ]
        },
        {
            "group_id": "169839000000046013",
            "display_name": "VBR Servers",
            "description": "",
            "monitors":
            [
                "169839000000047007"
            ]
        },
        {
            "group_id": "169839000000054197",
            "display_name": "LNX Servers",
            "description": "",
            "monitors":
            [
                "169839000000060483"
            ]
        },
        {
            "group_id": "169839000000046020",
            "display_name": "VSP Servers",
            "description": "",
            "monitors":
            [
                "169839000000060177",
                "169839000000060170",
                "169839000000060088",
                "169839000000060095",
                "169839000000060102",
                "169839000000060109",
                "169839000000054102"
            ]
        },
        {
            "group_id": "169839000000046058",
            "display_name": "WND Servers",
            "description": "",
            "monitors":
            [
                "169839000000066001",
                "169839000000063119"
            ]
        },
        {
            "group_id": "169839000000128001",
            "display_name": "TPT Servers",
            "description": "",
            "monitors":
            [
                "169839000000143041",
                "169839000000148017",
                "169839000000127035",
                "169839000000123003",
                "169839000000126011",
                "169839000000122011",
                "169839000000129001",
                "169839000000158028"
            ]
        }
    ]
}

我想打印组ID,名称,描述以及属于该组的监视器ID列表。我正在使用json_decode但我似乎无法从数组中读取正确的变量。

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

`<?php
/**
 * Connect to the Site API and extract the list of monitor groups
 */

 // URL to fetch
  $url = "https://www.mymonitorsite.com/api/monitor_groups";

 // Initialize cURL session
  $ch = curl_init($url);

 // Option to Return the Result, rather than just true/false
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 // Set Custom Headers
 $headers = array(
        'Authorization: Authtoken 12345678901234567890123456789012',
        'Content-Type: application/json;charset=UTF-8',
        'Accept: application/json; version=2.0',
 );

 // Option to set the custom headers
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

 // Perform the request, and save content to $result
  $mongrps_json = curl_exec($ch);

 // Close the cURL resource, and free up system resources!
  curl_close($ch);

 // Decode json data into a PHP Array
  $mongrps_array = json_decode($mongrps_json, true);   
?>
   <!DOCTYPE html>
   <html lang="en">
   <head>
   <meta charset="utf-8"/>
   <title>Monitor Group Test</title>
   </head>
   <body>
   <?php
      // List the first monitor group here (test)
      echo "Group ID   = " . $mongrps_array->data[0]->group_id . "<br>";
      echo "Group Name = " . $mongrps_array->data[0]->display_name . <br>";
  ?>
  </body>
  </html>
`

这是json_decode的var_dump:

array(3) { ["code"]=> int(0) ["message"]=> string(7) "success" ["data"]=> array(12) { [0]=> array(4) { ["group_id"]=> string(18) "169839000000116001" ["display_name"]=> string(11) "MTI Servers" ["description"]=> string(0) "" ["monitors"]=> array(0) { } } [1]=> array(4) { ["group_id"]=> string(18) "169839000000180001" ["display_name"]=> string(11) "PRB Servers" ["description"]=> string(0) "" ["monitors"]=> array(4) { [0]=> string(18) "169839000000179003" [1]=> string(18) "169839000000176013" [2]=> string(18) "169839000000175003" [3]=> string(18) "169839000000176007" } } [2]=> array(4) { ["group_id"]=> string(18) "169839000000046270" ["display_name"]=> string(10) "DB Servers" ["description"]=> string(0) "" ["monitors"]=> array(3) { [0]=> string(18) "169839000000051011" [1]=> string(18) "169839000000047023" [2]=> string(18) "169839000000078001" } } [3]=> array(4) { ["group_id"]=> string(18) "169839000000025200" ["display_name"]=> string(8) "EXT Apps" ["description"]=> string(35) "External Monitoring of Applications" ["monitors"]=> array(6) { [0]=> string(18) "169839000000025274" [1]=> string(18) "169839000000025377" [2]=> string(18) "169839000000025359" [3]=> string(18) "169839000000025369" [4]=> string(18) "169839000000025385" [5]=> string(18) "169839000000025226" } } [4]=> array(4) { ["group_id"]=> string(18) "169839000000025109" ["display_name"]=> string(12) "EXT Services" ["description"]=> string(31) "External monitoring of services" ["monitors"]=> array(7) { [0]=> string(18) "169839000000046165" [1]=> string(18) "169839000000025256" [2]=> string(18) "169839000000025168" [3]=> string(18) "169839000000025202" [4]=> string(18) "169839000000025189" [5]=> string(18) "169839000000025217" [6]=> string(18) "169839000000025265" } } [5]=> array(4) { ["group_id"]=> string(18) "169839000000046015" ["display_name"]=> string(11) "ZMB Servers" ["description"]=> string(0) "" ["monitors"]=> array(7) { [0]=> string(18) "169839000000050017" [1]=> string(18) "169839000000050025" [2]=> string(18) "169839000000049001" [3]=> string(18) "169839000000050001" [4]=> string(18) "169839000000053019" [5]=> string(18) "169839000000051003" [6]=> string(18) "169839000000050009" } } [6]=> array(4) { ["group_id"]=> string(18) "169839000000046282" ["display_name"]=> string(11) "NWK Devices" ["description"]=> string(0) "" ["monitors"]=> array(4) { [0]=> string(18) "169839000000082009" [1]=> string(18) "169839000000084077" [2]=> string(18) "169839000000084001" [3]=> string(18) "169839000000082229" } } [7]=> array(4) { ["group_id"]=> string(18) "169839000000046013" ["display_name"]=> string(11) "VBR Servers" ["description"]=> string(0) "" ["monitors"]=> array(1) { [0]=> string(18) "169839000000047007" } } [8]=> array(4) { ["group_id"]=> string(18) "169839000000054197" ["display_name"]=> string(11) "LNX Servers" ["description"]=> string(0) "" ["monitors"]=> array(1) { [0]=> string(18) "169839000000060483" } } [9]=> array(4) { ["group_id"]=> string(18) "169839000000046020" ["display_name"]=> string(11) "VSP Servers" ["description"]=> string(0) "" ["monitors"]=> array(7) { [0]=> string(18) "169839000000060177" [1]=> string(18) "169839000000060170" [2]=> string(18) "169839000000060088" [3]=> string(18) "169839000000060095" [4]=> string(18) "169839000000060102" [5]=> string(18) "169839000000060109" [6]=> string(18) "169839000000054102" } } [10]=> array(4) { ["group_id"]=> string(18) "169839000000046058" ["display_name"]=> string(11) "WND Servers" ["description"]=> string(0) "" ["monitors"]=> array(2) { [0]=> string(18) "169839000000066001" [1]=> string(18) "169839000000063119" } } [11]=> array(4) { ["group_id"]=> string(18) "169839000000128001" ["display_name"]=> string(11) "TPT Servers" ["description"]=> string(0) "" ["monitors"]=> array(8) { [0]=> string(18) "169839000000143041" [1]=> string(18) "169839000000148017" [2]=> string(18) "169839000000127035" [3]=> string(18) "169839000000123003" [4]=> string(18) "169839000000126011" [5]=> string(18) "169839000000122011" [6]=> string(18) "169839000000129001" [7]=> string(18) "169839000000158028" } } } }

2 个答案:

答案 0 :(得分:0)

我刚试过,不应该有任何问题来获取您正在寻找的数据。

例如,要获取数组中第一项的group_id:

echo $decodedJson->data[0]->group_id;

也许您正在尝试将数据作为数组获取?在这种情况下,将参数true传递给json_decode函数:

$decodedJson = json_decode($originalJson, true);
echo $decodedJson['data'][0]['group_id'];

答案 1 :(得分:0)

使用foreach循环显示以下数据:

  • GROUP_ID
  • DISPLAY_NAME
  • 说明和
  • 监视器(以逗号分隔的列表)

    $mongrps_array = json_decode($mongrps_json, true);
    
    foreach($mongrps_array['data'] as $arr){
        foreach($arr as $key => $value){
            if($key == "monitors"){
                echo $key . ": " . implode(", ", $value) . "<br />";
            }else{
                echo $key . ": " . $value . "<br />";
            }
        }
        echo "<br />";
    }