Leading slashes in JSON from the Google Finance API

I use the Google Finance API to successfully collect stock information. The problem is that after the http://www.google.com/finance/info?infotype=infoquoteall&q=[$tickerSymbol]JSON call that Google returns, it adds //before that, and therefore the string cannot be encoded using PHP json_encode(). JSONLint JSON Validator confirms that it is //invalid. The obvious workaround is to remove the slashes from the beginning of JSON. However, I am left wondering why Google is adding slashes to the JSON that it returns. Is there a target behind an extra slash? Is this a quirk with PHP json_encode()when other languages ​​just ignore extra characters? Am I doing something wrong?

Here is an example of a query result http://www.google.com/finance/info?infotype=infoquoteall&q=AAPLwith 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"
}
]
+5
source share
2 answers

For those looking for a ready-made answer, here is a working example with PHP; JSON is cleared and converted to an object. Values ​​can be easily retrieved.

Secondly, to make it more intimidating, it sends a push message to you on the PubNub channel when accessing the page (cron is your friend). A PubNub post can be easily received via javascript, hence live ...

<?php

  //Obtain Quote Info
  $quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXDB:DAX');

  //Remove CR from ouput - make it one line
    $json = str_replace("\n", "", $quote);

  //Remove //, [ and ] to build qualified string  
    $data = substr($json, 4, strlen($json) -5);

  //decode JSON data
    $json_output = json_decode(utf8_decode($data));

  // get the last price
    $last = $json_output->l;

  //Output Stock price .
  echo 'DAX: ' . $last; 


//////////////////////////////
//  send it through pubnub  //
//////////////////////////////

require_once('Pubnub.php');

// Publish and Subscribe Keys 
$publish_key   = 'demo';
$subscribe_key = 'demo';
$subscribe_key = false;

// Create Pubnub Object
$pubnub = new Pubnub( $publish_key, $subscribe_key, $secret_key );

// Publish !
$channel = 'quoteTheDax';

$timestamp = $pubnub->time();
$pubish_success = $pubnub->publish(array(
    'channel' => $channel,
    'message' => array("last" => $last2, "ts" => $timestamp)
));

//Boom its send to ppl subscribed to this channel arround the world
?>

Of course, if you need to update something alive all the time, there are better options. I just wanted to update every 30 minutes / 60 minutes.

+5
source

, Google , JSON, API Google.

+2

All Articles