3

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

3
  • You might be able to do this directly using just javascript. Commented Jul 14, 2016 at 7:17
  • I need the php for add the title in a db after @iceman Commented Jul 14, 2016 at 7:18
  • remove your fopen($url) checks. Commented Jul 14, 2016 at 7:50

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

stackoverflow.com/questions/38682343/… how can i do a loop of the get part? tnx @iceman

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.