Community discussions

MikroTik App
 
marklodge
Member Candidate
Member Candidate
Topic Author
Posts: 250
Joined: Sun Jun 21, 2009 6:15 pm

Help with PHP API

Sun Jun 03, 2018 11:49 am

I have the following working perfectly in Delphi. But i don't know how to run the same in PHP.
How do I do the following using PHP API:
Eg: 1

/ppp/secret/remove
=numbers=testuser

Eg: 2
/ppp/secret/add
=name=testuser
=password=testpass
=remote-address=10.1.1.1

 
Eg: 3
/ppp/secret/print
=count-only

Eg: 4
/queue/simple/print
=count-only

Eg: 5
/queue/simple/remove
=numbers=1,2,3


Eg: 6
/queue/simple/add
=parent=testparent
=name=childtest
=max-limit=4000/4000
=limit-at=800/800
=target=192.1.1.2


Eg: 7
/queue/simple/add 
=name=testmulti
=target=21.1.1.1/32,14.1.2.2/32,14.1.2.3/32,14.1.2.4/32
=max-limit=4096/8192 
=burst-limit=10024/10024
=burst-time=10/15


Eg: 8

/ip/hotspot/user/print
=.proplist=.id,name,mac-address,uptime

 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Help with PHP API

Sun Jun 03, 2018 2:04 pm

Using my PHP client:
<?php
require_once 'PEAR2_Net_RouterOS-1.0.0b6.phar';

use PEAR2\Net\RouterOS;

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));

$util->setMenu('/ppp secret');
$util->remove('testuser');
$util->add(
    array(
        'name' => 'testuser',
        'password' => 'testpass',
        'remote-address' => '10.1.1.1'
    )
);
$secretCount = $util->count();

$util->setMenu('/queue simple');
$queueCount = $util->count();
$util->remove(1, 2, 3);
$util->add(
    array(
        'parent' => 'testparent',
        'name' => 'childtest',
        'max-limit' => '4000/4000',
        'limit-at' => '800/800',
        'target' => '192.1.1.2'
    ),
    array(
        'name' => 'testmulti',
        'target' => '21.1.1.1/32,14.1.2.2/32,14.1.2.3/32,14.1.2.4/32',
        'max-limit' => '4096/8192',
        'burst-limit' => '10024/10024',
        'burst-time' => '10/15'
    )
);

echo '<table>';
foreach ($util->setMenu('/ip hotspot user')->getAll(array('.proplist' => '.id,name,mac-address,uptime')) as $user) {
    echo '<tr>';
    foreach ($user as $prop => $value) {
        echo '<th>' . htmlspecialchars($prop) . '</th><td>' . htmlspecialchars($value) . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
 
marklodge
Member Candidate
Member Candidate
Topic Author
Posts: 250
Joined: Sun Jun 21, 2009 6:15 pm

Re: Help with PHP API

Mon Jun 04, 2018 3:00 pm

Im getting this error. the protected array.
Also the parent is created but why does this happen
Fatal error: Uncaught PEAR2\Net\RouterOS\RouterErrorException: Router returned error when adding items in phar://C:/mikrotikweb/PEAR2_Net_RouterOS-1.0.0b6.phar/PEAR2_Net_RouterOS-1.0.0b6/src/PEAR2/Net/RouterOS/Util.php:867 Stack trace: #0 C:\mikrotikweb\tst2.php(23): PEAR2\Net\RouterOS\Util->add(Array, Array) #1 {main} Response collection: Array ( [0] => PEAR2\Net\RouterOS\Response Object ( [unrecognizedWords:protected] => Array ( ) [_type:PEAR2\Net\RouterOS\Response:private] => !trap [attributes:protected] => Array ( [message] => input does not match any value of parent ) [_tag:PEAR2\Net\RouterOS\Message:private] => ) [1] => PEAR2\Net\RouterOS\Response Object ( [unrecognizedWords:protected] => Array ( ) [_type:PEAR2\Net\RouterOS\Response:private] => !done [attributes:protected] => Array in phar://C:/mikrotikweb/PEAR2_Net_RouterOS-1.0.0b6.phar/PEAR2_Net_RouterOS-1.0.0b6/src/PEAR2/Net/RouterOS/Util.php on line 867
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Help with PHP API

Tue Jun 05, 2018 10:19 am

key part from this error message is
[_type:PEAR2\Net\RouterOS\Response:private] => !trap [attributes:protected] => Array ( [message] => input does not match any value of parent )
Apparently, you have no queue named "testparent", and thus the add() call in "/queue simple" fails. Add "testparent" first, and then this call will work.

You can handle errors like these more gracefully by surrounding the statement(s) with try-catch, e.g.
try {
$util->setMenu('/queue simple');
$queueCount = $util->count();
$util->remove(1, 2, 3);
$util->add(
    array(
        'parent' => 'testparent',
        'name' => 'childtest',
        'max-limit' => '4000/4000',
        'limit-at' => '800/800',
        'target' => '192.1.1.2'
    ),
    array(
        'name' => 'testmulti',
        'target' => '21.1.1.1/32,14.1.2.2/32,14.1.2.3/32,14.1.2.4/32',
        'max-limit' => '4096/8192',
        'burst-limit' => '10024/10024',
        'burst-time' => '10/15'
    )
);
} catch (RouterOS\RouterErrorException $e) {
    echo 'Router failed with the following first error: ' . $e->getResponses()->getAllOfType(RouterOS\Response::TYPE_ERROR)->getProperty('message');
}

Who is online

Users browsing this forum: No registered users and 62 guests