Zabbix Reading the Api

时间:2015-11-12 10:42:02

标签: php arrays json api zabbix

I'm getting Informations from the Zabbix Api with a PHP library. At the moment I get the "lastvalue" from the json Array:

try {
    // connect to Zabbix-API
    $api = new ZabbixApi($api_url, $username, $password);


 $params = array( 

                           'groupids' => '2',
                           'real_items'        =>TRUE,                    
                            'monitored_items'   =>TRUE, 
                            'search' => array('name' => 'Disk root used p'),                                                                    
                            'selectFunctions'   => 'extend',
                            'output'            => 'extend', 
                            'sortfield'         => 'name',
                            'limit'             => '1'

                            );



    $trends = $api->itemGet($params); // get data from api

  $names = array();
    foreach($trends as $trend)  {       // loop through the returned data
      $names[] = $trend->lastvalue;

    }
//print_r($names);

} catch(Exception $e) {

    // Exception in ZabbixApi catched
    echo $e->getMessage();
}

But now I want to get the "lastvalue" plus the "name" of the Item in this Array for example like that: "name"+"lastvalue". How can I get both of them into my array $names[]?

1 个答案:

答案 0 :(得分:1)

Here is my answer from my comments, hope its what you are looking for!

$trends = $api->itemGet($params);
$names = array();
foreach($trends as $trendKey => $trendValue)
{
    $names[$trendKey] = $trendValue;
}

#Test the names array
foreach ($names as $nameKey => $nameValue)
{
    print("{$nameKey} = {$nameValue}<br />");
}

Return value:

groupids = 2
real_items = TRUE
...
sortfield = name
limit = 1
相关问题