You might immediately notice that the code in Listing 3 doesn't look like PHP. That's because most of it isn't. It's standardized output that requires little in the way of dynamic content. The The The The The Finally, the Listing 4 is the main loop that produces each entry in the Atom feed. The vast majority of the work for producing the feed is done here. Once again, Listing 4 covers quite a bit of ground. First, is the Then the counter ( The next two lines retrieve the article date (from the Next, the The The The Next comes the The Finally, the loop counter ( That, in a nutshell, is the entire body of code associated with producing an Atom document from the Also, keep in mind that many elements in the Atom specification are not covered here. You can just as easily add those by following the same patterns I describe in this section of the code. For more information, see Resources. Now comes the fun part: testing! Rather than retype (or copy and paste) everything you see in the code listings above, you can simply use the PHP file that is included in the Download section. Copy that file to a local directory and make the necessary database changes that I described earlier (user name, password, and host). Then copy it to a PHP file structure that has access to the database. When you have the PHP file in the correct place, launch your browser and access your file as follows: http://your host/context/syndication.php. As with any customized solution, you need to change the values in italics to match your specific environment. As I stated previously, your results will vary depending upon which browser and version you use. Some of the more modern browsers detect that this is an Atom feed and display the results accordingly. Others display it in raw XML format. Still others might produce nothing because the document is not a standard HTML document. If the browser does not display the raw XML, you can do so simply by right-clicking on the document and selecting View Source. After you do that, you should see something similar to Listing 5.<feed>
element identifies this XML document as an Atom feed. The namespace used to define the elements is provided as an attribute of the <feed>
element. You also use the aforementioned xml:lang
attribute to specify that this is a document written in English.<title>
element specifies a title for the overall feed. Likewise, the <subtitle>
element specifies a subtitle for the overall feed.<link>
element specifies the URL of this syndication.php document. The address in the example works in the fictitious world that is described in this article, but in real life it does not. In reality, you can include a link that produces the output of this feed.<updated>
element produces a timestamp (compliant with the RFC 3339 standard) that tells the consumer of this feed when it was last updated. In this case, since the feed will always be up to date because it retrieves the latest data from the database, you use the current timestamp. And you may notice that there is a little snippet of PHP code in this element. That is a custom-built PHP function that produces a timestamp in RFC 3339 format.<author>
element defines the author of the overall feed. You'll be using your boss's name as the author because it was his idea.<id>
element uniquely identifies the feed in an Internationalized Resource Identifier (IRI) format.Listing 4. The loop
<?php $i = 0; while($row = mysql_fetch_array($result)) { if ($i > 0) { echo "</entry>"; } $articleDate = $row['posted']; $articleDateRfc3339 = date3339(strtotime($articleDate)); echo "<entry>"; echo "<title>"; echo $row['title']; echo "</title>"; echo "<link type='text/html' href='http://www.fishinhole.com/reports/report.php? id=".$row['id']."'/>"; echo "<id>"; echo "tag:fishinhole.com,2008:http: //www.fishinhole.com/reports/report.php?id=".$row['id']; echo "</id>"; echo "<updated>"; echo $articleDateRfc3339; echo "</updated>"; echo "<author>"; echo "<name>"; echo $row['author']; echo "</name>"; echo "</author>"; echo "<summary>"; echo $row['subtitle']; echo "</summary>"; $i++; } ?>
while
loop. Basically, this part of the code says, in English, "as long as there are rows in the table that haven't been included in the output yet, keep going." The current row in each iteration is stored in a PHP variable intuitively called $row
.$i
) is checked. If the counter is more than 0, then that means this is at least the second iteration. In that case, it is necessary to close the previous iteration's <entry>
element.POSTED
column) and convert it to RFC 3339 format using the aforementioned function.<entry>
element is started. Following that is the <title>
element, which is populated from the TITLE
column in the current row.<link>
element is unusual in that it doesn't contain any child text. Instead, the actual link is referenced as an attribute. This is part of the Atom standard. The link simply points the user to the URL where the user can read the entire article. Recall that this feed provides only a synopsis to the user.<id>
element is similar to the one that was described previously. It uniquely identifies this element in IRI format. And, as before, it is constructed from the relevant URL.<updated>
element contains the DATETIME
value (in RFC 3339 format) from the POSTED
column. Recall that the$articleDateRfc3339
variable for this document was populated earlier in this iteration.<author>
element. This element, unlike the others (but like the <author>
element in the preamble) has child elements. For this article, only one of those children is used: the author's name. The author's name is populated from theAUTHOR
column of the current row.<summary>
element contains the information gleaned from the SUBTITLE
column of the current row.$i
) is incremented, and the loop continues.REPORTS
table. As you can see, it's not as complicated as it might seem at first.
Listing 5. The output (abbreviated)
<?xml version='1.0' encoding='iso-8859-1' ?> <feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom"> <title>Fishing Reports</title> <subtitle>The latest reports from fishinhole.com</subtitle> <link href="http://www.fishinhole.com/reports" rel="self"/> <updated>2009-05-03T16:19:54-05:00</updated> <author> <name>NameOfYourBoss</name> <email>nameofyourboss@fishinhole.com</email> </author> <id>tag:fishinhole.com,2008:http://www.fishinhole.com/reports</id> <entry> <title>Speckled Trout In Old River</title> <link type='text/html' href='http://www.fishinhole.com/reports/report.php?id=4'/> <id>tag:fishinhole.com,2008:http://www.fishinhole.com/reports/report.php?id=4</id> <updated>2009-05-03T04:59:00-05:00</updated> <author> <name>ReelHooked</name> </author> <summary>Limited out by noon</summary> </entry> ... </feed>
Another way to test it is to verify that the feed is valid. You can do that using one of the many Atom feed validators you can find in cyberspace. A good one to use is http://www.feedvalidator.org. That Web site validates feeds in Atom, RSS, and Keyhole Markup Language (KML) formats.
Because you implement and deploy your Atom feed, thousands of new enthusiastic sport fishermen from around the world now have exposure to the fishing reports on your Web site. You are getting hundreds of incoming links from sport fishing sites that are embedding your Atom feed. Some enthusiastic sport fishermen are even using feed readers to view the reports on a daily basis.
Your boss pops back into your office after looking at the latest traffic reports. He is pleased with the additional visits and reports that unique visitors have increased by 10%. He gives you a thumbs up, slurps his coffee, and walks away.
The Atom specification is an ideal means of syndicating your Web content. Using PHP with MySQL, you can easily produce a Web feed that complies with the Atom standard and is always up to date because it reads directly from the database. The feed can then be read by a feed reader or embedded in other Web sites. The end result is broader exposure for your Web content, and that means more visitors and, most likely, an increase to your bottom line.
Crm Services could really give aide to those people that have more tasks than everybody else. availing this CMS Solutions could get your life run a little smoother.Thanks for this very informative review. This seems to be very interesting, and very helpful for the readers.
ReplyDeleteKeep on posting!
Crm Services