0

I'm looking to get some input on how to make modular code that performs a PHP parse_ini_file and then uses the returned values to run JSON decodes.

I have a BACnet API that returns a JSON structure for BACnet points in an automation system. I wrote the following code to decode the JSON data to return just the "present-value" field and then I display the value on a webpage.

<?php 
$url = "http://hostname.lcl:47800/api/v1/bacnet/devices/10100/objects/0.0";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
echo "<b>Room temperature</b>: ". $json_data["present-value"]. " DEG F";
;?>

This works well but I want to make this code modular so it can be used for many other points.

I created an INI file with a list of other points and the URL that contains the JSON data from the API.

## BACnet Configuration File
# BACnet Object URLs from WACNET Browser API
[bacnet]
SEA_RMT                     = http://hostname.lcl:47800/api/v1/bacnet/devices/10100/objects/0.0
SEA_SRV_SEA_SV1_01_EXHT     = http://hostname.lcl:47800/api/v1/bacnet/devices/10100/objects/0.3
SEA_SRV_SEA_SV1_02_EXHT     = http://hostname.lcl:47800/api/v1/bacnet/devices/10100/objects/0.4
SEA_SRV_SEA_SV1_03_EXHT     = http://hostname.lcl:47800/api/v1/bacnet/devices/10100/objects/0.5

What I'd like to do is use the INI file to get the present value of each point in the list and then create a variable that is the name of the point and set it equal to the "present-value" field. Then I can reference the point using the PHP variable on the HTML page like this:

<?php echo "$SEA_SRV_SEA_SV1_01_EXHT";?>

I started with the code below but it doesn't work.

<?php

// Parse the settings file
$bacnetini = parse_ini_file('/var/www/config/bacnet.ini');

// Parse the keys to variables and add data
foreach ($bacnetini as $key => $value) {
    $url = $value;
    $json = file_get_contents($url);
    $json_data = json_decode($json, true);
    $$key = $json_data; 
}

?>

I'd love to get some other opinions on the best way to accomplish this since I don't really know where to go from here.

I've looked through these other Stack Overflow questions but I don't know how to get the pieces to all fit together.

3
  • Using variable variables like $$key is rarely a good idea, because you have no idea what they might be called without looping through the keys of $bacnetini again.... why not simply build an array? Commented Dec 24, 2016 at 19:16
  • Thanks Mark. How could I use an array instead? Can you elaborate more? Commented Dec 24, 2016 at 19:36
  • Define the array as an empty array before your loop ($bacnetData = [];); populate that array using $bacnetData[$key] = $json_data; instead of $$key = $json_data;; and display using <?php echo $bacnetData['SEA_SRV_SEA_SV1_01_EXHT']";?> Commented Dec 24, 2016 at 19:58

1 Answer 1

0

Why not try something like this instead? This will allow you to create other sections in your INI file that won't affect your script.

<?php

$bacnetini = parse_ini_file('/var/www/config/bacnet.ini', true);

$data = array();

foreach ($bacnetini['bacnet'] as $key => $url) {
    $data[$key] = json_decode(file_get_contents($url), true);
}

var_dump($data['SEA_SRV_SEA_SV1_01_EXHT']);

?>
Sign up to request clarification or add additional context in comments.

3 Comments

This works but it returns all the JSON data from the API.
Is there a way to just return the "present-value" field from the JSON data?
Got it, I just added the "present-value" index to the dump like this: var_dump($data['SEA_SRV_SEA_SV1_01_EXHT']['present-value']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.