Php Google Finance JSON不能使用超过1个符号
我正在使用Google finance的PHP和JSON,使用 (这只是一个例子) 我的代码与上面提到的链接配合得很好(因为它只有一只我想买的股票,即GOOG) 但如果我想在最后再添加几个股票符号Php Google Finance JSON不能使用超过1个符号,php,json,stockquotes,google-finance,Php,Json,Stockquotes,Google Finance,我正在使用Google finance的PHP和JSON,使用 (这只是一个例子) 我的代码与上面提到的链接配合得很好(因为它只有一只我想买的股票,即GOOG) 但如果我想在最后再添加几个股票符号 { <?php <?php $url="http://finance.google.com/finance/info?client=ig&q=GOOG,AA
{
<?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 />";
?>
}
{
您必须从Google的respoce中删除“/”。
$quote=file\u get\u contents($url);
$json=str_replace('/[','[',$quote);
$data=substr($json,4,strlen($json)-5);
$results=json_decode($quote,true);
回声';
<?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);
打印(结果);
请从API发布JSON,您只需按照链接操作即可(此链接有效)(此链接与我的代码不兼容)它们都会生成所需的数据。它工作正常。您的问题是解析数据。问题是在调用json_decode之前修改数据的方式。特别是您对substr的调用。您正在切掉打开/关闭方括号,即数组的json。实际上,您应该删除前导的/
,并保留e> [
和]
。然后您可以使用$json\u输出[0]
,$json\u输出[1]访问数据
这是每个符号的数据。格式化代码块。更正拼写是的,我不久前就这么做了。但事实上,这并不是导致问题的原因…!修复了问题,现在一切都正常了!有一个小错误(但是大错误)把事情搞砸了。
$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);