Created
August 15, 2013 21:08
-
-
Save onel0p3z/6244922 to your computer and use it in GitHub Desktop.
Simple proof-of-concept ... how to consume API data with CFML ... yes, you read right ... C F M L ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfouput> | |
<form method="POST"> | |
<input name="zipcode" type="number" placeholder="ZIP CODE" size="5" value="#(StructKeyExists(FORM,"zipcode") ? '#FORM.ZIPCODE#' : '')#"/> | |
<input type="submit" value="Search" /> | |
</form> | |
<cfif StructKeyExists(FORM,"zipcode") AND FORM.zipcode NEQ "" AND Len(FORM.zipcode) EQ 5> | |
<cfset theURL = "http://api.zippopotam.us/us/#FORM.zipcode#"> | |
<cfhttp url="#theURL#" method="get" result="response"></cfhttp> | |
<cfset temp = #DeserializeJSON(response.filecontent)#> | |
<cfif response.responseheader.status_code eq 200> | |
<p>Your ZIP: <b>#FORM.zipcode#</b></p> | |
<p>API ZIP: <b>#temp['post code']#</b></p> | |
<p>Found <b>#ArrayLen(temp.places)#</b> Places from API</p> | |
<cfif ArrayLen(temp.places) NEQ 1 > | |
<cfloop from="1" to="#ArrayLen(temp.places)#" index="ai"> | |
<cfset tempst = #temp.places[ai]# > | |
<ul><b><i>#ai#</b></i> | |
<li>State: <b>#tempststate# - #tempst['state abbreviation']#</li> | |
<li>City: #tempst['place name']#</li> | |
<li>Latitude: #tempst.latitude#</li> | |
<li>Longitud: #tempst.longitude#</li> | |
</ul> | |
</cfloop> | |
<cfelse> | |
<cfset tempst = #temp.places[1]# > | |
<ul> | |
<li>State: <b>#tempst.state# - #tempst['state abbreviation']#</b></li> | |
<li>City: <b>#tempst['place name']#</b></li> | |
<li>Latitude: #tempst.latitude#</li> | |
<li>Longitud: #tempst.longitude#</li> | |
</ul> | |
</cfif> | |
<cfelse> | |
<h1>NOT FOUND</h1> | |
<p>Your ZIP: <b>#FORM.zipcode#</b></p> | |
</cfif> | |
</cfif> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment