我正在将来自我们的监控软件(PRTG)的API集成到我们的网站中,并尝试使用生成XML格式的数据列表的函数。在根据需要生成时,URL不会指向现有文件。
我尝试使用“simplexml_load_file”和“simplexml_load_string”并传递URL而没有运气。我也尝试使用“file_put_contents”来首先保存文件,但它也失败了,因为URL实际上并没有指向文件。
如何才能使其发挥作用?
<?php
$prtg_url = "http://prtg.domain.net:8080/";
$prtg_user = "username";
$prtg_hash = "passwordhash";
function getSensorData($deviceid)
{
$sensor_xml_file = $GLOBALS['prtg_url'] . "api/table.xml?content=sensors&output=xml&columns=objid,type,device,sensor,status&id=" . $deviceid . "&username=" . $GLOBALS['prtg_user'] . "&passhash=" . $GLOBALS['prtg_hash'];
file_put_contents("sensor.xml", fopen($sensor_xml_file, 'r'));
$sensors = simplexml_load_file("sensor.xml");
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");
?>
答案 0 :(得分:0)
它可能与文件处理程序有关。您可以尝试将simplexml_load_file()与您的URL一起使用。例如:
$url = $GLOBALS['prtg_url']
. "api/table.xml?content=sensors&output=xml&columns=objid,type,device,sensor,status&id="
. $deviceid . "&username=" . $GLOBALS['prtg_user']
. "&passhash=" . $GLOBALS['prtg_hash']);
$xml = simplexml_load_file($url);
答案 1 :(得分:0)
试试这个:
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $sensor_xml_file);
$xmlString = curl_exec($c);
curl_close($c);
$sensors = simplexml_load_string($xmlString);
答案 2 :(得分:0)
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Content-Type: text/xml\r\n",
'timeout' => 60
)
);
$context = stream_context_create($opts);
$url = $GLOBALS['prtg_url']
. "api/table.xml?content=sensors&output=xml&columns=objid,type,device,sensor,status&id="
. $deviceid . "&username=" . $GLOBALS['prtg_user']
. "&passhash=" . $GLOBALS['prtg_hash']);
$result = file_get_contents($url, false, $context);
echo $results;
这会强制操作超时60秒,这样您就可以看到它是否实际获取了任何结果。如果确实返回了不完整的结果,请切换到使用XMLReader作为解析器。