The GET Method
You generally use the GET method of the <CFHTTP> tag to retrieve an object from a remote server. Using this method, you can send information only right in the URL.
Now let's create a simple news agent that goes out to moreover.com and retrieves the headlines for the Phoenix, Arizona area. We do this by first going to http://www.moreover.com and browsing the many categories of information that it makes available (see Listing 1).
Listing 1: NewsAgent.cfm
<!---retrieve the headlines from moreover.com's free news headline service-------> <cfhttp method="get" url="http://p.moreover.com/cgi-local/page?c=Phoenix%20news&o=cf" resolveURL="Yes"> <!---deserialize the retrieved content----> <cfwddx action="wddx2cfml" input="#cfhttp.FileContent#" output="PhoenixHeadlines"> <h3>Today's Headlines in Phoenix</h3> <!---notice that we can output the packet as a query-----> <cfoutput query="PhoenixHeadlines" maxrows="5"> <!---use format, and output 5 headlines----> <font face="verdana, arial, Helvetica"> <a href="#PhoenixHeadlines.Document_URL#" target="_blank"> #PhoenixHeadlines.headline_text#</a>,<br></font> </cfoutput>
Moreover.com asks that developers respect its wishes as they provide this free service. That means retaining the absolute links to the originating news sources and not modifying the content of the headlines themselves.
In the above example, we retrieved a Web page and resolved the URLs so that links and images would still work. The file was set into a variable called CFHTTP.FileContent, which we then displayed with CFOUTPUT. ColdFusion automatically creates this and other variables during the CFHTTP operation. These variables are shown in Table 2.
CFHTTP Variables
There are several variables returned during a CFHTTP operation that you can use with your applications.
Table 2 Variables Returned by CFHTTP
Attribute |
Description |
---|---|
#CFHTTP.FileContent# |
Returns the contents of the file for text and MIME files. |
#CFHTTP.MIMEType# |
Returns MIME type. |
#CFHTTP.Header |
Returns the raw response header as a simple text variable. |
#CFHTTP.StatusCode |
Returns the HTTP error code and associated error string if throwOnError is NO. |
#CFHTTP.ResponseHeader# |
Returns the HTTP error code and associated error string. For instance, "200 Okay" or "404 File Not Found." |
Using a simple <CFOUTPUT> block, you can display the values of these variables form within the same request as a CFHTTP operation.
Saving a Page to a File
Instead of displaying the Web page contents retrieved immediately, we can instead save it to a file for processing later. To do this, you use the PATH and FILE attributes of the tag. Listing 2 shows the how we could retrieve and save the XML headlines page that we viewed earlier.
Listing 2: saveFile.cfm
<body> <!---retrieve the headlines from moreover.com's free news headline service-------> <cfhttp method="get" url="http://p.moreover.com/cgi-local/page?c=Phoenix%20news&o=cf" resolveURL="Yes" path = "C:\Partners" file = "Headlines_#DateFormat(Now())#.xml"> <h2>Done.</h2> </body>
NOTE
When you specify a file path, the CFHTTP.FileContent variable is not available. ColdFusion will throw an error if you try to access it.
Notice that here we have specified part of the filename dynamically using ColdFusion date functions.
Saving Binary Files
You can save binary files in addition to plain text and HTML files. Here's how you do it:
<CHTTP method = "GET" URL = "http://www.CyberTrails.com/MP3s/MySharona.mp3" PATH = "C:\ImportantFiles" FILE = "MySharona.mp3" >