我试图从谷歌财经retreive目前股价:在谷歌财经获得股票价格与PHP和JSON解码
我如何可以提取只是价格(“1”)?
<?php
$string = file_get_contents("http://finance.google.com/finance/info?client=ig&q=AAPL");
$json = json_decode($string, true);
$price = $json["1"];
echo $price;
?>
我试图从谷歌财经retreive目前股价:在谷歌财经获得股票价格与PHP和JSON解码
我如何可以提取只是价格(“1”)?
<?php
$string = file_get_contents("http://finance.google.com/finance/info?client=ig&q=AAPL");
$json = json_decode($string, true);
$price = $json["1"];
echo $price;
?>
的JSON返回的已被注释掉,因此json_decode()
不会做生意......你需要删除双斜线 - 我用explode()这样的:
<?php
$string = file_get_contents("http://finance.google.com/finance/info?client=ig&q=AAPL");
$arrMatches = explode('// ', $string); // get uncommented json string
$arrJson = json_decode($arrMatches[1], true)[0]; // decode json
$price = $assJson["l"];
echo $price;
哦,和键是小写L(l)不是数字(1)在json
在你的问题中打印$ json变量 – Akintunde007
...这应该回答你自己的问题 – Martijn