使用PHP从Web提取数据

时间:2016-05-11 13:36:41

标签: php json decode

我试图从这个显示数字的网址(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,你能帮忙吗?

上述问题解答后,我想尝试使用此网址中的更大内容(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";
?>

2 个答案:

答案 0 :(得分:0)

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

$data = file_get_contents($url);

你的其他两个例子都适合我。

答案 1 :(得分:0)

似乎代码在使用XAMMP时工作正常但在我使用000webhost时却没有。怪异。