If the respons content is a JSON response, you can use the
$remote->json()
method to covert the content to an array or object.
In this example, we get list of UK holidays from a freely accessible API and convert the JSON response to an array:
$response = Remote::get('https://www.gov.uk/bank-holidays.json');
if ($response->code() === 200):
$data = $response->json();
foreach ($data as $item): ?>
<h2><?= $item['division'] ?></h2>
<?php foreach ($item['events'] as $holiday): ?>
<li><?= $holiday['title'] . ' is on ' . $holiday['date'] ?></li>
<?php endforeach ?>
<?php endforeach ?>
<?php endif ?>
If you prefer to work with an object instead of an array, set the
$array
parameter to
false
. Here we get a list of historical US newspapers:
$response = Remote::get('https://chroniclingamerica.loc.gov/newspapers.json');
if ($response->code() === 200):
$data = $response->json(false); ?>
<?php foreach ($data->newspapers as $newspaper): ?>
<li><?= $newspaper->title ?></li>
<?php endforeach ?>
<?php endif ?>
Did you find an error? Help us improve our docs and edit this page on GitHub. Make sure to check out
our styleguide →