tags:

views:

53

answers:

3

In PHP there's a simple function called file_get_contents, and if I wanted to retrieve and display the HTML on, say, google.com, I would just do this:

<?php
$html = file_get_contents('http://www.google.com/');
echo $html;
?>

Is there an equivalent to this in ColdFusion? Can you retrieve the output of an external site into a string variable (and then manipulate it accordingly)?

+1  A: 
<cfhttp method="Get"
   url="127.0.0.1/blah.html"
   name="myvar">
 <cfdump var="#myvar#">
Mr.Dippy
This doesn't work. The `name` attribute is for the name of a query variable, when converting the URL from CSV->query object.
Peter Boughton
+7  A: 

The simplest cross-engine equivalent of what you've written is:

<cfhttp url="http://www.google.com/" />
<cfset html = cfhttp.FileContent />
<cfoutput>#html#</cfoutput>

You can specify an alternative to the auto-created cfhttp variable like this:

<cfhttp url="http://www.google.com/" result="Response" />
<cfset html = Response.FileContent />
<cfoutput>#html#</cfoutput>

Both of those will work in all major CFML engines (Adobe CF, OpenBD, Railo).

You can see the full set of options (methods,params,proxy settings,etc) in the cfhttp documentation, and to see the full response struct, just use <cfdump var=#cfhttp#/> after a call (or whatever the result var is named).


There is an extra option which works with Railo, which is more directly what you've got in PHP, Like this:

<cfset html = FileRead('http://www.google.com/') />
<cfoutput>#html#</cfoutput>

This works because Railo has Resources (virtual filesystems), so everywhere you can do a file operation, you can use various virtual filesystems, including HTTP, ZIP, RAM, and others.

(Adobe have started adding virtual filesystems also, but I think so far only support RAM, so this doesn't work there.)

Peter Boughton
Thanks, that worked great!
SoaperGEM
+1  A: 
<cfset destination = "http://www.google.com"&gt;
<cfhttp url = #destination# method = "post" result="httpResult">
<cfoutput>#httpResult.fileContent#</cfoutput>
Nick