我已经很难从Json url检索eth值了一段时间,它一直在返回错误“未定义的偏移量: 0”,因为它可能很容易给出答案的任何人,请考虑我是一个学习者和即将到来。我下面的代码
$ethjs = "https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=0xdac17f958d2ee523a2206206994597c13d831ec7&vs_currencies=eth";
$ejson = file_get_contents($ethjs);
$ejson = json_decode($ejson, true);
$one_eth = $ejson[0]->eth;发布于 2021-05-25 03:41:18
来自url的响应类似于
// https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=0xdac17f958d2ee523a2206206994597c13d831ec7&vs_currencies=eth
{
"0xdac17f958d2ee523a2206206994597c13d831ec7": {
"eth": 0.0003859
}
}上面输出的东西很少
由于这似乎是支持异构数组的PHP,因此您需要通过在url中传递的contract_address参数的值来访问它,因为这是结果对象中的key。
把它统称为.
$ethjs = "https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=0xdac17f958d2ee523a2206206994597c13d831ec7&vs_currencies=eth";
$ejson = file_get_contents($ethjs);
$ejson = json_decode($ejson, true);
$one_eth = $ejson['0xdac17f958d2ee523a2206206994597c13d831ec7']['eth'];
echo $one_eth;https://stackoverflow.com/questions/67681292
复制相似问题