php - 如何为 FTX REST API 执行 PHP POST 请求
问题描述
我想用其他 API更改我在 FTX 上的账户杠杆。只是为了测试,因为在那之后我想发布订单。但我不能让他们两个都工作,出于某种原因,我不知道该怎么做。
到目前为止,我能够执行获取请求并对其进行身份验证。这是我用于此类获取请求 WITH 身份验证的代码。我使用 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 请求。根据身份验证文档,我只需要将请求正文添加到它所说的身份验证中。我认为这些是$参数。但我不确定。无论如何,这是我到目前为止的代码:
<?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);
?>
此 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 得到的错误是 400 bad request。当我使用签名更改某些内容时,我大部分时间都会收到 401 Unauthorized。例如,将时间戳更改为秒或删除“?” 在签名中也给出了 401。
url 部分不会有任何问题,因为我已经发出了 get 请求。因此,如果有任何帮助,我将不胜感激。在我使用 Binance 之前,它运行良好。但是他们正在做一些粗略的事情,所以我必须采取行动以确保我的钱不会消失,以防万一他们变坏:(
编辑:
$parameters = 'leverage=10';
给我一个 $signature
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
and有关@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);
?>
请注意,错误捕获部分并不是捕获错误的真正好/可信赖的方法。至少对我来说,它并不总是像我希望的那样成功。我错过了一些超过 400/401 的 API 消息。
这是另一个如何在 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 的文档很难遵循 :(
推荐阅读
- apollo - 如何使用 SSR 将响应标头中的数据传递给 ApolloProvider?
- go - 在 Go 中使用 Autocert 并附加自签名证书以在本地提供 https
- visual-studio-code - VSC 命令面板(太窄)
- r - Move a column based in his colname after a column based in his colname in R
- android-studio - Flutter bottomSheet 更改主应用程序状态
- python - 如何使用 python beautiful-soup 从网站上抓取 url?
- c - 怎么会有比 (unsigned long long) > LONG_MAX 大的东西?
- node.js - 使用 Jest 和 Sinon 测试具有属性的函数
- java - 在 JBoss 上使用 Liferay 的 Java 垃圾收集问题:GC 执行了多次
- reactjs - 如何获取本地 JSON(获取)?