peterhuynh Posted March 5, 2015 Share Posted March 5, 2015 (edited) I'm two months into learning php - my first programming language. I'm thankful for all the support I've received from this board. This script is supposed to retrieve a balance from a bitcoin exchange. (Note: it is a dummy account, so no money actually in it.) Here is the script: <?php date_default_timezone_set("EST"); $nonce = date("Y-m-d H:i:s e"); $secret = 'banana'; $public = 'aGjnonYmA3gEQv6JzgEwcyR7cpa3ARxMlTUweXBer3xM'; $private = '2D0ndrWpzWasfDNIWfvKNizeE2cxmqJhQf31ubf0boyd'; $data = array( "t" => $nonce, "secret" => $secret ); $hash = hash_hmac('sha256', json_encode($data), $private); $headers = array( 'Accept: application/json', 'X-Auth: '. $public, 'X-Auth-Hash: '. $hash ); $header_data = json_encode($headers); $ch = curl_init('https://sandbox.cointrader.net/api4/account/balance'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($header_data)) ); $result = curl_exec($ch); curl_close($ch); var_dump($result); ?> The POST parameters are "time" and "secret". Time is supposed to be formatted a certain way, which I've done, and the secret is the word 'banana'. The exchange provides me with this message: string(205) "{"success":false,"message":"Unauthorized","data":{"errorCode":"401","message":"Headers or JSON POST missing tonce, corrupt or incomplete. Data: {\"t\":\"2015-03-04 22:33:57 EST\",\"secret\":\"banana\"}"}} " Edited March 5, 2015 by peterhuynh Quote Link to comment Share on other sites More sharing options...
CroNiX Posted March 6, 2015 Share Posted March 6, 2015 I don't think the postfields is supposed to be a json string. It should be a querystring Try curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&')); Quote Link to comment Share on other sites More sharing options...
peterhuynh Posted March 6, 2015 Author Share Posted March 6, 2015 I don't think the postfields is supposed to be a json string. It should be a querystring Try curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&')); AuthenticationAuthentication is accomplished with a private/public key combination that utilizes HMAC-SHA256. Parameter names AND values (total quantity, price, etc.) are passed as strings in JSON format in POST body of the request. To use the API users must first generate a key pair in the User Profile API section of the site. Multiple keypair combinations can be generated per account. NOTE: All POST parameters in JSON (payload) must be sent in string format (quoted). All connections must be be transmitted over HTTPS/SSL. Quote Link to comment Share on other sites More sharing options...
Solution peterhuynh Posted March 6, 2015 Author Solution Share Posted March 6, 2015 I FIGURED IT OUT!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.