[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 100
  • Last Modified:

Coldfusion and YQL help

Just trying to build a small stock widget for an intranet. I have coldfusion installed here. I'm trying to use YQL for this, if there is something easier let me know.

I enter in a basic query such as select * from yahoo.finance.stocks where symbol="yhoo"

The YQL console then generates a xml or json link. Should I use cfhttp? How would I show the output of this on an intranet page?

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.stocks%20where%20symbol%3D%22yhoo%22&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
0
cb_it
Asked:
cb_it
1 Solution
 
_agx_Commented:
Yes, you need to use cfhttp and do a call to that url.  Looks like it returns XML by default. I'd suggest appending "&format=json" so you get back JSON instead. It's a bit simpler to work with than XML. Just use deserializeJSON(..) to convert the response string into CF arrays and structures.

<!--- Call url with "&format=json" to get back a JSON string --->
<cfset yourURL = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.stocks%20where%20symbol%3D%22yhoo%22&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys">
<cfhttp method="get" url="#yourURL#&format=json">

<!--- if successful --->
<cfif cfhttp.statuscode contains "200">
	<!--- deserialize JSON into CF structures and arrays --->
	<cfset response = deserializeJSON(cfhttp.fileContent)>
	<!--- for demo purposes, extract "results" key --->
	<cfset result = response.query.results>
	<cfdump var="#result#" label="Show response.query.results structure">
	
	<!--- do something with response --->
	<cfoutput>
		FullTimeEmployees #result.stock.FullTimeEmployees#<br>
		Industry #result.stock.Industry#<br>
		Sector #result.stock.Sector#<br>
		Start #result.stock.Start#<br>
		Sector #result.stock.Sector#<br>
		Symbol #result.stock.Symbol#<br>
	</cfoutput>
<cfelse>
    <!--- do something on error --->
	Error. Unable to retrieve url
	<cfdump var="#cfhttp#">
</cfif>

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now