使用 Binance Api 和 php 检索 json 中所有市场对的列表时出现问题
Problem with retrieving a list of all market pairs in json using Binance Api with php
我正在尝试使用 foreach 检索 json 中所有市场对的列表。我希望结果是这样的
["BTC","LTC","ETH","NEO","BNB","QTUM","EOS","SNT","BNT","GAS","BCC","USDT","HSR","OAX","DNT","MCO","ICN","ZRX","OMG","WTC","YOYO","LRC","TRX"]
但是如果我使用我的 json_response 函数,我只会得到 BTC 或第一个值,如果我使用 echo 或 print,我会得到一个像 BTCLTCETH 这样的字符串,这意味着它会获取值,但它们只是一个大字符串。这是我的foreach。感谢任何帮助。
代码
$api = new Binance\API($key,$secret);
$exchangeInfo = $api->exchangeInfo();
foreach($exchangeInfo['symbols'] as $info) {
json_response($info['symbol']);
}
json_response 函数
function json_response($data=null, $httpStatus=200)
{
header_remove();
header("Content-Type: application/json");
http_response_code($httpStatus);
echo json_encode($data);
exit();
}
一对的 exchangeInfo 函数响应如下所示
{
"timezone": "UTC",
"serverTime": 1565246363776,
"rateLimits": [
{
}
],
"exchangeFilters": [
],
"symbols": [
{
"symbol": "ETHBTC",
"status": "TRADING",
"baseAsset": "ETH",
"baseAssetPrecision": 8,
"quoteAsset": "BTC",
"quotePrecision": 8, // will be removed in future api versions (v4+)
"quoteAssetPrecision": 8,
"baseCommissionPrecision": 8,
"quoteCommissionPrecision": 8,
"orderTypes": [
"LIMIT",
"LIMIT_MAKER",
"MARKET",
"STOP_LOSS",
"STOP_LOSS_LIMIT",
"TAKE_PROFIT",
"TAKE_PROFIT_LIMIT"
],
"icebergAllowed": true,
"ocoAllowed": true,
"quoteOrderQtyMarketAllowed": true,
"isSpotTradingAllowed": true,
"isMarginTradingAllowed": true,
"filters": [
//These are defined in the Filters section.
//All filters are optional
],
"permissions": [
"SPOT",
"MARGIN"
]
}
]
}
我需要从中提取所有 'symbol' 值
不要在循环中回显 JSON 字符串 X 次。在循环中构建一个数组,并从构建的数组中回显 JSON String ONCE。
看来您也从数据结构中的错误位置获取数据!
"symbol": "ETHBTC"
这不是您想要的。可能你需要
"baseAsset": "ETH"
and/or
"quoteAsset": "BTC"
$results = [];
foreach($exchangeInfo['symbols'] as $info) {
$results[] = $info['quoteAsset']; // or maybe baseAsset or maybe both
}
json_response($results);
Also you may want to remove the exit()
from your json_response()
function. It is a dangerous side effect to have in a function. If you need to exit once its called, put the exit
after the call to json_response()
but dont assume this will always be the last thing you want to do before terminating the script.
我正在尝试使用 foreach 检索 json 中所有市场对的列表。我希望结果是这样的
["BTC","LTC","ETH","NEO","BNB","QTUM","EOS","SNT","BNT","GAS","BCC","USDT","HSR","OAX","DNT","MCO","ICN","ZRX","OMG","WTC","YOYO","LRC","TRX"]
但是如果我使用我的 json_response 函数,我只会得到 BTC 或第一个值,如果我使用 echo 或 print,我会得到一个像 BTCLTCETH 这样的字符串,这意味着它会获取值,但它们只是一个大字符串。这是我的foreach。感谢任何帮助。
代码
$api = new Binance\API($key,$secret);
$exchangeInfo = $api->exchangeInfo();
foreach($exchangeInfo['symbols'] as $info) {
json_response($info['symbol']);
}
json_response 函数
function json_response($data=null, $httpStatus=200)
{
header_remove();
header("Content-Type: application/json");
http_response_code($httpStatus);
echo json_encode($data);
exit();
}
一对的 exchangeInfo 函数响应如下所示
{
"timezone": "UTC",
"serverTime": 1565246363776,
"rateLimits": [
{
}
],
"exchangeFilters": [
],
"symbols": [
{
"symbol": "ETHBTC",
"status": "TRADING",
"baseAsset": "ETH",
"baseAssetPrecision": 8,
"quoteAsset": "BTC",
"quotePrecision": 8, // will be removed in future api versions (v4+)
"quoteAssetPrecision": 8,
"baseCommissionPrecision": 8,
"quoteCommissionPrecision": 8,
"orderTypes": [
"LIMIT",
"LIMIT_MAKER",
"MARKET",
"STOP_LOSS",
"STOP_LOSS_LIMIT",
"TAKE_PROFIT",
"TAKE_PROFIT_LIMIT"
],
"icebergAllowed": true,
"ocoAllowed": true,
"quoteOrderQtyMarketAllowed": true,
"isSpotTradingAllowed": true,
"isMarginTradingAllowed": true,
"filters": [
//These are defined in the Filters section.
//All filters are optional
],
"permissions": [
"SPOT",
"MARGIN"
]
}
]
}
我需要从中提取所有 'symbol' 值
不要在循环中回显 JSON 字符串 X 次。在循环中构建一个数组,并从构建的数组中回显 JSON String ONCE。
看来您也从数据结构中的错误位置获取数据!
"symbol": "ETHBTC"
这不是您想要的。可能你需要
"baseAsset": "ETH"
and/or
"quoteAsset": "BTC"
$results = [];
foreach($exchangeInfo['symbols'] as $info) {
$results[] = $info['quoteAsset']; // or maybe baseAsset or maybe both
}
json_response($results);
Also you may want to remove the
exit()
from yourjson_response()
function. It is a dangerous side effect to have in a function. If you need to exit once its called, put theexit
after the call tojson_response()
but dont assume this will always be the last thing you want to do before terminating the script.