L
L
lam0er2018-03-15 00:09:26
PHP
lam0er, 2018-03-15 00:09:26

BITCOIN rate parser - RUB / JSON - PHP. How to make the code work?

I want to pull the 'price' parameter from JSON into the $price variable
There is a code:

<?php
$data = json_decode(file_get_contents("https://api.cryptonator.com/api/ticker/btc-rub"));
$price = $data->objects[0]->price;
echo $price;
?>

I get:
Notice: Undefined property: stdClass::$objects in C:\xampp\htdocs\market\index.php on line 5
Notice: Trying to get property of non-object in C:\xampp\htdocs\market\index. php on line 5
Testing on XAMP.
Who will tell?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick Sdk, 2018-03-15
@lidacriss

$price = $data->ticker->price;
just do var_dump($data) to see what we need to "pull"

object(stdClass)#2 (4) {
  ["ticker"]=>
  object(stdClass)#1 (5) {
    ["base"]=>
    string(3) "BTC"
    ["target"]=>
    string(3) "RUR"
    ["price"]=>
    string(15) "475260.76937314"
    ["volume"]=>
    string(13) "1206.63030836"
    ["change"]=>
    string(10) "3.44370048"
  }
  ["timestamp"]=>
  int(1521061861)
  ["success"]=>
  bool(true)
  ["error"]=>
  string(0) ""
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question