从操作中返回xml数据 - CakePHP

时间:2012-08-23 18:52:27

标签: xml file cakephp get

我正在尝试返回从xml文件中检索的一些数据,然后将其返回到视图页面。

xml文件将在用户启动的某个操作期间更新,然后我会弹出一个对话框并显示xml文件的内容。在此操作期间将更新xml文件。

在我看来,我想显示从读取xml文件的php动作返回的数据。

以下是获取更新的xml文件的操作。这将每秒调用一次以更新视图。

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

检索xml文件的内容并将其存储到$data变量中,但我一直收到500内部服务器错误。我尝试了很多不同的东西,但我不知道如何正确地返回数据。

以下是我显示信息的视图代码。

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

我无法弄清楚我做错了什么。如果我可以将数据变量返回到get请求,那么我可以解析并显示信息,但是我无法将数据从php获取到javascript get请求。 任何帮助都会很棒。

由于

1 个答案:

答案 0 :(得分:1)

我认为你的行动就是这样:

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

}