Capture php api error

Capture php api error

I am trying to capture the errors returned in api php … for example if a user already exists

<?php

//require('routeros_api.class.php');
require './routeros_api.class';
$API = new routeros_api();
$API->debug = false;
if ($API->connect('192.168.1.1', 'admin', '123')) {

  $API->comm("/ppp/secret/add", array(
          "name"     => "user",
          "password" => "pass",
          "remote-address" => "172.16.1.10",
          "comment"  => "{new VPN user}",
          "service"  => "pptp",
));
  
 // this is where I want to capture if an error occurs
  //example delphi
  if Res.Trap then
     echo "Error ".ROS.LastError
 
  
   $API->disconnect();
   
   

} else echo "Error al conectar a MK"
?>

I do it in delphi follows
// this is where I want to capture if an error occurs
//example delphi
if Res.Trap then
echo "Error ".ROS.LastError

regards

AFAIK, this client does not reveal the response types when using comm(). You could of course use read() to interpret the response “manually”, but as an alternative, I’d suggest replacing the client itself.

With the one from my signature, you can do what you want as easily as:

<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';

try {
    $util = new RouterOS\Util(new RouterOS\Client('192.168.1.1', 'admin', '123'));
} catch (Exception $e) {
    echo 'Error connecting to MikroTik.';
    return;
}

$util->changeMenu('/ppp/secret');

$id = $util->add(array(
    "name" => "user",
    "password" => "pass",
    "remote-address" => "172.16.1.10",
    "comment" => "{new VPN user}",
    "service" => "pptp",
));
if ('' === $id) {
    //entry already exists or some other (unknown) kind of an error happened.
}
//Entry was added, and $id contains its ID       

Or if you want to be a little more thorough with your error handling:

<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';

try {
    $client = new RouterOS\Client('192.168.1.1', 'admin', '123');
} catch (Exception $e) {
    echo 'Error connecting to MikroTik.';
    return;
}

$addRequest = new RouterOS\Request('/ppp/secret/add');
$addRequest
    ->setArgument('name', 'user')
    ->setArgument('password', 'pass')
    ->setArgument('remote-address', '172.16.1.10')
    ->setArgument('comment', '{new VPN user}')
    ->setArgument('service', 'pptp');

$responses = $client->sendSync($addRequest);
$errors = $responses->getAllOfType(RouterOS\Response::TYPE_ERROR);
if (!empty($errors)) {
    //Router responded with one or more errors. Output them.
    foreach ($errors as $error) {
        echo '(', $error->getArgument('category'), ') ', $error->getArgument('message');
    }
} else {
    //Entry was added without errors
    $id = $responses->getLast()->getArgument('ret');
    //$id now contains the new entry's ID
} 

i like last option better due to traps actually return usable information.

thanks for the reply … but I can not make it work PEAR2_Net_RouterOS api-1.0.0b4 … will not have a guide for newbies?? I’m using xp and AppServ 2.5.10

regards

That AppServ version has a PHP version that is too old - 5.2.6, when this client requires 5.3.0 at minimum (keeping in mind that the latest version right now is 5.5.4(!!!)).

I’d recommend getting another package that’s been more recently updated, like XAMPP or WampServer, or better yet, a manual installation of Apache with the ApacheLounge binaries (for XP, you’d want the VC10 x32 ones) + the official PHP binary from the dedicated PHP site (for XP, pick the latest 5.4 release).

If, for whatever reason, you really want AppServ, you should contact its authors, asking them to update it.