alain mardo alain mardo - 4 months ago 8
Ajax Question

Parse XML from RSS with PHP and pass using AJAX

I would like to get the most updated ITEM in the rss link (Reuters: Top News) and When a new ITEM will come the PHP/AJAX will replace the old ITEM to a new one.

I tried to do that with PHP and AJAX but I get a blank page or "Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\test4.php on line 11".

The function:

<?php function parserSide($feedURL) { $rss = simplexml_load_file($feedURL); $output = "
<ul class='newsSide'>"; $i = 0; foreach ($rss->channel->item as $feedItem) { $i++; $output .= "
<li><a href='$feedItem->link' title='$feedItem->title'>" . $feedItem->title . "</a></li>"; if($i >= 1) break; } $output .= "</ul>"; return $output; } ?>
<?php
if(isset($_GET['fetchOnly'])){
$url = 'http://feeds.reuters.com/reuters/topNews';

$response = array();
$handle = fopen($url, 'r');
if ($handle) {
while (($data = simplexml_load_file($url)) !== FALSE)
{
$response = parserSide($url);
}
fclose($handle);
}
echo $response;
die();
}
?>
<div id="responseText"></div>
<script>
// run the function, it will re-run itself
fetchRate();

function fetchRate() {
// create the new AJAX Object
xmlhttp = new XMLHttpRequest();
// this handles the request
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
// if the request came back successfully
if (xmlhttp.status == 200) {
// write the response to a div
div = document.getElementById("responseText")
div.innerHTML = xmlhttp.responseText;
} else {
// if the request had an error
div.innerHTML = div.innerHTML + '<br />Error fetching rates error code : ' + xmlhttp.status;
}
// rerun this function to fetch updates
setTimeout(fetchRate, 1000);
}
};
// open the AJAX Object
xmlhttp.open("GET", "<?= basename(__FILE__) ?>?fetchOnly", true);
// send the AJAX request
xmlhttp.send();
}
</script>


Thanks in advance

Answer

Simple xml will return false if load fails, so use that simply for your check. Single point reliable check.

<?php
    function parserSide($feedURL) {
    $rss = simplexml_load_file($feedURL);
    $output = "";
    if($rss){
        //simple_xml load was a success. It does everything
        $output .= "<ul class='newsSide'>";
        $i = 0;
        foreach ($rss->channel->item as $feedItem) {
            $i++;
            $output .= "<li><a href='$feedItem->link' title='$feedItem->title'>" . $feedItem->title . "</a></li>";
            if($i >= 1) break;
        }
        $output .= "</ul>";
    }
    return $output;
}
if(isset($_GET['fetchOnly'])){
    $url = 'http://feeds.reuters.com/reuters/topNews';
    $handle = fopen($url, 'r');
    $response = parserSide($url);
    echo $response;
    die();
}
?>
<div id="responseText"></div>
<script>
    // run the function, it will re-run itself
    fetchRate();
    function fetchRate() {
        var div = document.getElementById("responseText")
        div.innerHTML = "attempting to load data from server....";
        // create the new AJAX Object
        xmlhttp = new XMLHttpRequest();
        // this handles the request
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == XMLHttpRequest.DONE) {
                // if the request came back successfully
                if (xmlhttp.status == 200) {
                    // write the response to a div
                    div.innerHTML = xmlhttp.responseText;
                } else {
                    // if the request had an error
                    div.innerHTML = div.innerHTML + '<br />Error fetching rates error code : ' + xmlhttp.status;
                }
                // rerun this function to fetch updates
                setTimeout(fetchRate, 1000);
            }
        };
        // open the AJAX Object
        xmlhttp.open("GET", "?fetchOnly", true);
        // send the AJAX request
        xmlhttp.send();
    }
</script>
Comments