PHP-file\U get\U内容:无法在单个if语句中检查两个file\U get\U内容
我正在通过相同的if语句运行两个PHP-file\U get\U内容:无法在单个if语句中检查两个file\U get\U内容,php,if-statement,file-get-contents,Php,If Statement,File Get Contents,我正在通过相同的if语句运行两个api调用,以确保它们都返回值。错误检查的一种形式 它们都通过了测试,但是第一个文件\u get\u contents无法通过json\u decode访问或解码 if ( $exchangeRates = (file_get_contents('https://api.coinbase.com/v2/exchange-rates')) && $data = (file_get_contents('htt
api
调用,以确保它们都返回值。错误检查的一种形式
它们都通过了测试,但是第一个文件\u get\u contents
无法通过json\u decode
访问或解码
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\u get\u contents
吗?请检查,&&
的优先级较高,因此它首先执行get\u file\u 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];
}
请检查,&
的优先级较高,因此它首先执行获取文件内容
,然后使用&
并返回$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];
}
“有疑问时,用()来划分,然后征服”——孔子,请注意。响应可能仍然不是有效的json。它可以提取但无法解析。“当有疑问时,用()除并征服”——孔夫子,请注意。响应可能仍然不是有效的json。它可以获取但无法解析。