开发者

How do you retrieve HTML from an external website into a variable in ColdFusion?

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 开发者_JAVA百科into a string variable (and then manipulate it accordingly)?


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.)


<cfset destination = "http://www.google.com">
<cfhttp url = #destination# method = "post" result="httpResult">
<cfoutput>#httpResult.fileContent#</cfoutput>


<cfhttp method="Get"
   url="127.0.0.1/blah.html"
   name="myvar">
 <cfdump var="#myvar#">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜