2017-07-07 138 views
-2

你好这个问题具有相似性this postPHP获取谷歌财务数据

但不幸的是,这不是用PHP工作。

header('charset=utf8'); 
$rr=file_get_contents("https://www.google.com/finance/info?q=NSE:TCS"); 
$json = json_decode($rr, true); 
    echo '<pre>' . print_r($json, true) . '</pre>'; 

这只显示空白。没有数据。

仅使用file_get_contents给出了类似的内容。

// [ { "id": "784961" ,"t" : "TCS" ,"e" : "NSE" ,"l" : "2,437.00" ,"l_fix" : 
"2437.00" ,"l_cur" : "₹2,437.00" ,"s": "0" ,"ltt":"3:49PM GMT+5:30" ,"lt" : 
"Jul 20, 3:49PM GMT+5:30" ,"lt_dts" : "2017-07-20T15:49:12Z" ,"c" : "-12.60" 
    ,"c_fix" : "-12.60" ,"cp" : "-0.51" ,"cp_fix" : "-0.51" ,"ccol" : "chr" 
    ,"pcls_fix" : "2449.6" } ] 

如果json_decode不起作用,那么我有什么选择以其他方式做到这一点。 我会以表格格式或每个份额显示数据。

full quote link 
http://www.google.com/finance/info?infotype=infoquoteall&q=NSE:TCS" 

回答

1

我一开始没有正确理解问题。 基本上,谷歌在使用它之前返回无效JSON,需要被 “固定”:

$rr = trim(str_replace(array("\r", "\n", "//"), '', $rr)); 
$json = json_decode($rr); 

结果:

array(1) { [0]=> object(stdClass)#1 (16) { ["id"]=> string(6) "784961" ["t"]=> string(3) "TCS" ["e"]=> string(3) "NSE" ["l"]=> string(8) "2,437.00" ["l_fix"]=> string(7) "2437.00" ["l_cur"]=> string(15) "₹2,437.00" ["s"]=> string(1) "0" ["ltt"]=> string(15) "3:49PM GMT+5:30" ["lt"]=> string(23) "Jul 20, 3:49PM GMT+5:30" ["lt_dts"]=> string(20) "2017-07-20T15:49:12Z" ["c"]=> string(6) "-12.60" ["c_fix"]=> string(6) "-12.60" ["cp"]=> string(5) "-0.51" ["cp_fix"]=> string(5) "-0.51" ["ccol"]=> string(3) "chr" ["pcls_fix"]=> string(6) "2449.6" } } 
+0

我是有在 “//” 风格:)同样的想法。如何访问个人领域?例如,股票报价“l” – mimi

+0

我试过这样做echo $ arr [0] - > l;我得到错误未定义变量:arr,试图获取非对象2错误的属性。 – mimi

+1

@imimi上面的例子应该是'$ json [0] - > l' –