How to get JSON data with PHP -
i new php , trying 1 value json dictionary. seems simple (at to the lowest degree me) having problem it. looked @ php documentation json_decode
, looked @ "parsing json info remote server" didn't wanted. returned dictionary item wanted 0 spit out json file. looked @ "get info json php" , returns nothing.
json example
{ "ask": 344.28, "bid": 343.89, "last": 343.97 }
first try
$jsoncontent=file_get_contents("https://api.bitcoinaverage.com/ticker/global/usd");` $priceusd=json_decode($json-content); $lastusd=$priceusd['last']; echo "decoded json:\n"; echo "$priceusd\n"; echo "raw json:\n"; echo "$jsoncontent\n";
second try
$result=file_get_contents("https://api.bitcoinaverage.com/ticker/global/usd"); $result = iconv('utf-16', 'utf-8', $result); $json = json_decode($result); echo $json->last;
all want value of 1 of dictionary items in json @ url.
thanks!
assuming $data
contains returned server, do:
$object = json_decode($data); $last = $object->last;
or:
$array = json_decode($data, true); $last = $array['last'];
when leave out sec argument json_decode
, returns object, , access part want object property. if give sec argument true
, returns associative array, , access element using array notation.
php json dictionary
No comments:
Post a Comment