mikrotik to synchronize data from combobox with php api??

Greetings I hope someone knows help me I’m using mikrotik api with php but I want to get data from the profiles of the hotspot to place them in a combobox to select the profiles so who are the time to add a user that someone will know something thanks in advance

What JavaScript library are you using to create the combo box?

Typically, libraries provide some sort of a property on the combo box that you populate with all possibilities and/or they ask you to use a dropdown that they transform into a text field that acts like a combo box.

Without knowing the library, the only thing I can say is that you can get all of the existing profiles with “/ip/hotspot/user/profile/print”, but I bet you already knew that.

jejejeje sorry and thanks for answer..im using API mikrotik, you’re right is “dropdown” im mistake…but i don´t know how get tha data from profiles and show in the dropdown…i have no idea :frowning:

Like I said, you get them with “/ip/hotspot/user/profile/print”, so in the end, the code to get a dropdown would look something like this:

<select>
<?php
foreach ($client(new Request('/ip hotspot user profile print .proplist=name'))->getAllOfType(Response::TYPE_DATA) as $response) {
    echo '<option>', $response->getArgument('name'), '</option>';
}
?>
</select>

(the “getAllOfType” part is to filter out the last “!done” response; without it, you’ll have an empty option at the end, and you probably don’t want that)

ok im try whit api mikrotik that code thanks…and this same can use whit another print ???

Sure. Just remember to adjust the “.proplist” so that it contains all arguments you intend to use (or simply remove the whole “.proplist” part).

ok thanks im try and tell you later…

do not show me the page does not I did wrong here qe show you the code I’m using for a client’s income

here is just part of the combobox where I want the profiles

<select>
 <?php
require('routeros_api.class.php');
$API = new routeros_api();
$API->debug = false;
if ($API->connect('nukeko.******.info', '******', '*****')) {
foreach ($client(new Request('/ip/hotspot/user/profile/print .proplist=name'))->getAllOfType(Response::TYPE_DATA) as $response) {
    echo '<option>', $response->getArgument('name'), '</option>';
		  }
?>

          </select>

I hope you know how help me

Oh. I’m sorry. Given your latest reply in your previous topic, I naively assumed you successfully started using my client, so my suggestion was tailored to it, not to Denis’ class.

With Denis’ class, it’s still the same command, only the sending itself and the traversal of the responses is a little different.

:frowning: i dont now how use you class…i use Denis’ class… :blush: :blush:

How to use my class:

  1. Download one of the two files linked from my signature. TGZ or ZIP… they both work, their contents are equivalent.
  2. Extract the whole “src” folder somewhere on your server. Doesn’t matter where… In the example below, I assume you have extracted it in the same folder as your PHP file.
  3. At the top of your file, change
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 

to

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';
?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  1. Near the select part, replace
<?php
require('routeros_api.class.php');
$API = new routeros_api();
$API->debug = false;
if ($API->connect('nukeko.*****.info', '*****', '******')) {
 

with

<?php
$client = new Client('nukeko.*****.info', '*****', '******'); 

ok im to try whit you api thanks for you help

ok look the code…

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';
?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
</head>

<body>
<?php
$client = new Client('nukeko.*****.info', '*****', '******');
?>
<select>
<?php
foreach ($client(new Request('/ip hotspot user profile print .proplist=name'))->getAllOfType(Response::TYPE_DATA) as $response) {
    echo '<option>', $response->getArgument('name'), '</option>';
}
?>
</select>

</body>
</html>

im try but no show anything…be missing something but don´t know … if you have someting information about you api I like to see many thanks

There are two possible problems:

  1. Your path to Autoload.php is wrong. In other words, if your file is at “/home/users/nukeko/public_html/index.php”, Autoload.php should be at “/home/users/nukeko/public_html/PEAR2/Net/RouterOS/Autoload.php”, but that’s not the place where you have it.
  2. You have a PHP version earlier than PHP 5.3.0.

Let’s try and see which one it is…

  1. At the top, change
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';
?>

to

<?php
namespace PEAR2\Net\RouterOS;
if (is_file('PEAR2/Net/RouterOS/Autoload.php')) {
require_once 'PEAR2/Net/RouterOS/Autoload.php';
} else {
die('The path to Autoload.php is wrong!');
}
?>

And see if this results in

The path to Autoload.php is wrong!

If it does, then adjust the path to that file… use an absolute path if you have to.

  1. If you still don’t see anything after attempting the above, then replace that same part with
<?php
phpinfo();
die();
?>

and check if your PHP version is at least PHP 5.3.0.
If it’s not, you should ask your host to upgrade your PHP version… which they should do anyway.

and .. the same port using the api?

If you need to use another port, you can specify it as the 4th argument of Client (after the password), but by default… yes, it’s the same port as the default API port - 8728.

If you’ve successfully used Denis’ class before, this shouldn’t be the problem though, since it too uses the same port by default. Try doing the things above.

I understand but you have a single line where you see the connection with the mikrotik? and I need to use it on a form but I’m stuck in obtaining data from the dropdown mikrotik

thanks for your attention

Do you see an empty dropdown, or do you see nothing (as in “a big white nothing”; no dropdown, no text, no tables, no nothing) on the page? The suggestions above were about when you’re seeing nothing (as you said before).

If you’re seeing nothing on the page, then you may not be connecting to the router because you’ve either not included the autoloader properly, or you’re not using PHP 5.3.0. You can find out if that’s the problem by doing the above. Do it already…

If you’re seeing an empty dropdown, that’s not it. The problem is something totally different… like maybe you don’t have a single profile to be displayed.

look here http://www.nukeko.com.ve/lanaze/test3.php

and also did the test path and place the lines and I went blank, to stand the test of folding select from the list with the code you gave me everything I get blank display anything … I’ve done forms with your application as well as I do?

once again thanks

Yes, the form code is OK… I tested it on my machine and router, and it works.

Also, great, the phpinfo() output makes it clear you have PHP 5.3.8, which is perfect… precisely the version I have, so we also know it’s not a PHP problem.

But yeah, I also checked the “debugging” code I gave you, and THAT fails… so… let’s see…


<<tinkering…>>

Ah… yes, so… replace

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';
?>

with

<?php
namespace PEAR2\Net\RouterOS;
$autoloaderPath = stream_resolve_include_path('PEAR2/Net/RouterOS/Autoload.php');
if (is_file($autoloaderPath)) {
    require_once $autoloaderPath;
} else {
    die("The path to Autoload.php is wrong!");
}
?>

This will make sure the client is loaded properly. You should be seeing the error message otherwise.



<>
It seems the path to Autoload.php is OK (I guess by the fact I did not get a 404)… let’s dig deeper. Try this file:

<?php
namespace PEAR2\Net\RouterOS;
ini_set('display_errors', 'On');
$autoloaderPath = stream_resolve_include_path('PEAR2/Net/RouterOS/Autoload.php');
if (is_file($autoloaderPath)) {
    require_once $autoloaderPath;
} else {
    die("The path to Autoload.php is wrong!");
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
</head>

<body>
<?php
try {
$client = new Client('nukeko.*****.info', '*****', '******');
}catch(\Exception $e) {
    die('Failed connecting to the router. Details: ' . (string) $e);
}
?>
    <p>Connected...</p>
    <p>Fetching list...</p>
    <pre><?php
    
$fullList = $client(new Request('/ip hotspot user profile print .proplist=name'));
var_dump($fullList->toArray());
    ?></pre>
    <p>Filtering list...</p>
    <pre><?php
    $filteredList = $fullList->getAllOfType(Response::TYPE_DATA);
    var_dump($filteredList->toArray());
    ?></pre>
<select>
<?php
foreach ($filteredList as $response) {
    echo '<option>', $response->getArgument('name'), '</option>';
}
?>
</select>

</body>
</html>

It intentionally has debugging info all over the place, to pinpoint the point of failure.