How to use PHP to write an RSS subscription _ PHP Tutorial

Source: Internet
Author: User
How to use PHP to write an RSS subscription. RSS aggregation is very popular recently, so at least understanding RSS and its working methods is an urgent need of a PHP developer. This article introduces the basic knowledge of RSS and the popularity of RSS aggregation among many RSS applications. it is an urgent need for a PHP developer to understand RSS and its working methods at least. This article introduces the basic knowledge of RSS, some usage of RSS for many purposes, how to use PHP to create RSS feeds from the database, and how to use the XML_RSS module to read existing RSS feeds and convert them to HTML.

RSS aggregation is one of the most common TLA statements. RSS has many meanings as acronyms, but the standard meaning is: truly simple aggregation. This is the latest explanation of this extremely common and extremely useful standard.

In the early days of the Internet, a software called Pointcast pushed data to screen saver applications on users' computers to provide various news updates. Finally, browser developers (such as Netscape and Microsoft) created something similar to this very popular service. Netscape developed the most accepted variant, which was later published to the Internet development environment and eventually developed into the current RSS.

RSS delivers recently updated information to many recipients, very similar to broadcast systems. If you have a large number of users, the RSS feed will attract your users back to view updates like the beacon. Because RSS provides a much-needed way to maintain user attention, it is no wonder that RSS is becoming increasingly popular and used by more and more content providers.

This icon is a standard icon that provides the RSS service for content updates. The curves in the icons represent radio waves, and represent the broadcast nature of RSS feeds.

A considerable number of applications (many of which are free of charge) can read RSS feeds, many of which allow users to aggregate feeds. The aggregation feature even allows users to further improve the quantity and nature of received content. Each reader has different features designed to help manage incredible information volumes from the Internet.

Mozilla's Thunderbird and Firefox, Microsoft's Internet Explorer 7 and the coming Office versions, and many other products are examples that are closest to your search engine. Among the various methods for getting and reading the abstract, you should find the method that suits you. Of course, unless you are a discerning software developer and want to write your own program! This article will be introduced soon!

How to use RSS

Your site contains the content you want to publish to the public, which is the primary reason for publishing the content to the Internet. If many users know your site and content, will they come back to view updates every day? I'm afraid not. On websites that you frequently visit, do you visit them every day to check for updates? I'm afraid not. This is why RSS is generated.

RSS can be a huge benefit for your users, especially when they think that the views or News listed on your site are valuable. You can accurately understand your updates or added content without frequently returning your site, saving you time and energy, and never miss any updates!

If you integrate RSS feeds to support content aggregation on your site, it is not a problem to generate content. If you pull data from the abstract and include it in your site, you only need to invest a little time to add a lot of content to your site.

I personally like to use RSS to collect filtered results from different sites (such as Craigslist (www.craigslist.org. One of my tips is to use it to buy second-hand electronic products. You can set up a site search and then the RSS results page. If you have set a video clip to search for a camera within a certain price range, when someone publishes the camera sales information within your specified price range, you will see it in your RSS feed! If you want to become the first bidder, this will provide you with a huge advantage!

RSS 2.0 standard format

RSS standard definition and content containing the feed. These feeds may come from any data source defined as an Internet document. they are actually composed of a list of links and their descriptions.

Please refer to the following RSS format, which uses a sample document from the NASA "Liftoff News" feed.

 
   
      
   Liftoff News    
   http://liftoff.msfc.nasa.gov/    
   
    Liftoff to Space Exploration.
       
   
    en-us
       
   
    Tue, 10 Jun 2003 04:00:00 GMT
       
   
    Tue, 10 Jun 2003 09:41:01 GMT
       
   
    http://blogs.law.harvard.edu/tech/rss
       
   
    Weblog Editor 2.0
       
   
    editor@example.com
       
   
    webmaster@example.com
       
         
    Star City      
    http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp      
    
     How do Americans get ready to work with Russians aboard the        International Space Station? They take a crash course in culture, language        and protocol at Russia's Star City.
          
    
     Tue, 03 Jun 2003 09:39:21 GMT
          
    
     http://liftoff.msfc.nasa.gov/2003/06/03.html#item573
        
           
         
    Space Exploration      
    http://liftoff.msfc.nasa.gov/      
    
     Sky watchers in Europe, Asia, and parts of Alaska and Canada        will experience a partial eclipse of the Sun on Saturday, May 31st.
          
    
     Fri, 30 May 2003 11:06:42 GMT
          
    
     http://liftoff.msfc.nasa.gov/2003/05/30.html#item572
        
           
         
    The Engine That Does More      
    http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp      
    
     Before man travels to Mars, NASA hopes to design new engines        that will let us fly through the Solar System more quickly.  The proposed        VASIMR engine would do that.
          
    
     Tue, 27 May 2003 08:37:32 GMT
          
    
     http://liftoff.msfc.nasa.gov/2003/05/27.html#item571
        
           
         
    Astronauts' Dirty Laundry      
    http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp      
    
     Compared to earlier spacecraft, the International Space        Station has many luxuries, but laundry facilities are not one of them.        Instead, astronauts have other options.
          
    
     Tue, 20 May 2003 08:56:02 GMT
          
    
     http://liftoff.msfc.nasa.gov/2003/05/20.html#item570
        
     
  
 

The first child object of a document in XML format is . The channel is the feed itself and its related information. Many RSS feeds have one channel object, but if you want to use any filter to separate the feed, you can use multiple channels. Channel objects must be the title, link, and description objects. They define the basic description of the abstract. Optional objects: language, copyright, managingEditor, webMaster, pubDate, lastBuildDate, category, generator, docs, cloud, ttl, image, rating, textInput, skipHours, and skipDays.

The number of projects that a channel can contain is unrestricted. All the elements of the ITEM element are optional. However, at least one title or description is required to verify the element. These elements are title, link, description, author, category, comments, enclosure, guide, pubDate, and source.

Where can I use PHP?

This document assumes that you have some experience using PHP and can use functions to pass variables and return results. PHP has many functions that can quickly process XML inside and outside the application.

First, you want to obtain information from a locally stored data source (a content management system, blog, or any content that complies with the Internet document format) and publish it as a feed to your users. You need to get the data, format it as an RSS object, and then provide it to the request that requires it.

If some parts of your site require additional content, you do not need to search for additional content documents for your site all over the world. Instead, you can take advantage of a large number of RSS feeds you have prepared. You can use XML_RSS to obtain and process these feeds for your site.

XML_RSS () is a PEAR package that helps you easily complete more complex tasks-interpreting xml rss files. PEAR is an open-source library for PHP functions. it can be used for free and its development continues. You may have installed PEAR when installing PHP, but you may still need to install it for this article. XML_RSS () is just a function that loads the XML of the feed to an array when the location of the RSS feed is known, so as to prepare for its use in your PHP application. The elements of this array have keys named based on the elements and attributes of the read RSS file.

Extract data

Now that you know what the RSS data format is, you can view the data you want to publish and convert it to the RSS format. Fortunately, PHP has some powerful RSS and XML processing features that can accelerate your development. Like many common Web standards, PHP has many powerful functions that can be used in this application.

In this article, you will use "" to pull data from the database and format it as an RSS feed. You will set it so that whenever you call this RSS feed, it will be able to search for new content in your dataset and return a new RSS for the requester.

The feed can come from any data source on your site, but ultimately you need to ensure that it contains enough data so that people who receive the RSS feed can use this data. At least the URL name and description are required. Any data published on your site can be converted to a feed.

You will use PHP to connect to the Web application database, pull out the updated information, and format it as an xml rss document.

Assume that you have selected a database, you can create a connection in normal mode, and generate a page to display the XML layout in a way suitable for users to read.

Since you have formatted the data in your own code, you need to ensure that the data is published correctly so that when you input your URL into the reader, get the xml rss feed they need.

Complete RSS. php
             
                 
                      
   Our Demo RSS                    
   http://www.tracypeterson.com/RSS/RSS.php</link>                    
   
    A Test RSS
                       
   
    en-us
                       
   
    $now
                       
   
    $now
                       
   
    http://someurl.com</docs>                    
    
     you@youremail.com
                        
    
     you@youremail.com
                ";            foreach ($return as $line){    $output .= "
    
     ".htmlentities($line['headline'])."                    
     ".htmlentities($line['link'])."                    
     
      ".htmlentities(strip_tags($line['description']))."
                     
    ";}$output .= "
   
  
 ";echo $output;?>

Therefore, let's gradually achieve this goal. First, set a database connection object to the local database. In this database, there is a table with a record containing the title, link, and description fields. you put the content in your XML response. You use MYSQL_QUERY () to execute SQL queries on your table, and then you use WHILE to re-format the results to pre-arrange the obtained objects, and reformat the data into a new simple array.

After the new array is ready, you can start to use the $ output variable to construct an XML file, and add new elements by traversing each returned response by the $ line array. This does not take too much time, because in your SQL statement, you set the response limit to 15. To use this code snippet as the starting building block, you must replace the virtual link, database name, and login information to reflect your environment.

After the script is executed, you will get a very clean RSS file output, similar to listing 3.

RSS. php output
     
         
              
   Our Demo RSS            
   http://www.tracypeterson.com/RSS/RSS.php</link>            
   
    A Test RSS
               
   
    en-us
               
   
    Mon, 13 Nov 2006 22:46:06 PST
               
   
    Mon, 13 Nov 2006 22:46:06 PST
               
   
    http://someurl.com</docs>            
    
     you@youremail.com
                
    
     you@youremail.com
        
                
     This is Tracy's Web Page!            
     http://www.tracypeterson.com/</link>            
     
      This is a demonstration of how to get PHP to work for your RSS feed.
             
    
                
     This is Tracy's site again!            
     http://www.tracypeterson.com</link>            
     
      Again, this is a demonstration of the power of PHP coupled with RSS.
             
    
   
  
  

Now anyone can load the new RSS file containing all your updates by entering the RSS. php URL!

Import data

You will use the XML_RSS () function to import the RSS feed to a PHP script so that it is ready as any array. Just like querying a database, you will have an array ready for use as needed.

In this case, you connect to RSS. php, load a copy, and display it in the unordered list.

Showfeed. php
 parse();echo "Headlines from Tracy         Peterson's Siten"; echo "
 
 
    n";foreach ($rss->getItems() as $item) { echo "
  • " . $item['title'] . "
  • n";}echo "
n";?>

This example is directly from the PEAR manual because it is very concise. Let's check it row by row to see if it only uses the XML_RSS () class, constructor, and parse () methods. The parsing operation only renders the output as an array I mentioned earlier.

First, you use the require_once () function to load the RSS. php file from the PEAR installation. If PEAR is correctly set and XML_RSS is installed, it will find the reference file correctly and the XML_RSS object will be ready for your use. Next, you create a new object named $ rss, which is the result of passing the URL of the feed to your XML_RSS constructor.

You only use the parse () method to return values in the RSS feed. The first echo line starts to set the basic HTML for the RSS feed to be read by the user. In this case, you declare that this unordered list is a list of titles from my sites!

The foreach () statement uses the getItems () method as a new array $ items and obtains each project element from the parsed abstract. Each array element is named based on the actual XML tag containing them. In this case, you only use links and titles, and you will add a description of them later. Each time a foreach loop is processed, it is moved to the next element until the entire RSS feed is displayed in this way.

Now, add the description to each displayed result.

The following code is added to the foreach () loop.

foreach ($rss->getItems() as $item) {  echo "
  • " . $item['title'] . "

  • "; echo $item['description'] . "

    n"; }

    You only need to add a line break and description line to the unordered list.

    Conclusion

    In general, the Internet has just begun to realize its incredible potential. With the improved accessibility provided by RSS, you can now simplify the process of keeping your users synchronized. You can promptly notify users of updates without having to spend time checking whether new information has been added.

    You have just explored the RSS standard and how to create, receive, and convert a feed to available HTML. Now you are ready to use these skills in larger applications.

    Http://www.bkjia.com/PHPjc/752469.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752469.htmlTechArticleRSS aggregation has been very popular recently, so at least understanding RSS and how it works is an urgent need for a PHP developer. This article introduces the basic knowledge of RSS and its many uses...

    Related Article

    E-Commerce Solutions

    Leverage the same tools powering the Alibaba Ecosystem

    Learn more >

    Apsara Conference 2019

    The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

    Learn more >

    Alibaba Cloud Free Trial

    Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

    Learn more >

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.