dimanche 20 janvier 2019

PHP-file_get_contents: Unable to check two file_get_contents in a single if statement

I am running two api calls through the same if statement to make sure they both return values. A form of error checking.

They both pass the test, however the first file_get_contents cannot be accessed or decoded by json_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];
}

The above returns:

[
 1,
 {
  "data": 
   {
   "base": "BTC",
   "currency": "USD",
   "amount": "3532.335"
   }
 }
]

When I refer to individual values in $json1 they return null.

When the url's are entered into a url manually they both return the appropriate JSON.

Can only one file_get_contents be used per if statement?

Aucun commentaire:

Enregistrer un commentaire