ColdFusion HTTP (cfhttp)

Using the ColdFusion <cfhttp> tag, you can grab someone elses web page and present it or parts of it on your own site. I won't go into the copyright implications of this, but, assuming the third party agrees, this can be a really cool feature.

To grab a remote web page, you first use the <cfhttp> tag to do an HTTP call. Once it's done the HTTP call, the contents of the web page is stored in a variable called cfhttp.filecontent.

Since it's stored as a string, you can manipulate it just as you could with any other string. This enables you to display only part of the website if you wish. For example, you could present news, weather, stock prices etc from a third party source.

A <cfhttp> Example

In this example, we peform an HTTP call against a third party website. We then display the website by outputting the variable that it's code is stored in.

The above code would result in the third party web page being displayed within our own web page.

Grabbing Part of a Web Page

As cool as the above example is, there's probably not much value in doing this. If you really wanted the above result you could just have easily used frames or inline frames.

In reality, you might only want to display part of the web page on your page. For example, the above website includes a header, left navbar, footer etc. What if you only wanted the middle bit (the code generator)?

You can use ColdFusion's built in string functions to manipulate the contents of the cfhttp.filecontent variable. This way, you could eliminate all unnecessary code from the variable and only display the bit that you want.

Viewing the Source Code

The reason you can do this is because, the contents of the cfhttp.filecontent variable is actually the source code of the remote page. When we view it in a browser, the browser renders the source code, but the contents of the variable is still just a string of source code.

For example, if I use ColdFusion's htmlCodeFormat(), trim() and left() functions, I can view the top 250 lines (or as many as I like) of the source code.

Coding this:

Results in this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MySpace Text Generator</title>
<link href="https://fonts.googleapis.co

A More Useful Example

The following example uses ColdFusion's FindNoCase(),RemoveChars() and Len() functions to display only the part of the web page that we want to display — the HTML text generator.

Fortunately, the remote site has two HTML comment tags (<!-- Start Syndication --> and <!-- End Syndication -->) that indicate the start and end of the HTML text generator. We can use these tags to pick out everything in between them.

Coding this:

Results in this:

ERROR: We can't display this example right now, as there was a problem in processing it. A possible cause is that the remote site is down, or that the content has changed.