PHP API Login Method Example [Help Please]

Hello,

I am trying to login to a MikroTik router using an API, I believe the PHP in the current API is outdated and doesn’t match the new login method.

Current code:

When the API is run the following error is displayed in the MikroTik terminal.

system,error,critical login failure for user admin from 192.168.1.198 via API

Would someone be able to provide a working login example via a PHP API?

Thanks!

send in initial login message
/login
=name=user
=password=xxx

Thanks for your fast response, however I’m still stuck. Getting the same login failure.

My current login method is:

	function connect($ip, $login, $password) {

		for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {

			$this->connected = false;

			$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $ip . ':' . $this->port . '...');

			if ($this->socket = @fsockopen($ip, $this->port, $this->error_no, $this->error_str, $this->timeout) ) {

				socket_set_timeout($this->socket, $this->timeout);

				$this->write('/login');
				$this->write('=name=' . $login); 
				$this->write('=password=' . $password);

				$RESPONSE = $this->read(false);

				if ($RESPONSE[0] == '!done') {

					$this->connected = true;

					break;

				}			

				fclose($this->socket);

			}

			sleep($this->delay);

		}

		if ($this->connected)
			$this->debug('Connected...');
		else
			$this->debug('Error...');

		return $this->connected;

	}

I do not know anything about php api, but shouldn’t it be like this?

$this->write('/login', false);
$this->write('=name=' . $login, false); 
$this->write('=password=' . $password);

I’m not to sure on this PHP, I’ve updated the code like you suggested, same login failure :frowning:

Also guys, any reason why this page in unavailable?
https://wiki.mikrotik.com/wiki/API_PHP_class

Oh,

bump

Has anyone got a working PHP API connection?

Please :slight_smile:

Are you sure you have correct password, you are connecting to right router, etc? Because your code with mrz’s update works.

Please could you send me this code that you said works?

Cheers :slight_smile:

Here it is. It has slightly different formatting, but it’s the same code.
routeros_api.class.php.txt (12.8 KB)

Thanks for that! It’s working now.

(+ it turns out I was editing the wrong API class file as there were multiple for some reason! :stuck_out_tongue:)

Thanks mate! I was trying to make the connection work finally, due to your code, it works!
Thank you very much!

Sean.

Resuelto, muchas gracias!!