我从特定网站获取NULL检索JSON

时间:2013-03-04 12:23:13

标签: php json

我想从here中检索JSON数据:

{

    "input_address": "1BeE32K9FxvrnBNeuKwdyM26vZ4GsggqZG",
    "callback_url": "http://example.com",
    "fee_percent": 1.5,
    "destination": "12Za1i1zhYTCeHWBg8yJb72BeEGQVEuMdT"

}

...如果你把它放在浏览器上,你会看到JSON数据格式正确。没问题。

但是,当我尝试使用标准脚本使用PHP检索数据时:

<?php
$url = 'https://blockchain.info/api/receive?method=create&format=plain&anonymous=true&address=12Za1i1zhYTCeHWBg8yJb72BeEGQVEuMdT&callback=http%3A%2F%2Fexample.com';
$JSON = file_get_contents($url);
$data = json_decode($JSON);
var_dump($data);
echo 'url: '.$url;
?>

......我没有数据; var_dump写入“NULL”(您可以在http://bitstamina.com/theamazinghat/thehat.php处测试以前的代码)。然而,如果我尝试任何其他返回JSON数据的URL,我的代码就可以正常工作。

我做错了什么?它必须是我的一个非常愚蠢的错误,或者网站blockchain.info阻止我的网站发出请求。

2 个答案:

答案 0 :(得分:1)

你的php配置中是否启用了allow_url_fopen

如果没有,您将收到警告 - 您是否显示错误/警告?

检查错误,在脚本顶部添加以下行。

error_reporting(E_ALL);
ini_set('display_errors', true);

然后您可能会看到为什么file_get_contents()不起作用。

答案 1 :(得分:1)

我认为这是由于跨域不匹配造成的。将您的页面更改为https或使用http调用。     http://blockchain.info/api/receive?method=create&format=plain&anonymous=true&address=12Za1i1zhYTCeHWBg8yJb72BeEGQVEuMdT&callback=http%3A%2F%2Fexample.com

这很有效。