0

I am trying to pull values from the following url: https://api.binance.com/api/v1/ticker/allPrices

What I want to accomplish: Obtain all the "symbol" values printed in the website (one under the other)

The result I am looking for, would be:

ETHBTC
LTCBTC
...

This is what I have done so far:

<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);

for ($i=0; $i < count($json_data); $i++) { 
    # code...
    print_r($json_data);
}
?>

The result it brings is the entire data (the symbols, prices, plus other irrelevant data).

Thanks in advance.

3 Answers 3

2

You can use foreach loop, then echo the value by symbol key

$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);

foreach ($json_data as $value) {
    echo $value["symbol"] . '<br>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Erwin. How would it work if instead of all the full values list. I would just like to get one item of the array? example, the first array item. Something like echo $value["symbol[0]"] ?
if you just want one value, you can skip the loop and print it directly. E.g. echo $json_data[0]["symbol"]; wherein, [0] means the first set of array inside $json_data
2

This ought to do it:

<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);

for ($i=0; $i < count($json_data); $i++) {
    echo $json_data[$i]['symbol'] . "\n";
}
?>

1 Comment

Hi Chris, How would it work if instead of having all the list values displayed in the website, I just wanted to have specific list values displayed? Example: I want to have displayed the array item that has the symbol "LTCBTC"
0
json_data["symbol"] 

instead in the print

2 Comments

Like this? print_r($json_data["symbol"]);
does not work Notice: Undefined index: symbol in line 26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.