gpt4 book ai didi

php - WordPress获取json数据单值

转载 作者:行者123 更新时间:2023-12-02 03:30:20 25 4
gpt4 key购买 nike

尝试从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com