如何在 PHP 中按日期从 RSS 获取数据?

我正在编写一个小脚本,我只想获取昨天发布的网站的最新帖子,这意味着我想获得昨天的所有链接和标题。我尝试使用我的脚本,但我得到了所有 URL,我不确定如何修复它。


谁能帮我解决这个问题吗?


我想知道是否可以像我们通常在 SQL 中使用的那样使用“where”属性。我只想抓取 1 天的帖子。


<?php

header('Content-Type: application/json');

$url = "https://www.lifegoals.co.in/feed/";

$i=0;

 $invalidurl = false;

 if(@simplexml_load_file($url)){

  $feeds = simplexml_load_file($url);

 }else{

  $invalidurl = true;

  echo "<h2>Invalid RSS feed URL.</h2>";

 }

 if(!empty($feeds)){


  //$site = $feeds->channel->title;

  //$sitelink = $feeds->channel->link;


  //echo "<h1>".$site."</h1>";

  foreach ($feeds->channel->item as $item) {


   $title = $item->title;

   $link = $item->link;

   //$description = $item->description;

   $postDate = $item->pubDate;

   $pubDate = date('D, d M Y',strtotime($postDate));

   $currDate = date('D, d M Y');

   if($i>=10) break;   

   if($pubDate=$currDate){

   $rss =   "<item>

                <title>$title</title>

                <link>$link</link>

            </item>";     

            

   echo $rss;   

   $i++;

   }

  } 

  }

?>

我只想要 1 天的帖子,有 4 天的帖子


人到中年有点甜
浏览 114回答 2
2回答

缥缈止盈

我会为此添加一些调试,以确保您得到您想要的东西。在 foreach 循环中尝试以下操作:&nbsp; &nbsp; print_r([&nbsp; &nbsp; &nbsp; &nbsp; $postDate,&nbsp; &nbsp; &nbsp; &nbsp; $pubDate,&nbsp; &nbsp; &nbsp; &nbsp; $currDate,&nbsp; &nbsp; &nbsp; &nbsp; ($pubDate == $currDate),&nbsp; &nbsp; ]);

蛊毒传说

&nbsp; &nbsp;if($pubDate==$currDate){&nbsp; &nbsp;$rss =&nbsp; &nbsp;"<item>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <title>$title</title>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <link>$link</link>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </item>";&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;echo $rss;&nbsp; &nbsp;&nbsp; &nbsp;$i++;&nbsp; &nbsp;}谢谢==。
打开App,查看更多内容
随时随地看视频慕课网APP