poloniex futures api php



我无法将数据POST到Poloniex Futures API。而GET运行良好。

POST返回400005[msg]=>无效的PF-API-SIGN

这是config.php 的代码

global $baseurl;
$baseurl = "https://futures-api.poloniex.com";
global $api_key;
$api_key = "XXXXX";
global $api_secret;
$api_secret = "XXXXX";
global $api_passphrase;
$api_passphrase = "XXXXXX";

function signature($request_path = '', $body = '', $timestamp = false, $method = 'POST') {

$body = is_array($body) ? json_encode($body) : $body; // Body must be in json format
$timestamp = $timestamp ? $timestamp : time() * 1000;
$what = $timestamp . $method . $request_path . $body;
return base64_encode(hash_hmac("sha256", $what, $api_secret, true));
}

获取票证数据的工作代码ticker.php 代码

include_once "config.php";

$endpoint = "/api/v1/position?symbol=BTCUSDTPERP";
$url = "https://futures-api.poloniex.com".$endpoint;
$sigs = signature($endpoint,'',true,"GET");
$now = time() * 1000;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
"PF-API-SIGN: $sigs",
"PF-API-TIMESTAMP: $now",
"PF-API-KEY: $api_key",
"PF-API-PASSPHRASE: $api_passphrase"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
$return_data = json_decode($server_output ,true);
print_r($return_data );

以下是我为手动添加边距所做的尝试POST/api/v1/仓位/保证金/存款保证金

post_data.php 的代码

include_once "config.php";
$endpoint = "/api/v1/position/margin/deposit-margin";
$url = "https://futures-api.poloniex.com".$endpoint;
$now = time() * 1000;
$vars_margin = ["symbol"=>"BTCUSDTPERP","margin"=>1,"bizNo"=>"1112222"];
$vars_margin = json_encode($vars_margin);
$vars_margin_post_fields  = "symbol=BTCUSDTPERP&margin=1&bizNo=1112222";
$sigs = signature($endpoint,$vars_margin,true,"POST");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars_margin_post_fields);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
"PF-API-SIGN: $sigs",
"PF-API-TIMESTAMP: $now",
"PF-API-KEY: $api_key",
"PF-API-PASSPHRASE: $api_passphrase"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
$return_data = json_decode($server_output ,true);
print_r($return_data );

真的不知道为什么post方法总是说数组([code]=>400005[msg]=<无效的PF-API-SIGN(

任何帮助都将不胜感激。

感谢

Post字段应该与签名的json_encoded值相匹配。

所以试试这个:

curl_setopt($ch, CURLOPT_POSTFIELDS, $vars_margin);

并且一定要添加一个标头来指示POST数据是json编码的:

...
"PF-API-PASSPHRASE: $api_passphrase",
"content-type: application/json"
];

最新更新