gpt4 book ai didi

php - Json 在插入时解码 undefined index

转载 作者:行者123 更新时间:2023-11-30 22:02:15 27 4
gpt4 key购买 nike

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 插入数据库,但我不断收到错误注意: undefined index :域

如果刷新我的浏览器然后我得到这个错误

执行 'INSERT INTO (Domain, IP, UserName, Email, StartDate, DiskPartition, Quota, DiskSpaceUsed, Package, Theme, Owner, UnixStartDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, null, null, null, null, null, null, null, null, null, null, null]:

我也尝试过使用 controller persist 我在那里遇到了同样的错误。我不知道我做错了什么。

最佳答案

如果来自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);,您将能够了解访问任何索引所采用的访问路径。

希望这对您有所帮助。如果我误解了什么,请告诉我。

关于php - Json 在插入时解码 undefined index ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022690/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com