获取新闻发布到网站

时间:2013-01-13 19:07:09

标签: php html web

我想知道是否有办法从网站上获取包含图片的最新帖子(如果帖子有帖子)。

例如,获取带有图像的最后20个英国广播公司新闻业务帖子并将其显示在我的网站上?

2 个答案:

答案 0 :(得分:1)

找到该站点的RSS源的URL然后使用看看php feed库SimplePie。它会将您提供的网址上的Feed转换为易于使用的pho对象格式。

就你的例子而言,BBC英格兰的新饲料之一是http://feeds.bbci.co.uk/news/england/rss.xml

首先抓住简单的派对库来源:http://www.simplepie.org/downloads/

要在PHP中使用这个Feed并将其显示给用户,请执行以下操作:

 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');    
 }

答案 1 :(得分:1)

有些网站有API,可直接访问其内容,您可以使用XML或JSON格式进行检索。

你必须使用像SimpleXML,DomXML,json_decode();在PHP中将结果缓存在数据库中,或查询其API。

相关问题