[英]Lazy loading a RSS feed in PHP
我自己編寫的插件使我的Wordpress網站運行緩慢。 此插件從其他站點上的RSS feed加載事件。
我禁用了該插件,它在Google Page Speed中的差異為20。
我該如何在Ajax或Javascript中延遲加載RSS feed?
我有的代碼:
$rss = simplexml_load_file(get_option('capu_url'));
foreach ($rss->channel->item as $item) {
echo '<h4><a href="'. $item->link .'">' . $item->title . "</a></h4>";
//echo "<p>" . $item->description . "</p>";
$dom = new DOMDocument;
$dom->strictErrorChecking = FALSE ;
libxml_use_internal_errors(true);
$dom->loadHTML($item->description);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//ul[@class="ee-event-datetimes-ul"]'); // get <ul>'s with class 'up'
foreach($nodes as $node) { // loops through each <ul>
foreach($node->getElementsByTagName('li') as $li) { // loops through the <li>'s
echo $li->nodeValue . "<br/>\n"; // echo's the <li> elements
根據對服務器的訪問權限,考慮運行一個cron作業,例如每小時運行一次以檢索RSS feed,將其轉換為JSON並將其另存為Web根目錄中的某個文件。
現在,您可以使用JavaScript從服務器以JSON數據文件的形式異步檢索提要,並將其顯示在您的站點上。
如果您沒有外殼程序訪問權限,請嘗試在Wordpress中使用wp_cron
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.