Getting content from a remote server using Smarty

October 22, 2009

Although our CMS is designed to handle most of the requirements of our clients, occasionally we find ourselves needing to include information that is stored in another system. One such project (the details of which I can't reveal yet) was just such a case. The client has a legacy e-commerce and CRM system running on a Windows server, which they are tied to for various reasons.

Ideally, we would interact with the remote app by using public APIs to fetch data and do any processing and formatting on our server before outputting it in a Smarty template. This particular system, however, does not provide an API.

Fortunately, PHP provides several ways to get the text of a remote file and manipulate it as you would any other content. The easiest of these methods, and the one we decided to use, is file_get_contents. Given the name of a file, this function returns its entire contents as a string, easy as that. Now we can just output that string into the content area of our template, surrounded by the same header, footer, and navigation as the other pages on the site.

Is It Really That Easy?

Unfortunately, there are a few caveats. The first is that you either need to have access to the templating system of the app you are pulling from—so you can remove everything but the actual content—or you will need an HTML parser to pull out the section you want to use. Dropping an entire html page into your template will give you a huge mess, as you will have duplicate <html>, <head>, and <body> tags, which is most definitely not allowed.

The second issue is that any relative urls in image or link tags will now be broken. We are using a regular expression (created using our handy new RegEx Tester!) to find all the relative urls and replace them with absolute urls pointing back to the original server.

Smarty Plugin

I have wrapped this whole process up in a Smarty plugin. Just download the plugin file and drop it into the plugins folder inside your copy of Smarty. There is no configuration or installation: you're ready to go.

Usage

{get_remote url="http://www.test.com/page.html" [default="Text to show if the file isn't found"] [fix="href|src"]}

Parameters

  • url: The remote url to fetch. This is the only required parameter.
  • default: Text to be shown if the remote file couldn't be found or if no url is passed in. Defaults to "Not found".
  • fix: Pipe-delimited list of html attributes that will have their contents checked for relative urls. Defaults to "href|src|action".

Warnings!

Smarty evaluates PHP code that is included this way! Generally, this shouldn't be a problem, since the remote server will have already processed the php code and given you the results. However there is the potential for serious security issues here if, for instance, the remote server doesn't have PHP enabled and gives you the raw source code. If you don't trust the remote server, you probably shouldn't be using this plugin.

This is a beta release. There are likely still bugs in it. If you find any, please report them in the comments.

What is still to do?

Caching is the main thing. I plan to add that capability within the next few weeks. I would also like to implement more flexible url replacements at some point (i.e. not just in html attributes), but that is a fairly complex problem.

Leave the first comment