2017-09-02 95 views
0

我试图从谷歌财经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; 
?> 
+1

在你的问题中打印$ json变量 – Akintunde007

+0

...这应该回答你自己的问题 – Martijn

回答

1

的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

+0

哦,my.dont正则表达式当你不得不。 'substr($ string,3);' – Martijn

+0

@Martijn,你可以把程序员从perl中拿出来,但是你不能把perl从程序员那里拿出来!更新到爆炸()preg_match() –

+0

不,不要这样。数组函数,其中简单的字符串函数uffice也是不行的;)substr真的是你想要的 – Martijn