Form to connect Mikrotik PEAR2 API PHP

hi guys, im here again :S
Im trying create a form to connect a router mikrotik:

<?php //ConectarAPI use PEAR2\Net\RouterOS; // require_once 'pear2\src\PEAR2\Autoload.php'; require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar'; //Formulario echo "
Conectar
"; //CONEXION A MIKROTIK $client = new RouterOS\Client('$ip', '$username', '$password'); ?>

Its something like that, but i cant connect, and i have this alert: “Fatal error: Uncaught exception ‘PEAR2\Net\Transmitter\SocketException’ with message 'stream_socket_client(): php_network_getaddresses: getaddrinfo failed: No such host is known. ’ in phar://C:/wamp/www/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/RouterOS/Communicator.php on line 151”
I think that API is triyng to connect without values :S

Thanks :slight_smile:

The single quotes mean the string is passed literally, rather than variables being interpolated.

In other words,

'$ip' 

refers to a hostname called “$ip”, which is obviously not a valid IP or domain name.

Remove all apostrophes to pass the actual variable values, i.e.

$client = new RouterOS\Client($ip, $username, $password); 

Hmmm i want this form for the last scripts, to choose what router i want list the ppp’s connections, and i still having alerts :S


Something like: <?php
//ConectarAPI
use PEAR2\Net\RouterOS;
// require_once ‘pear2\src\PEAR2\Autoload.php’;
require_once ‘PEAR2_Net_RouterOS-1.0.0b4.phar’;

//Formulario
echo "

IP Usuario Password Conectar
";

//CONEXION A MIKROTIK
$client = new RouterOS\Client($ip, $username, $password);

… (Rest of code)And when i press the button, connect me with the router and print the list

pd. The form should be able alltime, to change the ip and list other router

That’s not how PHP works…

When you first open up a page, PHP runs from the start to end of the file (without any breaks) and it stops. In your browser, you then eventually submit a form that again executes everything from start to end, without any recollection of what happened during any previous requests.

You’d need to use sessions (here’s a tutorial and another one) to in essence, create a dedicated login form, which would store the entered credentials into the session. You can then retrieve those session values from $_SESSION, and fill in the constructor with them. It should go without saying that you can of course modify those later, but the point is to store & retrieve them, so that they’re remembered for a while once entered.