What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

PRTG API is inaccessible from PHP Script

Votes:

0

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");

?>

api php prtg

Created on Sep 4, 2013 6:20:02 AM

Last change on Sep 5, 2013 1:03:14 PM by  Konstantin Wolff [Paessler Support]



3 Replies

Votes:

0

Hi,
the API call you are using is not producing any content on the target page at all. It just creates a downloadable XML file. The only way to get website content from the API is to use the JSON format as output. Then you should be able to parse the output.
Best regards

Created on Sep 5, 2013 1:02:57 PM by  Konstantin Wolff [Paessler Support]



Votes:

0

Thank you for the response. That call actually is producing XML formatted content on the page when I access it via my browser. When using table.xml it tried to download the file, but when I changed it to "table.json" and kept the output as XML it properly outputs the XML formatted content to my browser as shown below. This is why there has been confusion over why it results in a Failed to Open Stream no matter what method is used.

<?xml version="1.0" encoding="UTF-8"?>
  <sensors>
   <prtg-version>13.3.5.2685</prtg-version>
   <item>
    <objid>3402</objid>
    <type>Ping</type>
    <type_raw>ping</type_raw>
    <device>US-CHI-012</device>
    <sensor>PING 40</sensor>
    <status>Up </status>
    <status_raw>3</status_raw>
   </item>
   <item>
    <objid>3416</objid>
    <type>WMI Free Disk Space (Multi Disk)</type>
    <type_raw>wmidiskspace</type_raw>
    <device>US-CHI-012</device>
    <sensor>Disk Free 1 1 24</sensor>
    <status>Up </status>
    <status_raw>3</status_raw>
   </item>
   <item>
    <objid>3417</objid>
    <type>Windows Network Card</type>
    <type_raw>wminetwork</type_raw>
    <device>US-CHI-012</device>
    <sensor>Intel[R] 82574L Gigabit Network Connection</sensor>
    <status>Unusual </status>
    <status_raw>10</status_raw>
   </item>
   <item>

Created on Sep 5, 2013 1:09:57 PM

Last change on Sep 9, 2013 8:46:27 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

Hi,
first, with the API call only groups or devices can be accessed. When providing a sensor id the script will fail. Additional, I had a deeper look at your script and it seems that PRTG simply does not like the URL encoding of the URL itself. The following example should work:

<?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'];

        //This line will print the whole output of the PRTG API
        echo file_get_contents($sensor_xml_url);
        if ($response_xml_data = file_get_contents($sensor_xml_url) == 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("50");

?>

In this form the script does not throw any error but you might have a look at the part of the function which parsing the XML as well as no output is produced.
Best regards

Created on Sep 9, 2013 8:54:52 AM by  Konstantin Wolff [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.