使用 PHP 和 JSON 解码 Google 金融获取股票价格

Get Stock Price with PHP & JSON Decode on Google FInance

我正在尝试从 Google 财经检索当前股票价格:

http://finance.google.com/finance/info?client=ig&q=AAPL

如何只提取价格 ("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;

哦,而且 json

中的键是小写 L (l) 而不是数字 (1)