Trying to get the single value from google finances api. I can retrieve the data perfectly however when I try to echo out a single value it doesn't seem to work. Can anyone help?
My code is:
$request = wp_remote_get('http://www.google.com/finance/info?q=NASDAQ%3aGOOG', $args );
$price = wp_remote_retrieve_body( $request );
print_r($price);
The output is:
// [
{
"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" : ""
}
]
I've tried echoing out the single value, adding a foreach statement and then echoing out the value based on 'l_fix' and the 'id', and also tried splitting the string up but it wouldn't work.
Thanks
Do it:
$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 APIs (in this specific case) return JSON with two first chars ('//').
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With