Our monitoring software supports a stateless API that with the correct parameters returns either XML or JSON output to the browser. When trying to pull this information, I have found that file_get_contents, file, and any other approaches always fail with "failed to open stream: No such file or directory".
I have made sure that "allow_url_fopen" is enabled, tried the URL with and without encoding, using file_get_contents with and without stream context, and just to simplify it, tried pulling the URL contents for just the base URL without any parameters.
I am at a complete loss as to why nothing works to pull the contents from this URL. The URL works fine in the browser but seems inaccessible from PHP, even on different web-servers.
My full code... <?php
$protocol = "http";
$prtg_url = "prtg.domain.net:8080/";
$prtg_user = "username";
$prtg_hash = "passhash";
function getSensorData($deviceid)
{
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Content-Type: text/xml\r\n",
'timeout' => 60
)
);
$context = stream_context_create($opts);
$sensor_xml_url = $GLOBALS['protocol'] . "://". $GLOBALS['prtg_url'] .
"api/table.json?content=sensors&output=json&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");
?>
This also fails...
$results = file_get_contents(rawurlencode("http://prtg.domain.net:8080"));
echo $results;
Why are you rawurlencoding that url? In doing so you're trying to get the contents from "http%3A%2F%2Fprtg.fragnet.net%3A8080"
Why not just: rawurlencode the actual parameters and leave the domain/subdomain/protocol/port alone etc.
$results = file_get_contents("http://prtg.fragnet.net:8080"); ?