PHP parameters using CLI

Hi everyone, i would like to ask if i could use CLI script to be run on PHP?

i was reading about php script and came across with @boen.robot pdf

<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
header('Content-Type: text/plain');
$util = new RouterOS\Util($client);
$util->exec('
/ip dhcp-client lease
make-static [find address=$address]
comment [find address=$address] $name
/log info "User $name now has the static IP $address"
',
array(
'name' => $_GET['user'],
'address' => $_GET['ip']
)
);

i was thinking

<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
header('Content-Type: text/plain');
$util = new RouterOS\Util($client);
$util->exec('
/ system script add name="delete_user" source={ 
:foreach i in [/ip hotspot user find secret=tommy] do={ 
:if ([/ip hotspot user get $i limit-uptime]<=[/ip hotspot user get $i \ 
uptime]) do={ 
/ip hotspot user remove $i 
} 
} 
} 
',
array(
'name' => $_GET['user'],
'address' => $_GET['ip']
)
);

can i be able to run CLI script using PHP? i apologize for being a noob, just learning php. thank you

It should work, yes. That’s what Util::exec() is for.

Although for the particular scenario you want, I would recommend you just use add() instead. And if you want to integrate some local variables from user input, there’s the new Script::prepare() in the new version (the previous one had it under Util::prepareScript()).

f.e.

$util = new RouterOS\Util($client);
$util->setMenu('/system script')->add(array(
'name' => 'delete_user',
'source' => RouterOS\Script::prepare('
:foreach i in [/ip hotspot user find secret=$secret] do={ 
:if ([/ip hotspot user get $i limit-uptime]<=[/ip hotspot user get $i \ 
uptime]) do={ 
/ip hotspot user remove $i 
} 
}
', array(
'secret' => 'tommy'
)
)
));

Each call to Util::exec() is 3 API calls under the hood (add script, run script, remove script), whereas Util::add() is just one API call.