我正在使用Google财经的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 />";
?>
}
答案 0 :(得分: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);
答案 1 :(得分:0)
您必须删除&#39; //&#39;来自Google的respoce。
$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);