0

I'm using PHP and JSON from Google finance to get real-time stock updates/quotes using http://finance.google.com/finance/info?client=ig&q=GOOG (this is just an example) My code works fine with above mentioned link (because it only has one stock that I want to get i.e GOOG) but if I try to add few more stock symbols at the end

{
    <?php                                                   
        <?php $url="http://finance.google.com/finance/info?client=ig&q=GOOG,AAPL,MAC "
         /* (here I'm trying to get data for 3 stocks (i.e GOOG, AAPL, MAC) 
         it generates the JSON but I'm unable to change it into proper Array.*/          
              $g_f_data= file_get_contents($url);
              $json = str_replace("\n", "", $g_f_data);
              $data = substr($json, 4, strlen($json) -5);

              $json_output = json_decode($data, true);
              echo"<pre>";
              print_r ($json_output);
              echo"</pre>";
             echo $json_output['t'],$json_output['l'],$json_output['cp'];echo "<br />"; 
    ?>
}
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Amit
  • 187
  • 2
  • 15
  • please post the JSON from the api –  Mar 09 '15 at 22:05
  • You can just simply follow the links http://finance.google.com/finance/info?client=ig&q=GOOG (this one works) http://finance.google.com/finance/info?client=ig&q=GOOG,AAPL,MAC (this one does not work properly with my code) Both of them generate the required data – Amit Mar 09 '15 at 22:17
  • it *works* fine. your issue is parsing the data –  Mar 09 '15 at 22:17
  • The issue is the way you are modifying the data before calling json_decode. Specifically your call to substr. You are chopping off the open/close square bracket which is JSON for array. Really you should be removing the leading `//` and leaving the `[` and `]` in there. Then you can access the data with `$json_output[0]`, `$json_output[1]` which is the data for each symbol. – Jonathan Kuhn Mar 09 '15 at 22:32
  • Formatted code block. Corrected spelling – Dijkgraaf Mar 09 '15 at 22:52
  • Let me simplify I want to generate json for the following [{"id": "304466804484872","t" : "GOOG","e" : "NASDAQ","l" : "568.85","l_fix" : "568.85","l_cur" : "568.85","s": "2","ltt":"4:05PM EDT","lt" : "Mar 9, 4:05PM EDT","lt_dts" : "2015-03-09T16:05:45Z","c" : "+1.16","c_fix" : "1.16","cp" : "0.21","cp_fix" : "0.21","ccol" : "chg","pcls_fix" : "567.685","el": "568.29","el_fix": "568.29","el_cur": "568.29","elt" : "Mar 9, 5:08PM EDT","ec" : "-0.56","ec_fix" : "-0.56","ecp" : "-0.10","ecp_fix" : "-0.10","eccol" : "chr","div" : "","yld" : ""}] It only returns Array( [0] => Array:- (xzy ) ) – Amit Mar 09 '15 at 22:56
  • Even after removing call to substr() and chopping of those "//" The problem is that, it is only generating $json_output[0] (i.e only array for GOOG in this case) and not generating Array for other symbols . Offtopic: BTW thanks for your quick reply, This is my first thread at SOF and I wish to go long way. – Amit Mar 09 '15 at 23:13
  • i posted an answer below, its basically what you have, it works fine (tested). –  Mar 10 '15 at 01:18
  • I had a similar problem [here](http://stackoverflow.com/questions/34228761/using-google-api-and-json-to-retrieve-stock-info). – SanLuka Dec 14 '15 at 15:59

2 Answers2

0
<?php 

$url="http://finance.google.com/finance/info?client=ig&q=GOOG,AAPL,MAC";

$quote= file_get_contents($url);

$json = str_replace("\n", "", $quote); //clean
$data = substr($json, 4, strlen($json) -5); 
$json_output = json_decode($data, true);  
echo '<pre>';
print_r($json_output);
0

You have to remove the '//' from the respoce from Google.

$quote= file_get_contents($url);

$json = str_replace('// [', ' [', $quote);
$data = substr($json, 4, strlen($json) -5); 
$results = json_decode($quote, true);
echo '<pre>';
print_r($results);
  • Yes I've done that a while ago.. but actually it wasn't something that caused the problem...! fixed the issue and it all works fine now.! There was a minor (yet major) error that messed it all. – Amit Sep 30 '15 at 06:18