Benjamin Benjamin - 6 months ago 9
PHP Question

Extracting data from web using PHP

Im trying to extract the file from this url (http://chainz.cryptoid.info/cbx/api.dws?q=getdifficulty) which shows a number but I can't seem to display it. Is there a problem in my code or is it in the site that I'm getting it from?

Here's my code:

<?php
$url = "http://chainz.cryptoid.info/cbx/api.dws?q=getdifficulty";
$data = json_decode(file_get_contents($url), true);
echo "$data";
?>


It only shows null, can you please help?

Once the above is being answered, I would like to try using a bigger content from this url (http://chainz.cryptoid.info/cbx/api.dws?q=lasttxs&a=5h9ZZpokW2P15yXr66MsHKknPvYmECvaDF)

Here's my code below:

<?php
$url = "http://chainz.cryptoid.info/cbx/api.dws?q=lasttxs&a=5h9ZZpokW2P15yXr66MsHKknPvYmECvaDF";
$data = json_decode(file_get_contents($url), true);
$content = $data[0]; //trying to get the 1st row
$hash = $content['hash'];
echo "$hash";
?>


The code is not showing anything. Can you please help in displaying the value using php? Thanks

This code though is working ok:

<?php
$url = "https://www.cryptonator.com/api/ticker/cbx-usd";
$data = json_decode(file_get_contents($url), true);
$ticker = $data['ticker'];
$latest_price = $ticker['price'];
echo "$latest_price";
?>

Answer

In the first example the data isn't returned as JSON, it's just a raw number. You can just use:

$data = file_get_contents($url);

Both of your other examples works fine for me.

Comments