nicehash/rest-clients-demo

Exchange Order API

Closed this issue · 4 comments

I've been trying to call the Exhange Order API but unluckily I can't succeed. I don't know what is the problem with the code below. I can get the Wallet balance and the Server time but not on POSTING new Order..

`<?
echo '

';
$url_root = "https://api2.nicehash.com";
$org_id = "";
$api_key = "";
$api_secret = "";

//get current time
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, $url_root."/api/v2/time");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);

$time = json_decode($result, true)['serverTime'];
echo "server time: ".$time."\n";


$nonce     = uniqid();
$path      = "/exchange/api/v2/order";
//$qs        = "market=BTCUSDC&side=SELL&type=LIMIT&quantity=.00012&price=35000&secQuantity=.00012";
$qs ="";

$postbody  = json_encode(array(
	"market"=>"BTCUSDC",
	"side"=>"SELL",
	"type"=>"LIMIT",
	"quantity"=>0.00012,
	"price"=>35000,
	"minSecQuantity"=>'',
	"secQuantity"=>0.00012,
	"minQuantity"=>''
	)
);

print_r($postbody);
$postlen   = strlen($postbody);

//$signature = $api_key."\x00".$time."\x00".$nonce."\x00"."\x00".$org_id."\x00"."\x00"."POST"."\x00".$path."\x00".$qs;
//$signature = $api_key."\x00".$time."\x00".$nonce."\x00"."\x00".$org_id."\x00"."\x00"."POST"."\x00".$path."\x00".$qs."\x00".$postbody;
$signature = $api_key."\x00".$time."\x00".$nonce."\x00"."\x00".$org_id."\x00"."\x00"."POST"."\x00".$path."\x00".$postbody;
$signhash  = hash_hmac('sha256', $signature, $api_secret);


//$postlen   = strlen($qs);

//echo $postlen .' <br>';

$headers = array(
	"X-Time: {$time}",
	"X-Nonce: {$nonce}",
	"X-Organization-Id: {$org_id}",
	"X-Request-Id: {$nonce}",
	"X-Auth: {$api_key}:{$signhash}",
	"Content-Length: {$postlen}",
);
echo ' <br>headers: <br>';
print_r ($headers);

$curl = curl_init($url_root.$path);
//curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postbody); 


//curl_setopt($curl, CURLOPT_URL, $url_root.$path.'?'.$qs);
//curl_setopt($curl, CURLOPT_URL, $url_root.$path);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);

if(curl_errno($curl)){
    echo 'Err : ' . curl_error($curl) .'<br>';
}
print_r($result);

curl_close($curl);
//if(!$result){echo "Connection Failure";}


//print_r($result);

$info = json_decode($result, true);
echo '<br>info: <br>';
print_r($info);


echo '-- END -- <br>';

?>`

Fox exchange order post body is not needed and for SELL limit order secQuantity is ignored. From the code above everything looks ok. What is the error you receive from the system?

FYI: To test your script and without loosing your money, you can use https://test.nicehash.com

Fox exchange order post body is not needed and for SELL limit order secQuantity is ignored. From the code above everything looks ok. What is the error you receive from the system?

FYI: To test your script and without loosing your money, you can use https://test.nicehash.com

I'm not also receiving any error from the API only blank response.

I tried to comment the this line curl_setopt($curl, CURLOPT_POSTFIELDS, $postbody); but when I tried to run error response that "Empty reply from server"

bl4z commented

if adding

curl_setopt($curl, CURLOPT_VERBOSE, true);

you can see NH servers are not happy with post content length responding with

< HTTP/1.1 411 Length Required

so just add Content-Length: 0 to your headers should do the trick since all params are in query string

$headers = array( "X-Time: {$time}", "X-Nonce: {$nonce}", "X-Organization-Id: {$org_id}", "X-Request-Id: {$nonce}", "X-Auth: {$api_key}:{$signhash}", "Content-Length: 0" );

if adding

curl_setopt($curl, CURLOPT_VERBOSE, true);

you can see NH servers are not happy with post content length responding with

< HTTP/1.1 411 Length Required

so just add Content-Length: 0 to your headers should do the trick since all params are in query string

$headers = array( "X-Time: {$time}", "X-Nonce: {$nonce}", "X-Organization-Id: {$org_id}", "X-Request-Id: {$nonce}", "X-Auth: {$api_key}:{$signhash}", "Content-Length: 0" );

Thank you bl4z, it's now working....