I have an API I’m trying to integrate into my web application (a bitcoin website), which is written in php. The API has a php library, and they communicate through JSON. On my server I have 3 scripts: the script that calls the function I require (blockTest.php), the script that has all my credentials (i.e. API key, pin, API version; cred.php), and the library for the block.io API which has all their methods (block_io.php).
My first php script has the following code:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once 'block_io.php';
require_once 'cred.php';
$addressInfo = $block_io_btc->get_my_addresses(array())
?>
I’ve set up the development environment on my localhost and these scripts work fine. I can make all the calls to the block.io API and get the appropriate data. However, when I upload my scripts to my shared hosting i get the following errors:
Fatal error: Uncaught exception 'Exception' with message 'Failed: '
in/home/sites/public_html/vendor/block_io.php:124 Stack trace: #0
/home/sites/public_html/vendor/block_io.php(65): BlockIo-
>_request('get_my_addresse...', Array) #1
/home/sites/public_html/vendor/blockTest.php(14): BlockIo-
>__call('get_my_addresse...', Array) #2
/home/sites/public_html/vendor/blockTest.php(14): BlockIo-
>get_my_addresses(Array) #3 {main} thrown in
/home/sites/public_html/vendor/block_io.php on line 124
Step by step, this corresponds to the following lines of code in block_io.php(65):
$response = $this->_request($name, $args);
and in blockTest.php(14):
$addressInfo = $block_io_btc->get_my_addresses(array())
and in block_io.php(124):
if ($json_result->status != 'success') { throw new Exception('Failed: ' .
$json_result->data->error_message); }
// Spit back the response object or fail
return $result ? $json_result : false;
}
The error is that status != success. This is the http status, but there is no data->error_message. I went onto their website to play around with their API. If I enter my details correctly I get the following output:
As you can see the response is a 200: success with the correct credentials. When I change the credentials by entering the wrong API key I get the following:
In the latter case there is a 404: error. In my production environment I do not get any data->error_message. This lead me to believe that the php version my Web host uses might be creating the problem with json_decode(), since block_io.php line 121-130 is this:
$json_result = json_decode($result);
if ($json_result->status != 'success') { throw new Exception('Failed: ' .
$json_result->data->error_message); }
// Spit back the response object or fail
return $result ? $json_result : false;
}
I’ve made sure my php version on MAMP is the same as my web host (5.5). Still nothing (remember the exact same script works fine on localhost). I checked the http status of my script here and all is good:
(reputation isn't good enough to post more than 2 links, but my server is responding with a 200:success status)
So…. I really don’t know what the problem is. The phpinfo() for my web host shows that they have JSON enabled and as far as I can tell they do not have magic_quotes = on in their php.ini. If they did have magic_quotes on I attempted to stripslashes() on the $json_result & separately on $result in block_io.php but to no avail. Now I really don’t know what else to try!