In this hands on example, you are going to make a call to Twitter to get a list of your latest tweets and output them to the contact page.
Tags Used: <cfhttp>, <cfloop>, <cfoutput>
Functions Used: dateFormat
- Open up the /www/contact.cfm file in your code editor.
-
First, make a <cfhttp> call to get the data from Twitter. Create a <cfhttp> tag directly after the <cfset> block. Your code should be on or around line 7. The <cfhttp> tag should have the following attributes:
- url: https://api.twitter.com/1/statuses/user_timeline.xml?&count=5&screen_name=[Your Screen Name]
- method: get
- result: twitterFeed
Note: If you do not have a twitter screen name you can use the 'simonfree' screen name for now.
-
The code should look similar to this:
<cfhttp url="https://api.twitter.com/1/statuses/user_timeline.xml?&count=5&screen_name=simonfree" method="get" result="twitterFeed" />
-
Now that you have the feed data in the twitterFeed variable, you need to output it on the page. To do this, loop over the XML data that was received from Twitter and create a <cfloop> tag. Locate the comment that reads Twitter Output and create the <cfloop> tag before the first <li> tag. The <cfloop> tag should have the following properties:
- array: #xmlParse(twitterFeed.fileContent).statuses.status#
- index: feedItem
- Place the closing </cfloop> tag after the closing </li> tag.
-
Inside the <li> tags, place the following line of code:
#dateformat(feedItem.created_at.xmlText,'mm/dd/yyyy')# - #feedItem.text.xmlText#
-
Now wrap the entire <cfloop> block in a <cfoutput> tag so that the variables will be displayed to the user. Your code should look similar to this:
<ul> <cfoutput> <cfloop array="#xmlParse(twitterFeed.fileContent).statuses.status#" index="feedItem" > <li> #dateformat(feedItem.created_at.xmlText,'mm/dd/yyyy')# - #feedItem.text.xmlText# </li> </cfloop> </cfoutput> </ul>
- Open up the /www/contact.cfm page in your browser and notice that the 5 latest tweets are now displaying under the 'Latest Tweet' heading.