我想知道是否有一种方法可以从网站上获取最新的帖子,如果帖子有图片,则包括图片。
例如,获取最近 20 个带有图像的 bbc 新闻业务帖子并将其显示在我的网站上?
找到该站点 rss 提要的 url,然后使用查看 php 提要库 SimplePie。它将您提供的 url 上的提要转换为易于使用的 pho 对象格式。
例如,BBC England 的新提要之一是http://feeds.bbci.co.uk/news/england/rss.xml
先抓取简单派库源码:http ://www.simplepie.org/downloads/
要在 PHP 中使用这个提要并将其显示给用户,请执行以下操作:
 require_once('../simplepie.inc'); //explicitly include the SimplePie library
 $feed = new SimplePie(); //create your feed object
 $feed->set_feed_url('http://feeds.bbci.co.uk/news/england/rss.xml'); //set the feed url to read
 $feed->init(); //Start consuming the feed!
 //the newly initialized feed object has some properties like it name, description, ect...
 echo "Feed Url ".$feed->get_permalink();  
 echo "Feed Title ".$feed->get_title(); 
 echo "Feed Description: ". $feed->get_description(); 
 $count = 0;
 //now, run though post of feeds, stop at 20
 foreach ($feed->get_items() as $item){ 
     if($count >= 20){
          break;
      }else{
          $count++;
      }
      echo "Link to original post: ".$item->get_permalink();
      echo "Title of Post: ". $item->get_title();
      echo "Description of the Post: ". $item->get_description();
      echo "Date Posted".$item->get_date('j F Y | g:i a');    
 }
有些网站有一个 API,可以直接访问其内容,您可以以 XML 或 JSON 格式检索它。
您将不得不使用类似 SimpleXML、DomXML、json_decode(); 在 PHP 中将结果缓存在您的数据库中,或查询它们的 API。