Home » Tutorials Beginner Tutorial

"Roll Your Own" JavaScript and PHP Syndication

Page Visited Visited: 20004
2.8/5.0 (4 votes total)
Rate:

by Will Bontrager
April 28, 2006


Will Bontrager
Will Bontrager has written 1 tutorials for JavaScriptSearch.
View all tutorials by Will Bontrager...

When you have content on your web site that you wish to make available for others to publish, yet want control of what is published, it can be syndicated with JavaScript or with PHP. Let me stress that you can control what is published, not if it is published.

If you wish the ability to ban certain domains from having your content delivered to them, and the ability to limit delivery to only certain domains you specify, Master Syndication Gateway V2 from http://willmaster.com/gateway can do that. It's excellent software.

But if you want to allow anyone to publish your content, so long as you control what is published, this article will show you a way to do it yourself.

How You Control What Is Published

The content others publish is delivered from your server to their web page with either JavaScript or PHP.

If you make a change to the content, the change is immediately available on all remote web pages where your content is published. Thus, you could make seasonal changes or update news or rotate ads and so forth.

Because it is on your server, you control the content.

Differences Between JavaScript and PHP Syndication

This table shows differences between the two syndication methods, JavaScript and PHP:

  JavaScript PHP
Easy for site owner to prepare content. No Yes
Easy for syndication sites to use. Yes Yes
Search engine friendly syndicated content. Unlikely Yes
Will work on all syndication web sites. Yes No
Will work in all browsers. No Yes

At first glance, it would seem that PHP is the clear winner. The one "No," however, could have a huge impact on how many sites can carry your content.

Weigh the importance of each line of the table before making a decision for one or the other, or for using both methods.

Line #1
Easy for site owner to prepare content.

To syndicate with JavaScript, the syndication content needs to be converted to JavaScript code. With PHP, no conversion is required. Both methods are addressed in their respective sections, below.

Line #2
Easy for syndication sites to use.

Both JavaScript and PHP require only a couple lines of code that remote syndication web site owners paste into their pages. The code pulls your content into the page.

Line #3
Search engine friendly syndicated content.

It is possible but unlikely that search engine spiders will follow URLs of JavaScript syndicated content. PHP syndicated content appears to spiders just like static text.

Line #4
Will work on all syndication web sites.

The JavaScript code will work on all web pages (provided the browser is JavaScript enabled, see next). PHP, on the other hand, requires that the server has PHP installed and configured in a certain way and it requires web pages to have certain file name extensions, the most common being extension .php

Line #5
Will work in all browsers.

The JavaScript method requires browsers to be have JavaScript turned on. Without JavaScript, the content is not displayed.

Because PHP appears to browsers just like static text would, it works just fine in all of them.

Deciding What To Syndicate

Almost anything that a regular web page can have, syndicated content can have, too.

There are exceptions and restrictions, however.

The content you will be syndicating can have no relative URLs. All URLs must be absolute http://... URLs — images, links, all of them. Because your content will be on other servers, relative URLs will break or point to actual locations on the remote server instead of on yours.

Style sheets that modify predefined HTML tags (like <p> or <h1>, etc.) should not be syndicated. When such a style sheet is syndicated, whether in the content or as a remote file, it can have an effect on all content on the page, not just your syndicated content. If you mess with a web page's style you could make site owners unhappy.

If you must use CSS styles, define your own style with names unlikely to be used on any web sites your content may be published at.

Forms, sounds, images, additional JavaScript, all can be published on remote web sites. Just remember that URLs (to sounds, images, and so forth) must be absolute http://... URLs.

SSI tags and PHP code can be processed before delivering the content with the PHP syndication method, but not when syndicating with JavaScript. (SSI tags and PHP code are the only exception to the "all URLs must be absolute" rule, and that's because they are processed before the content is delivered to the remote web site.)

Only content that would normally go in the BODY area of web page source code should be published.

The JavaScript Method — Preparation

Preparing the content for JavaScript syndication isn't hard, but can be tedious.

First, put the content for syndication into a separate file.

Note that SSI and PHP aren't processed when syndicated with this method. To syndicate content processed with SSI tags or PHP code:

  1. Put the SSI/PHP on a web page on your server.
  2. Type the URL of the web page into your browser.
  3. Use your browser's "View Source" menu item and copy the relevant content.
  4. Paste the content into the file containing the content to be syndicated.

Edit the syndication content file with these steps:

  1. Precede each backslash ( \ ) with another backslash: \\
  2. Precede each apostrophe ( ' ) with a backslash: \'
  3. Replace the word: script
    with: scri'+'pt
    wherever it occurs. (This is to prevent browsers from misinterpreting the word "script" as JavaScript that must be executed independently.)
  4. Begin each line with: document.writeln('
  5. End each line with: ');

Save the file with a .js file name extension and put it on your server in a location where your browser can access it (type the file's URL into your browser to test).

The JavaScript Method — Syndication

Here is the JavaScript that remote syndication site owners paste on their web pages. Where they put the JavaScript is where your content appears:

<script 
type="text/javascript"
language="JavaScript"
src="http://example.com/file.js">
</script>

Replace http://example.com/file.js with the URL to the syndication content file on your server.

The PHP Method — Preparation

Put the content for syndication into a separate file.

Save the file with an .html (or other) file name extension and put it on your server in a location where your browser can access it.

Other file name extensions that may be used are .htm, .php, or .shtml — .php if you require PHP code to be processed before delivering the content or .shtml if you require SSI tags to be processed.

Type the file's URL into your browser to test.

The PHP Method — Syndication

Here is the PHP that remote syndication site owners paste on their web pages. Where they put the PHP is where your content appears:

<?php
@readfile('http://example.com/file.html');
?>

Replace http://example.com/file.html with the URL to the syndication content file on your server.

Automating Things

After you've done things manually for a while, you may wish to automate the process. The Master Series at http://willmaster.com/scripts has several syndication software tools to do exactly that.

Syndication software can have control panels to paste in the content to be syndicated, to prepare the content, to generate the code remote syndication sites paste into their pages, and can maintain many different syndication channels.

Some syndication software inserts affiliate codes into syndicated content, automatically.

 ____

Will Bontrager
Copyright 2006 Bontrager Connection, LLC


Add commentAdd comment (Comments: 0)  

Advertisement

Related Resources

Other Resources

arrow