0

I have the following json that I am trying to access through PHP:

{
    "terms": {
        "url": "https://www.bankofcanada.ca/terms/"
    },
    "seriesDetail": {
        "FXCADUSD": {
            "label": "CAD/USD",
            "description": "Canadian dollar to US dollar daily exchange rate"
        }
    },
    "observations": [{
            "d": "2017-01-03",
            "FXCADUSD": {
                "v": 0.7443
            }
        },
        {
            "d": "2017-01-04",
            "FXCADUSD": {
                "v": 0.7510
            }
        },
        {
            "d": "2017-01-05",
            "FXCADUSD": {
                "v": 0.7551
            }
        }
    ]
}

And here is my PHP:

$url = 'https://www.bankofcanada.ca/valet/observations/FXCADUSD/json';
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
$results = json_decode($contents, true);

foreach($result['observations'] as $item) {
    echo $item['d'];
}

I am trying to echo to begin with just the date value but ultimately will want to display the exchange date value as well once I figure this out.

I am not sure if it is because the json is downloading as a file from the URL if that is what is causing the issue? Or if it is just my code that is the problem.

Barmar
  • 669,327
  • 51
  • 454
  • 560
Sackling
  • 1,724
  • 5
  • 35
  • 70

0 Answers0