0

到目前为止,我有这个 PHP cURL 代码:

<?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;
}

到目前为止,我已经尝试过(不成功):

$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>';
 }
}

有人帮我遍历 JSON 并将数据按行放在我的表上。

接口链接

在邮递员上,使用基本身份验证,用户名:admin,密码:区提前谢谢!

4

1 回答 1

1

首先,你有

$immunization = $data["rows"];

foreach ($immunizations as $immunization) {

您应该将首字母复数$immunization$immunizations使其起作用。所以你有

$immunizations = $data["rows"];

foreach ($immunizations as $immunization) {

其次,查看您返回的数据,他们没有name, date, type密钥。他们只是有这样的价值观。所以将你的循环更新为

foreach ($immunizations as $immunization) {
   echo '<tr>';

   echo '<td>' . $immunization[0] . '</td>';
   echo '<td>' . $immunization[1] . '</td>';
   echo '<td>' . $immunization[2] . '</td>';

   echo '</tr>';

}

于 2017-07-10T22:44:22.780 回答