使用 PHP 从网络中提取数据

Extracting data from web using PHP

我正在尝试从此 url (http://chainz.cryptoid.info/cbx/api.dws?q=getdifficulty) 中提取文件,它显示了一个数字,但我似乎无法显示它。我的代码有问题还是我从中获取代码的网站有问题?

这是我的代码:

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

只显示null,请问可以帮忙吗?

以上问题得到解答后,我想尝试使用此 url (http://chainz.cryptoid.info/cbx/api.dws?q=lasttxs&a=5h9ZZpokW2P15yXr66MsHKknPvYmECvaDF)

中的更大内容

下面是我的代码:

<?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";
?>

代码没有显示任何内容。你能帮忙用php显示这个值吗?谢谢

这段代码虽然工作正常:

<?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";
?>

在第一个示例中,数据未作为 JSON 返回,它只是一个原始数字。您可以只使用:

$data = file_get_contents($url);

你的其他两个例子对我来说都很好。

似乎代码在使用 XAMPP 时工作正常,但在我使用 000webhost 时却不行。奇怪。