使用 PHP 从 RSS 解析 XML 并使用 AJAX 传递
Parse XML from RSS with PHP and pass using AJAX
我想在 rss link(路透社:头条新闻)中获取最新的 ITEM,当新的 ITEM 出现时,PHP/AJAX 会将旧的 ITEM 替换为新的.
我尝试使用 PHP 和 AJAX 来做到这一点,但我得到的是空白页或 "Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\test4.php on line 11"。
函数:
<?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>
提前致谢
如果加载失败,简单 xml 将 return false,因此只需将其用于检查。单点可靠校验。
<?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>
我想在 rss link(路透社:头条新闻)中获取最新的 ITEM,当新的 ITEM 出现时,PHP/AJAX 会将旧的 ITEM 替换为新的.
我尝试使用 PHP 和 AJAX 来做到这一点,但我得到的是空白页或 "Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\test4.php on line 11"。
函数:
<?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>
提前致谢
简单 xml 将 return false,因此只需将其用于检查。单点可靠校验。
<?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>