php - read and update XML value for matching value



I am trying to update an XML file if a value passed matches the parent value. Basically once a minute a series of IP Addresses are pinged and the result is passed to my php script like this: http://ipaddress/presence.php?ip=192.168.0.55$present=1.


XML (presence.xml):



<?xml version="1.0" encoding="UTF-8"?>
<Presence>
<Person IP="192.168.0.55">
<Name>Bob</Name>
<Present>1</Present>
<LastSeen>14/09/2014 01:48:43 pm</LastSeen>
</Person>
<Person IP="192.168.0.56">
<Name>John</Name>
<Present>0</Present>
<LastSeen>13/09/2014 08:32:19 pm</LastSeen>
</Person>
</Presence>


PHP (presence.php):



<?php

$ip = $_REQUEST['ip'];
$present = $_REQUEST['present'];

// Write latest data to XML file
$Presence = simplexml_load_file('presence.xml');
foreach ($Presence->xpath('//Person') as $Person) {
if ($Person[@IP] == $ip) {
$Person->Present = $present;
}
else {}

}

?>


The code above I had hoped would update the <Present> tag with the value passed in the URL. Yet nothing happens, I don't get any errors in the log file. I did have the $present value inside the IF statement written in the wrong case initially which did throw an error regarding undefined variable, which indicated the code worked as far as the IF statement.


Any help appreciated, I have looked at various Q & As on Stackoverflow which is how I got this far, but can't figure this out. Once I have cleared this hurdle I will hopefully be able to add the code to update the <LastSeen> tag in the XML.


No comments:

Post a Comment