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
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.
In this example, I peform an HTTP call against a page on a third party website. Actually, for the purposes of this example, I'm using a different page on the same website. But this could easily be a page on another website.
Once I've done the HTTP call, I then display the page 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.
In this case, I'm simply using the homepage of this ColdFusion tutorial that you're reading now, which is located here (opens in a new window).
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 (in this case, the table of contents for the ColdFusion tutorial)?
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
left() functions, I can view the top 100 lines (or as many as I like) of the source code.
Results in this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" cont
A More Useful Example
The following example uses ColdFusion's
Len() functions to display only the part of the web page that we want to display — the table of contents for the ColdFusion tutorial.
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.
Results in this:
- ColdFusion Installation
- ColdFusion Administrator
- ColdFusion Editors
- ColdFusion Syntax
- ColdFusion Includes
- ColdFusion Variables
- ColdFusion Variable Types
- ColdFusion If Statements
- ColdFusion Loops
- ColdFusion Datasource
- ColdFusion Database Queries
- ColdFusion Lists
- ColdFusion Arrays
- ColdFusion Redirect
- ColdFusion Debugging
- ColdFusion Error Handling
- ColdFusion Mail
- ColdFusion Functions
- ColdFusion Components
- ColdFusion Web Services
- ColdFusion Upload File
- ColdFusion Read File
- ColdFusion Write File
- ColdFusion Append File
- ColdFusion Rename File
- ColdFusion Copy File
- ColdFusion Move File
- ColdFusion Delete File
- Cffile Parameters
- Using cffile Parameters
- ColdFusion FTP
- cfftp Cached Connections
- ColdFusion HTTP (cfhttp)
- ColdFusion Query of Queries
- ColdFusion Charts
- ColdFusion Scheduled Tasks
- ColdFusion Package & Deploy
- ColdFusion Summary
The above example is actually a live example. I am actually using the above sample
cfhttp code to pull that table of contents in from the CF tutorial homepage.