如何执行 PHP POST 请求 FTX 休息 API

How to do a PHP POST request for FTX rest API

我想改变我的 account leverage on FTX with there rest API。只是为了测试,因为之后我想 post 下订单。但是我无法让它们都工作,并且出于某种原因我不知道该怎么做。

到目前为止,我可以执行获取请求并对其进行身份验证。这是我用于此类带有身份验证的获取请求的代码。我使用 PHP 来完成它并使用 Javascript 来获取 php 文件。

<?php

// API keys.
$keys = array(
    'apiKey'=> '....',
    'secretKey'=> '....'
);

// Get current time * 1000 to make sure I get a timestamp in milliseconds. Which is needed for the api.
$timestamp = time() * 1000;

// Base url for all api calls.
$baseURL = 'https://ftx.com/api';

// Specified url endpoint. This comes after the baseUrl.
$endPoint = '/account';

// Parameters that are needed for the endPoint its call.
$parameters = '';

// Data that should be added to the encryption of the keys.
$signature = $timestamp . 'GET/api' . $endPoint;

// Hashing the secret key.
$secret = hash_hmac('sha256', $signature, $keys['secretKey']);

// All parts together.
$url = $baseURL . $endPoint . '?' . $parameters;

//Init session for CURL.
$ch = curl_init();

// Options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Make sure curl_exec() will return the result on success and not "success" on success. Also removes error when fetched in Javascript.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_FAILONERROR, true);

// Init headers for access to the API signed data.
$headers = array();
$headers[] = 'FTX-KEY: ' . $keys['apiKey'];
$headers[] = 'FTX-SIGN: ' . $secret;
$headers[] = 'FTX-TS: ' . $timestamp;

// Setting headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute request.
$result = curl_exec($ch);

if (curl_errno($ch)) {
    $error_msg = curl_error($ch);
    echo($error_msg);
}

// Ends the CURL session, frees all resources and deletes the curl (ch).
curl_close($ch);

echo($result);
exit();

?>

再一次,这成功了。但现在我想做一个 POST 请求。根据 authentication documentation 我只需要在它说的身份验证中添加一个请求正文。我认为这些是 $parameters。但我不确定。无论如何,这是我目前的代码:

<?php

// API keys.
$keys = array(
    'apiKey'=> 'normal',
    'secretKey'=> 'secret'
);

// Get current time * 1000 to make sure I get a timestamp in milliseconds. API asks for it this way.
$timestamp = time() * 1000;

// Base url for all api calls.
$baseURL = 'https://ftx.com/api';

// Specified url endpoint. This comes after the baseUrl.
$endPoint = '/account/leverage';

// Parameters that are needed for the endPoint its call.
$parameters = 'leverage=10';

// Data that should be added to the encryption of the keys. NOTE: Added $parameters.
$signature = $timestamp . 'POST/api' . $endPoint . '?' . $parameters;

// Hashing the secret key.
$secret = hash_hmac('sha256', $signature, $keys['secretKey']);

// For account data
$url = $baseURL . $endPoint . '?' . $parameters;

// Init session for CURL.
$ch = curl_init();

// Options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Make sure curl_exec() will return the result on success and not "success" on success. Also removes error when fetched in Javascript.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_FAILONERROR, true);

// Tried this, but didn't do much.
// curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);

// Init headers for access to the API signed data.
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-type: application/x-www-form-urlencoded';
$headers[] = 'FTX-KEY: ' . $keys['apiKey'];
$headers[] = 'FTX-SIGN: ' . $secret;
$headers[] = 'FTX-TS: ' . $timestamp;

// Setting headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute request.
$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo(curl_error($ch));
    exit();
}

// Ends the CURL session, frees all resources and deletes the curl (ch).
curl_close($ch);

echo($result);

?>

This url 是他们为身份验证提供的“示例”。在底部有一个“POST 签名示例”,其参数如下。

b'1588591856950POST/api/orders{"market": "BTC-PERP", "side": "buy", "price": 8500, "size": 1, "type": "limit", "reduceOnly": false, "ioc": false, "postOnly": false, "clientId": null}'

第一个b'部分我觉得是Python的东西,长号是我加的时间戳,POST/api/orders我也有。但是对象(我认为它是一个对象)我没有。我试过了,没成功。

我从 API 得到的 error 是 400 错误请求。当我用签名更改某些内容时,我大多数时候都会收到 401 未经授权。例如,将时间戳更改为秒或删除“?”在签名中也给出了 401.

url 部分没有问题,因为我已经完成了 get 请求。因此,如果有任何帮助,我将不胜感激。在我使用 Binance 之前,它工作得很好。但是他们正在做一些粗略的事情,所以我必须采取行动以确保我的钱不会消失,以防万一他们变坏:(

编辑:

$parameters = 'leverage=10'; 给我一个

的$签名
1627494427000POST/api/account/leverage?leverage=10

并导致 400 错误请求。 API 消息:“缺少参数杠杆”。

$specialParam = json_encode(['leverage' => '10']); 给我签名

1627494427000POST/api/account/leverage?{"leverage":"10"}

并导致 400 错误请求。

对两者执行相同操作,但在我的 $signature 中没有 . '?' . 部分会导致两者都进入 401 Unauthenticated... API 消息:“未登录”

我知道怎么做了。它与 $parameters@signature 有关。在执行 POST 请求时,您需要 json_encode() 参数,然后在 $signature 中使用它。参见 $specialParam。下面的代码使 杠杆 .

成为可能
<?php

// API keys.
$keys = array(
    'apiKey'=> 'normal',
    'secretKey'=> 'secret'
);

// Get current time * 1000 to make sure I get a timestamp in milliseconds. API asks for it this way.
$timestamp = time() * 1000;

// Base url for all api calls.
$baseURL = 'https://ftx.com/api';

// Specified url endpoint. This comes after the baseUrl.
$endPoint = '/account/leverage';

// Encoded parameters, needed for authentication and post request.
$specialParam = json_encode(['leverage' => 10]);

// Data that should be added to the encryption of the keys.
$signature = $timestamp . 'POST/api' . $endPoint . $specialParam;

// Hashing the secret key.
$secret = hash_hmac('sha256', $signature, $keys['secretKey']);

// For account data
$url = $baseURL . $endPoint;

// Init session for CURL.
$ch = curl_init();

// Options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $specialParam);

// Init headers for access to the FTX API signed data.
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'FTX-KEY: ' . $keys['apiKey'];
$headers[] = 'FTX-SIGN: ' . $secret;
$headers[] = 'FTX-TS: ' . $timestamp;

// Setting headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute request.
$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo(curl_error($ch));
    exit();
}

// Ends the CURL session, frees all resources and deletes the curl (ch).
curl_close($ch);

echo($result);
?>

请注意,错误捕获部分并不是真正的 good/trustable 捕获错误的方法。至少对我来说,它并不总是像我希望的那样成功。我遗漏了一些 API 超过 400/401 的消息。

这是另一个如何在 FTX 上设置订单的示例:

<?php

// API keys.
$keys = array(
    'apiKey'=> 'normal',
    'secretKey'=> 'secret'
);

// Get current time * 1000 to make sure I get a timestamp in milliseconds. API asks for it this way.
$timestamp = time() * 1000;

// Base url for all api calls.
$baseURL = 'https://ftx.com/api';

// Specified url endpoint. This comes after the baseUrl.
$endPoint = '/orders';

$specialParam = json_encode(
    [
        'market'=> 'AAVE/USDT',
        'side' => 'buy',
        'price' => null,
        'type' => 'market',
        'size' => 3
    ]
);

// Data that should be added to the encryption of the keys.
$signature = $timestamp . 'POST/api' . $endPoint . $specialParam;

// Hashing the secret key.
$secret = hash_hmac('sha256', $signature, $keys['secretKey']);

// For account data
$url = $baseURL . $endPoint;

// Init session for CURL.
$ch = curl_init();

// Options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $specialParam);

// Init headers for access to the FTX API signed data.
$headers = array();
// $headers[] = 'Content-type: application/x-www-form-urlencoded';
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'FTX-KEY: ' . $keys['apiKey'];
$headers[] = 'FTX-SIGN: ' . $secret;
$headers[] = 'FTX-TS: ' . $timestamp;

// Setting headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute request.
$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo(curl_error($ch));
    exit();
}

// Ends the CURL session, frees all resources and deletes the curl (ch).
curl_close($ch);

echo($result);
?>

如果您也对 GET 请求 感兴趣,我会为您提供帮助。这得到最后的价格(不是过去的,而是当前的)。:

<?php
// To make sure I always get the last line existing.
$startTime = time() - 900;

// Base url for all api calls.
$baseURL = 'https://ftx.com/api';

// Specified url endpoint. This comes after the baseUrl.
$endPoint = '/markets/ETH/USDT/candles';

// Parameters that are needed for the endPoint its call.
$parameters = 'resolution=900&start_time=' . $startTime;

// All parts together.
$url = $baseURL . $endPoint . '?' . $parameters;

//Init session for CURL.
$ch = curl_init();

// Options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Make sure curl_exec() will return the result on success and not "success" on success. Also removes error when fetched in Javascript.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_FAILONERROR, true);

// Execute request.
$result = curl_exec($ch);

if (curl_errno($ch)) {
    $error_msg = curl_error($ch);
    echo($error_msg);
}

// Ends the CURL session, frees all resources and deletes the curl (ch).
curl_close($ch);

echo($result);
?>

我希望这些示例足以使 FTX API 可用。不幸的是,FTX 的文档很难理解:(