尝试从Google Finances API获取单一价值。我可以完美地检索数据,但是当我尝试回显单个值时,它似乎不起作用。有人可以帮忙吗?

我的代码是:

  $request  = wp_remote_get('http://www.google.com/finance/info?q=NASDAQ%3aGOOG', $args );
                $price    = wp_remote_retrieve_body( $request );

                print_r($price);

输出为:
    // [
    {
    "id": "304466804484872"
    ,"t" : "GOOG"
    ,"e" : "NASDAQ"
,"l" : "533.75"
,"l_fix" : "533.75"
,"l_cur" : "533.75"
,"s": "2"
,"ltt":"4:01PM EST"
,"lt" : "Dec 2, 4:01PM EST"
,"lt_dts" : "2014-12-02T16:01:56Z"
,"c" : "-0.05"
,"c_fix" : "-0.05"
,"cp" : "-0.01"
,"cp_fix" : "-0.01"
,"ccol" : "chr"
,"pcls_fix" : "533.8"
,"el": "533.00"
,"el_fix": "533.00"
,"el_cur": "533.00"
,"elt" : "Dec 2, 7:59PM EST"
,"ec" : "-0.75"
,"ec_fix" : "-0.75"
,"ecp" : "-0.14"
,"ecp_fix" : "-0.14"
,"eccol" : "chr"
,"div" : ""
,"yld" : ""
}
]

我试过回显单个值,添加一个foreach语句,然后回显基于'l_fix'和'id'的值,还尝试将字符串分割开,但它不起作用。

谢谢

最佳答案

做到:

 $request  = wp_remote_get('http://www.google.com/finance/info?q=NASDAQ%3aGOOG', $args );
 $data = wp_remote_retrieve_body( $request );
 $data = str_replace('//','',$data);
 $data = json_decode($data);
 $price = $data[0]; // $price = array_shift($data);
 print $price->l_fix .....

Google API(在这种情况下)返回带有两个首字符('//')的JSON。

关于php - WordPress获取JSON数据单个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27270725/

10-11 11:42