我已经有了功能代码,但是我目前正在尝试找到破解它的方法,以便我可以找到要捕获的异常。下面的代码连接到Bluecat / Proteus API,并尝试针对通过CSV上传的IP / MAC组合列表分配DHCP Reservation。了解将要使用此工具的人员,IP和/或MAC中的拼写错误是可能的,并且在给定的CSV上传中可能有数百个要处理的项目。
为了测试,我传递了一个带有无效八位字节的IP和一个具有相同问题的MAC。不幸的是,我遇到过这种错误类型,并且根据我搜索过的内容,没有办法优雅地捕获并记录错误,然后转到要处理的列表中的下一个项目。
这是我的代码:
<?php
session_start();
$client = new SoapClient("...server with WSDL thingy...");
$client->login($_SESSION['user'], $_SESSION['pass']);
# List of items to process taken from a form upload on a preceeding page
# List saved as array in 'csv'
foreach ($_SESSION['csv'] as $i=>$row) {
try {
$_SESSION['csv'][$i] = array_combine($_SESSION['keys'], $row);
$client->assignIP4Address('5',$_SESSION['csv'][$i]['ip4Address'],$_SESSION['csv'][$i]['macAddress'],'','MAKE_DHCP_RESERVED','');
}
catch (Exception $e) {
#Exception messaging goes here
}
}
?>
我得到的具体错误是:
致命错误:未捕获的SoapFault异常:[env:Server]无效的八位字节值:C:\ Program Files(x86)中的666 \ ampps \ www \ proteustool \ dhcp_reservations.php:30堆栈跟踪:#0 C:\ Program文件(x86)\ ampps \ www \ proteustool \ _dhcp_reservations.php(30):SoapClient-&gt; __ call(&#39; assignIP4Addres ...&#39;,Array)#1 C:\ Program Files(x86)\ Ampps \ www \ proteustool \ _dhcp_reservations.php(30):SoapClient-&gt; assignIP4Address(&#39; 5&#39;,&#39; 10.166.28.666&#39;,&#39; cc:dd:ee: ff:00:...&#39;,&#39;&#39;,&#39; MAKE_DHCP_RESER ...&#39;,&#39;&#39;)#2 {main}引入第30行的C:\ Program Files(x86)\ ampps \ www \ proteustool \ dhcp_reservations.php
和
SoapFault:无效的八位字节值:第30行的C:\ Program Files(x86)\ ampps \ www \ proteustool \ dhcp_reservations.php中的666
请注意,错误会在无效IP处杀死脚本,因为它首先出现在soap提供程序的方法调用的参数列表中。我不知道无效MAC的错误是什么,但我猜是类似的东西。
无效的IP / MAC八位字节是这样的:x.x.x.666和xx:xx:xx:xx:xx:ZZ
有没有办法捕获这种特定类型的致命错误并继续处理foreach循环中的下一个项目?正如我所说的,源CSV中可能有数百行,并且脚本会在某个随机位置死亡,因为输入错误会让人痛苦。
这个问题适用于PHP 5.6,只是为了完整。
答案 0 :(得分:0)
foreach ($_SESSION['csv'] as $i=>$row){
$_SESSION['csv'][$i] = array_combine($_SESSION['keys'],$row);
if (filter_var($_SESSION['csv'][$i]['ip4Address'],FILTER_VALIDATE_IP) && filter_var($_SESSION['csv'][$i]['macAddress'],FILTER_VALIDATE_MAC)){
$client->assignIP4Address('5',$_SESSION['csv'][$i]['ip4Address'],$_SESSION['csv'][$i]['macAddress'],'','MAKE_DHCP_RESERVED','');
}else{
// nothing actually it will just move on to then next row
//you could record the faliur or alert the user if required
}
}
尝试\ catch删除,因为你似乎没有做任何事情
答案 1 :(得分:0)
美丽。我不得不添加一些东西来测试实际的分配步骤,但它现在运行得很好。这是我最终得到的结果,万一有人好奇:
foreach ($_SESSION['csv'] as $i=>$row){
$_SESSION['csv'][$i] = array_combine($_SESSION['keys'],$row);
if (filter_var($_SESSION['csv'][$i]['ip4Address'],FILTER_VALIDATE_IP) && filter_var($_SESSION['csv'][$i]['macAddress'],FILTER_VALIDATE_MAC)){
$success = true;
try {
$client->assignIP4Address('5',$_SESSION['csv'][$i]['ip4Address'],$_SESSION['csv'][$i]['macAddress'],'','MAKE_DHCP_RESERVED','');
}
catch (Exception $e) {
echo "Failure: " . $_SESSION['csv'][$i]['ip4Address'] . "/" . $_SESSION['csv'][$i]['macAddress'] . " could not be allocated. Cause: " . $e->getMessage() . "<br>";
$success = false;
}
if ($success) {
echo "Success: " . $_SESSION['csv'][$i]['ip4Address'] . " has been allocated to " . $_SESSION['csv'][$i]['macAddress'] . "<br>";
}
}
else{
echo "Failure: " . $_SESSION['csv'][$i]['ip4Address'] . "/" . $_SESSION['csv'][$i]['macAddress'] . " could not be allocated. Please check that the IP or MAC is valid. <br>";
}
}