I'm not sure why am I getting this behavior? I'm trying to implement the Blockchain.com web APIs in my PHP script. Their documentation states that I should use file_get_contents
PHP function to query it, which I do as such:
$xpub = "xpub123455";
$callback_url = "https://example.com";
$APIKey = "12345";
$url = "https://api.blockchain.info/v2/receive?xpub=".urlencode($xpub).'&callback='.urlencode($callback_url).'&key='.urlencode($APIKey);
//echo("URL: ".htmlentities($url)."<br><br>");
$res = @file_get_contents($url);
if($res)
{
echo("RES: ".htmlentities(var_export($res, true)));
}
else
{
$err = error_get_last();
echo("err: ".($err && isset($err['message']) ? $err['message'] : "-"));
}
In that case file_get_contents
returns false
and if I then call error_get_last
, I get the following error:
err: file_get_contents(-url-): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized
But if I just copy the URL that is being passed to file_get_contents
and paste it into the address bar for Chrome:
https://api.blockchain.info/v2/receive?xpub=xpub123455&callback=https%3A%2F%2Fexample.com&key=12345
it returns a valid JSON data:
Why are these two outputs different? And how can I get a JSON response from file_get_contents
?