我通过相同的if语句运行两个api调用,以确保它们都返回值。错误检查的一种形式。
它们都通过了测试,但是json_decode无法访问或解码第一个file_get_contents。
if (
$exchangeRates = (file_get_contents('https://api.coinbase.com/v2/exchange-rates'))
&&
$data = (file_get_contents('https://api.coinbase.com/v2/prices/spot?currency=USD'))
){
$json1 = json_decode($exchangeRates, true);
$json2 = json_decode($data, true);
return [$json1, $json2];
}以上返回:
[
1,
{
"data":
{
"base": "BTC",
"currency": "USD",
"amount": "3532.335"
}
}
]当我在$json1中引用单个值时,它们返回null。
当手动将url输入到url中时,它们都返回适当的JSON。
每条if语句只能使用一个file_get_contents?
发布于 2019-01-21 15:08:34
请检查Operator Precedence,&&的优先级较高,因此它首先执行get_file_contents,然后使用&&并返回到$exchangeRates。最后,$exchangeRates是布尔值。在这种情况下,您应该正确使用():
if (
($exchangeRates = file_get_contents('https://api.coinbase.com/v2/exchange-rates'))
&&
($data = file_get_contents('https://api.coinbase.com/v2/prices/spot?currency=USD'))
) {
$json1 = json_decode($exchangeRates, true);
$json2 = json_decode($data, true);
return [$json1, $json2];
}https://stackoverflow.com/questions/54284734
复制相似问题