Solved

Coldfusion and YQL help

Posted on 2014-12-23
1
90 Views
Last Modified: 2015-01-27
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
Comment
Question by:cb_it
1 Comment
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 40515402
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question