0
votes

I am trying to get the v3 api working in PHP, can you please help me work out what I am doing wrong?

The current response I am getting is "UNAUTHORIZED", (the old v1.1 works with the api key and secret I have).


    $timestamp = time()*1000;
    $url = "https://api.bittrex.com/v3/balances";
    $method = "GET";
    $contentHash = hash('sha512', '');
    $auth = $timestamp . $url . $method . $contentHash;
    $sign=hash_hmac('sha512',$auth,$apisecret);
    $headers = array (
        'Api-Key' => $apikey,
        'Api-Timestamp' => $timestamp,
        'Api-Content-Hash' => $contentHash,
        'Api-Signature' => $sign,
    );

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $execResult = curl_exec($ch);
    curl_close($ch);
    $obj = json_decode($execResult, TRUE);

3
I have it working correctly below, but am unable to get it working without guzzle. Can anyone convert the answer below into a simple curl_setopt method above please?Gary Lee

3 Answers

2
votes

This is my working code:

- for get balances:

$timestamp = time()*1000;
$url = "https://api.bittrex.com/v3/balances";
$method = "GET";
$content = "";
$subaccountId = "";
$contentHash = hash('sha512', $content);
$preSign = $timestamp . $url . $method . $contentHash . $subaccountId;
$signature = hash_hmac('sha512', $preSign, $apisecret);

$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Api-Key: ".$apikey."",
"Api-Signature: ".$signature."",
"Api-Timestamp: ".$timestamp."",
"Api-Content-Hash: ".$contentHash.""
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$execResult = curl_exec($ch);
curl_close($ch);
echo $execResult;

- for create a new order:

$timestamp = time()*1000;
$url = "https://api.bittrex.com/v3/orders";
$method = "POST";

$content = '{
  "marketSymbol": "BTC-USDT",
  "direction": "BUY",
  "type": "LIMIT",
  "quantity": "0.00276225",
  "limit": "7226.00306482",
  "timeInForce": "GOOD_TIL_CANCELLED"
}';

$subaccountId = "";
$contentHash = hash('sha512', $content);
$preSign = $timestamp . $url . $method . $contentHash . $subaccountId;
$signature = hash_hmac('sha512', $preSign, $apisecret);

$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Api-Key: ".$apikey."",
"Api-Signature: ".$signature."",
"Api-Timestamp: ".$timestamp."",
"Api-Content-Hash: ".$contentHash.""
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
$execResult = curl_exec($ch);
curl_close($ch);

echo $execResult;
0
votes

    use Http\Promise\Promise;
    use GuzzleHttp\Client;
    use GuzzleHttp\Exception\RequestException;
    use GuzzleHttp\HandlerStack;
    use GuzzleRetry\GuzzleRetryMiddleware;
    use GuzzleHttp\Psr7\Request;
    use guzzle\guzzle;
    use Monolog\Logger;
    include '../vendor/autoload.php';

    function requestTimestamp()
    {
     list($usec, $sec) = explode(' ', microtime());
     return (int) ((int) $sec * 1000 + ((float) $usec * 1000));
    }

    $apiKey = 'YOUR_API_KEY';
    $apiSecret = 'YOUR_SECRET_KEY';
    $url = 'https://api.bittrex.com/v3/balances';

    $method = 'GET';
    $content = '';

    $timestamp = requestTimestamp();
    $contentHash = hash('sha512', $content);
    $preSignature = ($timestamp.$url.$method.$contentHash);

    $signature = hash_hmac('sha512', $preSignature, $apiSecret);

    $headers = ['Api-Timestamp'=> $timestamp, 'Api-Key'=> $apiKey, 'Api-Content-Hash' => $contentHash, 'Api-Signature' => $signature, 'Accept'=> 'application/json', 'Content-Type'=> 'application/json'];

    $client = new \GuzzleHttp\Client();
    $response = $client->request($method, $url, ['headers' => $headers], json_encode($content));

    echo $response->getBody();

0
votes

You might try adding a Content-Type header set to application/json.

$headers = array (
    'Api-Key' => $apikey,
    'Api-Timestamp' => $timestamp,
    'Api-Content-Hash' => $contentHash,
    'Api-Signature' => $sign,
    'Content-Type' => "application/json"
);