PHP API version of /tool user-manager user reset-counters 0

Hi There,

I have a PHP script I have written to email the clients their usage and cost. I want to add the PHP API version of this

/tool/user-manager/user/reset-counters 0
and/or
/tool user-manager user reset-counters A_Number

to it but I cant seem to get around the space after the reset-counters command with the api.
All my clients are under customer=admin

Can I use the reset-counters commmand with ‘name’?

Any help on the subject would be wonderful.

Thanking you in advance

Markto1970
springlandswifi.thruhere.net

In general, when using the API, you need to write arguments separately from the command AND you need to explicitly use the name of the argument you’re setting.

You can find out the name by going to a terminal and using “?” with the command. Nameless arguments have the form “” (in the API, you write them without the “<” and “>”). In the case of /tool/user-manager/user, the argument’s name is “numbers”.

Hi, do you mean like this?

if ($API2->connect(‘172.17.0.1’, ‘api’, ‘api1234marks’)) {

$AA = $API2->comm(“/tool/user-manager/user/print”, array(‘.proplist’ => ‘.id’, ‘?name’ => ‘markto1970’, ));
$BB = $API2->comm(‘/tool/user-manager/user/reset-counter’, array(‘=numbers’ => ‘$AA’, ));


print_r($AA);
print_r($BB);
}

This results with

Array ( [0] => Array ( [.id] => *1 ) ) Array ( [!trap] => Array ( [0] => Array ( [message] => unknown parameter ) ) )

What am i missing?

  1. No quotes around the variable name. Otherwise, you’re writing out the literal string “$AA” rather the value of $AA.
  2. It appears the comm() function expects arguments without the surrounding “=”, as it should.
  3. Looking at the output, $AA is an array of arrays of arguments, and you therefore need to explicitly target where the ID is.

With those corrections in mind:

if ($API2->connect('172.17.0.1', 'api', 'api1234marks')) {

    $AA = $API2->comm("/tool/user-manager/user/print", array('.proplist' => '.id', '?name' => 'markto1970'));
    $BB = $API2->comm('/tool/user-manager/user/reset-counter', array('numbers' => $AA[0]['.id']));

    print_r($AA);
    print_r($BB);
}
 

If it doesn’t work even with these corrections, I invite you to try out my client (from my signature, or at the bottom of the wiki page about the API), in which you can do it with something like this:

<?php
namespace PEAR2\Net\RouterOS;

require_once 'PEAR2/Net/RouterOS/Autoload.php';
require_once 'PEAR2/Net/Transmitter/Autoload.php';

try {
    $client = new Client('172.17.0.1', 'api', 'api1234marks');

    $printRequest = new Request('/tool/user-manager/user/print');
    $printRequest->setArgument('.proplist', '.id');
    $printRequest->setQuery(Query::where('name', 'markto1970'));

    $userId = $client->sendSync($printRequest)->getArgument('.id');

    $resetRequest = new Request('/tool/user-manager/user/reset-counter');
    $resetRequest->setArgument('numbers', $userId);

    $client->sendSync($resetRequest);
} catch(Exception $e) {
    echo $e;
}
?>

And in a version I’ll release soon, you could (if you want to) squeeze the code as tight as:

<?php
namespace PEAR2\Net\RouterOS;

require_once 'PEAR2/Net/RouterOS/Autoload.php';

try {
    $client = new Client('172.17.0.1', 'api', 'api1234marks');

    $client(new Request('/tool/user-manager/user/reset-counter numbers=' .
        $client(new Request('/tool/user-manager/user/print .proplist=.id', null, Query::where('name', 'markto1970')))
            ->getArgument('.id')
    ));
} catch(Exception $e) {
    echo $e;
}
?>

[edit]I’ve now released the version I was talking about…[/edit]

I got this error when trying to get your package. Will test your mods to the code and advise.

Thanks for replying


The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.

\

An invalid character was found in text content. Error processing resource 'http://cloud.github.com/downloads/pear2/PEAR2_Ne...

PK

What am I missing? I have looked at the examples in ur wiki. Puzzled.

<?php namespace PEAR2\Net\RouterOS; require_once 'PEAR2/Net/RouterOS/Autoload.php'; require_once 'PEAR2/Net/Transmitter/Autoload.php'; \ \ \ try { $client = new Client('172.16.0.1', 'api', 'api1234barbs'); $printRequest = new Request('/tool/user-manager/user/print'); $printRequest->setArgument('.proplist', '.id'); $printRequest->setQuery(Query::where('name', $name)); $userId = $client->sendSync($printRequest)->getArgument('.id'); $resetRequest = new Request('/tool/user-manager/user/reset-counters'); $resetRequest->setArgument('numbers', $userId); $client->sendSync($resetRequest); $addRequest = new Request('/tool/user-manager/user/comment'); $addRequest->setArgument('comment', $comment); $addRequest->setArgument('numbers', $userId); if ($client->sendSync($addRequest)->getType() !== Response::TYPE_FINAL) { echo "Error when Setting comment for $name
"; } } catch(Exception $e) { echo $e; } ?>

Thanks for your help in advance.

That’s odd… but seeing that you’re playing around with the code, I’m assuming you’ve somehow worked around this? Did you tried the same link again or…?

Anyhow… the only wrong thing I see in your code is that you haven’t defined $name and $comment anywhere. I don’t use the user manager, so I can only assume the reset-counters call should work. But after adding a test user on my configuration, adding the variables and trying out the script, the comment was modified, so it should work.

Here’s an example with your previous username (I’ve also added an “OK” message at the end, for the sake of ensuring it’s not a PHP error):

<?php

namespace PEAR2\Net\RouterOS;

require_once 'PEAR2/Net/RouterOS/Autoload.php';
require_once 'PEAR2/Net/Transmitter/Autoload.php';

$name = 'markto1970';
$comment = 'Hello world from PHP API!';

try {
 $client = new Client('172.16.0.1', 'api', 'api1234barbs');

 $printRequest = new Request('/tool/user-manager/user/print');
 $printRequest->setArgument('.proplist', '.id');
 $printRequest->setQuery(Query::where('name', $name));

 $userId = $client->sendSync($printRequest)->getArgument('.id');

 $resetRequest = new Request('/tool/user-manager/user/reset-counters');
 $resetRequest->setArgument('numbers', $userId);

 $client->sendSync($resetRequest);

$addRequest = new Request('/tool/user-manager/user/comment');
 
$addRequest->setArgument('comment', $comment);
$addRequest->setArgument('numbers', $userId);
if ($client->sendSync($addRequest)->getType() !== Response::TYPE_FINAL) {
 echo "Error when Setting comment for $name<br>";
} else {
 echo "Comment '{$comment}' set successfully for '{$name}'.";
}
} catch(Exception $e) {
 echo $e;
}

?>

I got the package from another link, cant remember where.

The $userId and $comment are in the Bigger monthlyaccount script. Will advise on code changes.

I’m going to guess sourceforge.net, since that’s the two places from which it can be downloaded.

Are you including the file given here or is there simply some code around it? If there’s code around it, you have to make sure the namespace declaration is at the top of the file (as in “the first statement in a file that starts with nothing but ‘<?php’”) no matter what.

Hi,

It turns out that when I set up the api user I has set it to ‘read’ not ‘full’. All is right in the world and all is working as it should. Will post the monthlyaccount script after I have tidyed it up a little. I used the script to do this months accounts. Happy as. Thanks boen_robot.

Mark