Hi Nitrous,
I tried to read and follow your guide to try to pull up some data, but I am getting errors in the response when I just try to get the ticker info.
array(3) { ["result"]=> string(5) "error" ["error"]=> string(20) "Chosen API not found" ["token"]=> string(13) "unknown_error" }
Basically, all I did was taking the example php template and put in my api key and secret (not shown here), and changed the base URL to
https://data.mtgox.com/api/2/
and changed the path to
but the var dump says Chosen API not found. I tested with many new api key/secret codes, but that appears not to be the source of the problem. Any insight is appreciated. Thank you.
Hi bittencoin,
You are correct that the API code and secret are not the problem, the problem is with this part of your code:
$prefix = '';
if (substr($path, 0, 2) == '2/') {
$prefix = substr($path, 2)."\0";
}
// generate the extra headers
$headers = array(
'Rest-Key: '.$key,
'Rest-Sign: '.base64_encode(hash_hmac('sha512', $prefix.$post_data, base64_decode($secret), true)),
);
Your prefix is empty in a normal scenario, instead, you should change this code to the following:
$prefix = $path;
if (substr($path, 0, 2) == '2/') {
$prefix = substr($path, 2);
}
// generate the extra headers
$headers = array(
'Rest-Key: '.$key,
'Rest-Sign: '.base64_encode(hash_hmac('sha512', $prefix."\0".$post_data, base64_decode($secret), true)),
);
I have made sure your prefix variable contains the path, and I've moved the null character into the rest-sign to make sure it's always included. This code worked for me after doing those fixes
Thank you Nitrous. I will test it out and report back. But by looking at the original template code from mtgox, I can see why it does not work now.
This block of code is suppose to check if the "path" is api version 2
$prefix = '';
if (substr($path, 0, 2) == '2/') {
$prefix = substr($path, 2)."\0";
}
the check is supposed to look for the "2/" at end of the string, but in this case the $path variable is "BTCUSD/money/ticker" and does not contain the "2/". If I am correct, the original code tried to look for the "2/" in the wrong string, it should be looking for the version in the base url
https://data.mtgox.com/api/2/
using the code
substr($path, -2, 2) == '2/'
. So if that is fixed, then you don't need to hard code the null character into the REST sign.
UPDATE:
Here is my fix of that block of code
$prefix = '';
$baseurl = 'https://data.mtgox.com/api/2/';
if (substr($baseurl, -2, 2) == '2/') {
$prefix = $path."\0";
}