Json在插入时解码未定义的索引

时间:2017-03-25 22:28:53

标签: php mysql json api symfony

json


  {
      "data": {
        "acct": [
          {
            "maxaddons": "1",
            "ip": "192.168.0.20",
            "ipv6": ["0101:ca75:0101:ca75:0101:ca75:0101:ca77"],
            "outgoing_mail_suspended": 0,
            "outgoing_mail_hold": 0,
            "min_defer_fail_to_trigger_protection": 5,
            "legacy_backup": 0,
            "diskused": "0M",
            "maxftp": "2",
            "startdate": "13 Jul 08 14:33",
            "max_defer_fail_percentage": "10",
            "disklimit": "100M",
            "is_locked": "0",
            "suspendtime": null,
            "email": "",
            "domain": "example.com",
            "unix_startdate": 1373312011,
            "user": "example",
            "plan": "plan9",
            "shell": "\/bin\/bash",
            "maxpop": "20",
            "backup": 0,
            "theme": "paper_lantern",
            "owner": "root",
            "max_email_per_hour": "100",
            "suspendreason": "not suspended",
            "maxlst": "5",
            "suspended": 0,
            "maxsql": "1",
            "maxparked": "1",
            "partition": "home",
            "maxsub": "5"
          },
        ]
      },
      "metadata": {
        "version": 1,
        "reason": "OK",
        "result": 1,
        "command": "listaccts"
      }
    } 

// controller

public function ApiAction()
      {
          $Cpanel = new \Gufy\CpanelPhp\Cpanel;
          $Cpanel->setAuthType('hash');
          $Cpanel->setHost($this->container->getParameter('api_host'));
          $Cpanel->setAuthorization($this->container->getParameter('api_user'), $this->container->getParameter('api_hash'));
          $Cpanel->setTimeout(50);

          $Arguments = array();
          $QueryCpanel= $Cpanel->__call('listaccts', $Arguments);
          $em = $this->getDoctrine()->getEntityManager();
          $json = json_decode($QueryCpanel, true);

          foreach ($json as $List) {
            $Data['Domain'] =$List ['domain'];
            $Data['IP'] = $List ['ip'];
            $Data['UserName'] = $List ['user'];
            $Data['Email'] = $List ['email'];
            $Data['StartDate'] = $List ['startdate'];
            $Data['DiskPartition'] = $List ['partition'];
            $Data['Quota'] = $List ['disklimit'];
            $Data['DiskSpaceUsed'] = $List ['diskused'];
            $Data['Package'] = $List ['plan'];
            $Data['Theme'] = $List ['theme'];
            $Data['Owner'] = $List ['owner'];
            $Data['UnixStartDate'] = $List ['unix_startdate'];

              $em->getRepository('AppBundle:Listaccts')
                  ->InsertListaccts('listaccts', $Data);
        }

我试图将数据从api插入到数据库中,并且我一直收到错误 注意:未定义的索引:域

如果刷新我的浏览器,那么我收到此错误

执行'INSERT INTO(域,IP,用户名,电子邮件,StartDate,DiskPartition,配额,DiskSpaceUsed,包,主题,所有者,UnixStartDate)时发生异常(?,?,?,?,?,?,带有参数的?,?,?,?,?,?)'[null,null,null,null,null,null,null,null,null,null,null,null]:

我也试过控制器坚持我在那里得到了同样的错误。 我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

如果API中的JSON字符串结构如上所示,那么显然访问方法有些不正确。

正确的方法应如下:

$json = json_decode($QueryCpanel, true);
foreach($json["data"]["acct"] as $list){
    echo "Domain: ".$list["domain"];
}

Demo

这里的假设是,如果API返回多个帐户,它们将全部作为data索引下的数组提供。

尽管可能不太可能,但如果您的数组结构在data内直接重复,那么foreach和后续引用将需要一些改变。也许就是:

foreach($json["data"] as $list){
    ....
}

在任何情况下,如果您使用print_r(json);,您就可以了解访问路线以获取任何索引。

希望这会有所帮助。如果我误解了某些内容,请告诉我。