How to use PHP to write an RSS subscription _php tutorial

Source: Internet
Author: User
RSS aggregation has recently been very popular, so at least a good understanding of RSS and how it works is an urgent need for a PHP developer. This article describes RSS basics, some uses of RSS for many purposes, how to create an RSS feed from a database using PHP, and how to use the Xml_rss module to read an existing RSS feed and convert it to HTML.

RSS aggregation is one of the most common TLA (three-letter abbreviations). RSS has many meanings as abbreviations, but the current standard meaning is: Really simple aggregations. This is the latest explanation for this very common and extremely useful standard.

Recalling the early days of the Internet, a software called Pointcast pushes data to a screen saver application on a user's computer to provide a variety of news updates. Ultimately, browser developers (such as Netscape and Microsoft) have created something like this very popular service. Netscape developed the most receptive variant, which was later published in the Internet development environment and eventually developed into the current RSS.

RSS distributes recently updated information to many recipients, much like a broadcast system. If you have a large number of users, the RSS feed is like a lighthouse that attracts your users back to check for updates. Because RSS provides a much-needed way to maintain user attention, it's not surprising that RSS is becoming more popular and used by more and more content providers.

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

Quite a few applications (many of which are free) can read RSS feeds, many of which allow users to aggregate feeds. Aggregation attributes even allow users to further improve the amount and nature of the content they receive. Each reader has different features designed to help manage the incredible amount of information coming from the Internet.

Mozilla's Thunderbird and Firefox, Microsoft's Internet Explorer 7 and upcoming versions of Office, and many other products are some examples of the closest search engine. In so many ways to get and read feeds, you should be able to find the right method for you. Of course, unless you are a discerning software developer, want to write your own program! This article will be introduced soon!

How to use RSS

Your site contains content that you want to advertise to the public, which is the first reason you publish your content to the Internet. If many users know your site and content, will they come back every day to check for updates? I'm afraid not. In the sites you visit frequently, do you visit them every day to check for updates? I'm afraid not. That's why RSS is produced.

For your users, RSS can be a huge benefit, especially if they think the ideas or news listed on your site are valuable. Users don't have to return to your site frequently to get an accurate picture of what you're doing with updates or additions, so users can save time and effort without missing any updates!

If you integrate RSS feeds to support content aggregation for your own sites, then generating content is not an issue. If you pull data from a feed and include it in your site, you can add a lot of content to your site with a small amount of time invested.

I personally like to use RSS to collect a feed of filtered results from different sites, such as Craigslist (www.craigslist.org). One of the little tricks I used was to use it to buy secondhand electronics. You can set up a site search and then the RSS results page. If you set up a feed that searches for a camera in a price range, you'll see it in your RSS feed when someone publishes the camera sales information in the price range you set! If you want to be the first bidder, this will give you a huge advantage!

RSS 2.0 Standard Format

The RSS standard defines and contains the content of the feed. These feeds may come from any data source defined as an Internet document, which is actually made up of linked manifests and their descriptions.

Please see the RSS format below, which uses a sample document from the NASA Liftoff News feed.

 
   
      
   <title>Liftoff News</title>    
   
    
   http://liftoff.msfc.nasa.gov/    
   
    
     Liftoff to Space exploration.
    
       
   
    
     en
     -US
       
   
    
     Tue, June 2003 04:00:00 GMT
    
       
   
    
     Tue, June 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 does Americans get ready-to-work with 
      
     
      Russia NS aboard the International Space station?
 They take a crash course in culture, language and protocol at Russia ' s Star City. 
      
     
      Tue, June 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 would
 Experience a partial eclipse of the Sun on Saturday, May 31st. 
      
     
      Fri, 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 would let us fly through the Solar System to more quickly.
 The proposed VASIMR engine would do. 
      
     
      Tue, 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 Internation        Al Space station had many luxuries, but laundry facilities is not one of the them.
 Instead, astronauts has the other options. 
      
     
      Tue, 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 definition. The channel is the feed itself and its related information. Many RSS feeds have one channel object, but if you want to detach the feed with any filter, you can use multiple channels. The channel object requires title, link, and Description objects. They define the basic descriptive information for the feed. Optional objects are: language, copyright, Managingeditor, WebMaster, PubDate, lastbuilddate, category, generator, Docs, Cloud, TTL, Image, rating, TextInput, skiphours, and Skipdays.

The number of items the channel can contain is unrestricted. All elements of the ITEM element are optional, however, at least one title or description is required to validate the element. These elements are: title, link, description, author, category, Comments, enclosure, guide, pubdate, and source.

Where do you use PHP?

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

First you'll want to get information from a locally stored data source (a Content management system, blog, or any content that conforms to the Internet document format) and publish it as a feed to your users. You need to get this data, format it as an RSS object, and then make it available to requests that require it.

If some parts of your site require additional content, you do not need to look for additional content documents for your site all over the place, but you can take advantage of the large number of RSS feeds that have been prepared. You can use Xml_rss to get and process these feeds for your site.

Xml_rss () is a PEAR software package that helps you more easily accomplish more complex tasks-interpreting XML RSS files. PEAR is an open source library of PHP functions, it is free to use and its development work continues. You may have installed PEAR when you installed PHP, but you may still need to install it for this article. Xml_rss () is just a function that loads the XML of this feed into an array when it is known to be in the position of the RSS feed, and prepares it for its use in your PHP application. The elements of this array will have keys named according to the elements and attributes of the RSS file being read.

Extracting data

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

In this article, you will use "" to pull data out of the database and format it as an RSS feed. You'll set it up so that whenever you call this RSS feed, it can find what's new in your dataset and return the new RSS to the requester.

Feeds can come from any data source in your site, but ultimately you need to make sure that it contains enough data so that the person receiving the RSS feed can use the data. At a minimum, the URL name and description are required. Any data that is 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.

Suppose you select a database, you create a connection in a normal way, and generate a page that displays the layout of the XML in a way that is appropriate for the user to read.

Now that you have nicely formatted the data in your own code, you need to ensure that the data is published correctly so that when users enter your URL into a reader, they can get the XML RSS feed they need.

The Complete rss.php

             
 
   
    
     our Demo rss  
    
  • http://www.tracypeterson.com/rss/rss.php A Test RSS en-US $now $now http://someurl.com 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;? >

    So let's step up to that end. First, you set up a database connection object to the local database. In this database you have a table with records that contain headings, links, and Description fields, and you will request that the content be put into your XML response. You use mysql_query () to execute a SQL query on your table, and then you use the while reformat results to pre-order the resulting objects and reformat the data into a new simple array.

    When the new array is ready, you can start building the XML file with $output variables, appending the new elements in a way that iterates through $line array for each returned response. This should not take too much time, because in your SQL statement, you set the response limit to 15. To use this snippet as a starting building block, you need to replace the virtual link, database name, and login information to reflect your own environment.

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

    rss.php output

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

    Anyone can now load a new RSS file with all of your updates by entering the URL to rss.php!

    Import data

    You will use the Xml_rss () function to import the RSS feed into a PHP script so that it is ready as any array. Just like a query to a database, you will have an array ready to be used when you need it.

    In this case, you will connect to rss.php and load a copy, and then display it in an unordered list.

    showfeed.php

     
      Parse (); echo "

    Headlines from Tracy Peterson ' s Site

    n "; echo "
      n"; foreach ($rss->getitems () as $item) { echo "
    • " . $item [' title ']. "
    • n ";} echo "
    n";? >

    This example comes directly from the PEAR manual, which I use because it is very concise. Let's look at it row by line to see if it uses only a few of the methods provided by the XML_RSS () class, the constructor, and parse (). The parse operation only renders the output as the array I mentioned earlier.

    First, you use the require_once () function to load the rss.php file from the PEAR installation. If PEAR is set correctly and Xml_rss is installed, it will find the reference file correctly, and then the Xml_rss object will be ready for you to 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 use the parse () method only to return the values in the RSS feed. The first echo line starts setting up 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 site!

    The foreach () statement obtains each project element from the parsed feed by using the GetItems () method as a new array $items. Each array element is named according to the actual XML tag that contains them. In this case, you've only used links and headings, and you'll add a description of them. Each time the Foreach loop is processed, it moves to the next element until the entire RSS feed is displayed in this way.

    Now, add the description to each displayed result.

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

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

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

    n "; }

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

    Conclusion

    Overall, the Internet is just beginning to realise its incredible potential. With the increased accessibility offered by RSS, you can now simplify the process of keeping your users in sync. You can notify users of updates in a timely manner without the user having to spend time checking to see if you have added new information.

    You have just explored the criteria for the RSS format and how to create feeds, receive feeds, and convert them to available HTML. Now you're ready to use these skills in a larger application.

    http://www.bkjia.com/PHPjc/752469.html www.bkjia.com true http://www.bkjia.com/PHPjc/752469.html techarticle RSS Aggregation has recently been very popular, so at least a good understanding of RSS and how it works is an urgent need for a PHP developer. This article introduces RSS basics, RSS For many purposes ...

  • 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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.