Reputation: 1233
I am trying to return some data retrieved from an xml file and then return it to a view page.
The xml file will be updated during a certain action a user will start, and then i have a dialog box that pops up and displays the contents of the xml file. The xml file will be updated during this action.
In my view I want to display the data returned from the php action that reads the xml file.
Here is my action to get the updated xml file. This will be called every second to update the view.
public function getUpdate()
{
$myFile = '/srv/www/xml-status/update_status.xml';
$file = fopen($myFile, 'r');
$data = file_get_contents($myFile);
//debug($data);
//print_r($data);
//echo json_encode($data);
//echo $data;
return json_encode($data);
}
The contents of the xml file are retrieved and stored into the $data
variable, but I keep getting a 500 internal server error. I tried a bunch of different things, but I don't know how to return the data correctly.
Here is my view code to display the info.
<script>
window.onload = timedRefresh(1000);
// Get the updated xml file every second
function timedRefresh(timeoutPeriod)
{
// Do a get request to get the updated contents of the xml file
$.get('/tools/getUpdate', function(data)
{
alert(data);
});
window.setTimeout('timeRefreshed(1000)', 1000);
}
</script>
<?php
if(false != $xml = simplexml_load_file($xmlFile))
{
foreach ($xml->LogEvent->sequence as $temp)
{
echo 'Step: ', $temp['step'], ' ', 'Step Name: ', $temp['stepName'], ' ', 'd1: ', $temp['d1'], ' ',
'd2: ', $temp['d2'], ' ', 'd3: ', $temp['d3'], ' ', 'Status: ', $temp['status'],'<br>';
}
}
else
{
echo '<br>Error: Update status can\'t be displayed<br>';
}
?>
I can't figure out what I am doing wrong. If I can get the data variable to be returned to the get request I can then parse through and display the info, but I am having trouble getting the data from php to the javascript get request. Any help would be great.
Thanks
Upvotes: 0
Views: 929
Reputation: 2025
I think your action would be like that :
public function getUpdate()
{
$myFile = '/srv/www/xml-status/update_status.xml';
$file = fopen($myFile, 'r');
$data = file_get_contents($myFile);
//debug($data);
//print_r($data);
echo json_encode($data);
exit;
}
OR
public function getUpdate()
{
$this->autoRender = false;
$myFile = '/srv/www/xml-status/update_status.xml';
$file = fopen($myFile, 'r');
$data = file_get_contents($myFile);
//debug($data);
//print_r($data);
echo json_encode($data);
}
Upvotes: 1