When accessing our PRTG installation API directly via URL in my browser, I receive the contents without any problems at all, whether it be charts, sensor data, etc. However, despite hours of testing and try every method from file() to file_get_contents() to cURL, it always results in a "Failed to open Stream".
I have posted my code on several forums, worked with numerous people testing different approaches (url encoding, headers, etc) and absolutely cannot get the contents from the API as a PHP response.
What in the world could the problem possibly be?
<?php $protocol = "http"; $prtg_url = "prtg.domain.net:8080/"; $prtg_user = "username"; $prtg_hash = "xxxxxxxxxx"; function getSensorData($deviceid) { $sensor_xml_url = $GLOBALS['protocol'] . "://". $GLOBALS['prtg_url'] . "api/table.json?content=sensors&output=xml&columns=objid,type,device,sensor,status&id=" . $deviceid . "&username=" . $GLOBALS['prtg_user'] . "&passhash=" . $GLOBALS['prtg_hash']; $xml_url_encoded = rawurlencode($sensor_xml_url); if ($response_xml_data = file_get_contents($xml_url_encoded) == false) { echo "Error fetching XML\n"; } else { $sensors = simplexml_load_string($response_xml_data); foreach ($sensors->item as $sensor) { $sensor_ping = $sensor->ping; $sensor_id = $sensor->objid; $sensor_type = $sensor->type; $sensor_typeraw = $sensor->type_raw; echo $sensor_ping . "</br>"; echo $sensor_id . "</br>"; echo $sensor_type . "</br>"; echo $sensor_typeraw . "</br>"; } } } getSensorData("3401"); ?>
Add comment