Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-file\U get\U内容:无法在单个if语句中检查两个file\U get\U内容_Php_If Statement_File Get Contents - Fatal编程技术网

PHP-file\U get\U内容:无法在单个if语句中检查两个file\U get\U内容

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

我正在通过相同的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('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。它可以获取但无法解析。