Figure 2: XML Syndication: Efficient But Requires Expertise
If you are syndicating to websites that are not under your control, you don't know that the webmaster will have the expertise to implement a syndication strategy using XML. You might be syndicating to a small company that used FrontPage to make the website; they certainly can't set up a dynamic process to fetch an XML feed from your site, cache it, and integrate the data into their site.
- For the target website, implementation is simple: just add one line of HTML to the target page. It works for any web server environment -- you don't need server-side technologies such as PHP, Perl, Python, or Java.
- When you update the content on your site, changes are immediately reflected on syndication sites. With cached solutions such as RSS there is typically a self-imposed delay of up to an hour.
- You can control how your content is presented, or you can allow partners to customize the presentation.
Figure 3: Tracking visitors on the syndication site
Let's assume that you run a network of websites, each on a different server, and you want to put a common element at the top of each page:
topbar.js like the following:
document.write('<div class="topbar">'); document.write('The Fubar Network: '); document.write('<a href="http://web1.net">Web1<\/a>,'); document.write('<a href="http://web2.net">Web2<\/a>,'); document.write('<a href="http://web3.net">Web3<\/a>'); document.write('</div>');
Encoding Your Data
Even with the simple example above, some problems might occur.
Since everything in
topbar.js must be valid
correctly. For example, since the
function is printing a single-quoted string, you must be sure to
"escape" any single quotes within that string.
</" -- older browsers have been known to abort when
encountering this sequence -- so you must escape that as well.
As an alternative to worrying about which characters to escape, I
recommend encoding all of your data,
unescape() function to decode
it prior to writing. So our file
topbar.js becomes the following (the encoded data has been abbreviated in this example):
In the example above, we displayed static HTML, but what if we want to display dynamic information? For example, we might want to query a database and display the time of the next live event.
A Dynamic Example
First we'll write our PHP page:
<script> element can be used to fetch any URL, even if it's not a
.js file, so we'll tell it to fetch
topbar.php instead of
Let's try it right here to see it in action:
Here is how it works:
- When you include
ob_start(), to start output buffering.
While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. -- PHP documentation
- When your script finishes, PHP automatically calls the function
- Design the content you want to syndicate - it can be plain HTML or dynamic PHP. We'll call the file
<?php // Simulate fetching the live event $event = sprintf('%s: <a href="%s">%s</a>', 'Tuesday', 'http://web1.net/events/', 'Internet Security Webcast'); ?> <div class="topbar"> The Fubar Network: <a href="http://web1.net">Web1</a>, <a href="http://web2.net">Web2</a>, <a href="http://web3.net">Web2</a> <div>Upcoming event: <?= $event ?></div> </div>
- Create the syndication file
- To display your syndicated content, add the following to any HTML page:
The examples so far assume that every website would display the same syndicated content, but you can use parameters to customize the output for each of your syndication partners.
For example, if your PHP script accepted a parameter
n, then you could specify
Adding a Web Wizard
writes content to the page using
you can do more than that in your script.
- MagpieRSS: RSS for
- XML button created via CSS
Clever technique for creating an XML button without using a graphic.