I need help with PHP API for adding an address to the address-list if it doesn't already exist and removing if it does exist.
I am using routeros_api.class.php from https://wiki.mikrotik.com/wiki/API_PHP_class.
I have tried this, the removal part works correctly but does not check if it exists first before removing but the first part of the code does not, it just hangs when running.
if ($datalimitexceeded == "1") {
$API->write('/ip/firewall/address-list/print', false);
$API->write('?comment='.$comment, false);
$API->write('?address='.$site_ip, false);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
if(count($ARRAY)>0){
$API->write("/ip/firewall/address-list/add",false);
$API->write("=.id=".$ARRAY[0]['.id'],false);
$API->write('=list='.$list,false);
$API->write('=address='.$site_ip,false);
$API->write('=comment='.$comment,true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
} else {
$API->write("/ip/firewall/address-list/add",false);
$API->write('=list='.$list,false);
$API->write('=address='.$address,false);
$API->write('=comment='.$comment,false);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
}
Fixed and sorted my problem!
Here is my working code:
if ($datalimitexceeded == "1") {
$API->write('/ip/firewall/address-list/print',false);
$API->write('?comment='.$comment,true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
if(count($ARRAY)>0){
$API->write('/ip/firewall/address-list/set',false);
$API->write("=.id=".$ARRAY[0]['.id'],false);
$API->write('=disabled=no',true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
} else {
$API->write('/ip/firewall/address-list/add',false);
$API->write('=list='.$list,false);
$API->write('=address='.$site_ip,false);
$API->write('=comment='.$comment,true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
}
}
if ($datalimitexceeded == "0") {
$API->write('/ip/firewall/address-list/print',false);
$API->write('?comment='.$comment,true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
if(count($ARRAY)>0){
$API->write('/ip/firewall/address-list/remove', false);
$API->write('=.id=' . $ARRAY[0]['.id']);
$READ = $API->read(false);
}
}