Skip to content

Instantly share code, notes, and snippets.

@learncfinaweek
Created November 20, 2012 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save learncfinaweek/4121341 to your computer and use it in GitHub Desktop.
Save learncfinaweek/4121341 to your computer and use it in GitHub Desktop.
Document Handling - Hands On 23

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

  1. Open up the /www/contact.cfm file in your code editor.
  2. 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:
    Note: If you do not have a twitter screen name you can use the 'simonfree' screen name for now.
  3. 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" /> 
    
  4. 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
  5. Place the closing </cfloop> tag after the closing </li> tag.
  6. Inside the <li> tags, place the following line of code:
    #dateformat(feedItem.created_at.xmlText,'mm/dd/yyyy')# - #feedItem.text.xmlText#	
    
  7. 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>			
    
  8. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment