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.

2
  • 1
    aandd I see my typoe in $results my bad! Commented Feb 21, 2019 at 17:50
  • 1
    Turn PHP notices and warnings on. You were passing an invalid value to foreach. Commented Feb 21, 2019 at 18:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.