0

我正在使用来自 Google Finance 的 PHP 和 JSON 来使用http://finance.google.com/finance/info?client=ig&q=GOOG获取实时股票更新/报价 (这只是一个示例)我的代码运行良好使用上述链接(因为它只有一只我想获得的股票,即 GOOG),但如果我尝试在最后添加更多股票代码

{
    <?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 />"; 
    ?>
}
4

2 回答 2

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);
于 2015-03-09T22:32:02.390 回答
0

您必须从 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);
于 2015-09-25T05:18:42.993 回答