I've been using the Google Finance API to successfully gather some stock info. The problem is that after a call to http://www.google.com/finance/info?infotype=infoquoteall&q=[$tickerSymbol]
, the JSON that Google returns has //
added before it and therefore the string cannot be encoded using PHP's json_encode()
. The JSONLint JSON Validator confirms that the //
s are not valid. The obvious workaround is to strip the slashes from the beginning of the JSON. None-the-less, I am left wondering why Google is adding slashes to the JSON it is returning. Is there a purpose behind the extra slashes? Is this a quirk with PHP's json_encode()
when other languages would simply ignore the extra characters? Am I doing something incorrectly?
Here is an example of the result of a request for http://www.google.com/finance/info?infotype=infoquoteall&q=AAPL
with the leading slashes.
// [ {
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "340.65"
,"l_cur" : "340.65"
,"ltt":"4:00PM EST"
,"lt" : "Jan 18, 4:00PM EST"
,"c" : "-7.83"
,"cp" : "-2.25"
,"ccol" : "chr"
,"el": "345.20"
,"el_cur": "345.20"
,"elt" : "Jan 18, 5:45PM EST"
,"ec" : "+4.55"
,"ecp" : "1.34"
,"eccol" : "chg"
,"div" : ""
,"yld" : ""
,"eo" : ""
,"delay": ""
,"op" : "327.05"
,"hi" : "344.76"
,"lo" : "326.00"
,"vo" : "66.34M"
,"avvo" : "11.28M"
,"hi52" : "348.48"
,"lo52" : "190.25"
,"mc" : "313.75B"
,"pe" : "22.49"
,"fwpe" : ""
,"beta" : "1.38"
,"eps" : "15.15"
,"name" : "Apple Inc."
,"type" : "Company"
}
]
For those looking for a ready answer, here is a working example with PHP; The JSON is cleaned and transformed into an object. Values can easily be extracted.
The second is just to make it more awesome, it sends a push message you a PubNub channel when page is accessed (cron is your friend). PubNub message can easily be received via javascript hence live...
of course if you need something live updating all the time there are better options. I was just looking to update every 30min/60min.
I guess it's because google don't want you to work with that JSON, they recommend to use the Google Data API.