Reputation: 57
So far, I have this PHP cURL code:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://play.dhis2.org/release1/api/25/analytics.json?dimension=dx%3AYtbsuPPo010%3Bl6byfWFUGaP%3Bs46m5MS0hxu&dimension=pe%3ALAST_12_MONTHS&filter=ou%3AImspTQPwCqd&displayProperty=NAME&skipMeta=true",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic YWRtaW46ZGlzdHJpY3Q=",
"cache-control: no-cache",
"postman-token: c7fc6164-194a-24ea-ccea-9f6bdbcd66f5"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
So far I have tried(unsuccessfully) with this:
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$data = json_decode($response, true);
$immunization = $data["rows"];
foreach ($immunizations as $immunization) {
echo '<tr>';
echo '<td>' . $immunization['name'] . '</td>';
echo '<td>' . $immunization['date'] . '</td>';
echo '<td>' . $immunization['type'] . '</td>';
echo '</tr>';
}
}
Someone to help me iterate through the JSON and place the data in rows on my table.
On postman, use Basic Auth, username: admin, password: district Thanks in advance!
Upvotes: 0
Views: 247
Reputation: 1140
First of all, you have
$immunization = $data["rows"];
foreach ($immunizations as $immunization) {
You should pluralize the initial $immunization
as $immunizations
for it to work. So you get to have
$immunizations = $data["rows"];
foreach ($immunizations as $immunization) {
Secondly, looking at your returned data, they don't have the name, date, type
keys. They just have the values there like that. So update your loop to
foreach ($immunizations as $immunization) {
echo '<tr>';
echo '<td>' . $immunization[0] . '</td>';
echo '<td>' . $immunization[1] . '</td>';
echo '<td>' . $immunization[2] . '</td>';
echo '</tr>';
}
Upvotes: 1