rssリンク(ロイター:トップニュース)で最新のITEMを取得したいと思います。新しいITEMが来ると、PHP/AJAXは古いITEMを新しいものPHPでRSSからXMLを解析し、AJAXを使用して渡す
私はPHPとAJAXでこれをやろうとしましたが、空白のページまたは「致命的なエラー:最大実行時間30秒がC:\ xampp \ htdocs \ test4.phpの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>
事前に感謝
これは、JavaScriptを使用して直接行うことができます。 – Iceman
@icemanの後のdbにタイトルを追加するにはPHPが必要です – TheQuestionerMan
okです。次にcをします。 – Iceman