Community discussions

MikroTik App
 
JoshSullivan20
just joined
Topic Author
Posts: 8
Joined: Fri Mar 20, 2020 9:54 am

PHP API Login Method Example [Help Please]

Fri Mar 20, 2020 10:09 am

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:
Image

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!
Last edited by JoshSullivan20 on Sat Mar 21, 2020 6:58 pm, edited 1 time in total.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6196
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: PHP API Login Method Example

Fri Mar 20, 2020 10:17 am

send in initial login message
/login
=name=user
=password=xxx
 
JoshSullivan20
just joined
Topic Author
Posts: 8
Joined: Fri Mar 20, 2020 9:54 am

Re: PHP API Login Method Example

Fri Mar 20, 2020 10:27 am

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;

	}
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6196
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: PHP API Login Method Example

Fri Mar 20, 2020 10:37 am

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);
 
JoshSullivan20
just joined
Topic Author
Posts: 8
Joined: Fri Mar 20, 2020 9:54 am

Re: PHP API Login Method Example

Fri Mar 20, 2020 10:49 am

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 :(
 
JoshSullivan20
just joined
Topic Author
Posts: 8
Joined: Fri Mar 20, 2020 9:54 am

Re: PHP API Login Method Example

Fri Mar 20, 2020 12:00 pm

Also guys, any reason why this page in unavailable?
https://wiki.mikrotik.com/wiki/API_PHP_class
 
JoshSullivan20
just joined
Topic Author
Posts: 8
Joined: Fri Mar 20, 2020 9:54 am

Re: PHP API Login Method Example

Fri Mar 20, 2020 6:30 pm

Oh,
Image
 
JoshSullivan20
just joined
Topic Author
Posts: 8
Joined: Fri Mar 20, 2020 9:54 am

Re: PHP API Login Method Example

Sat Mar 21, 2020 6:52 pm

*bump*

Has anyone got a working PHP API connection?

Please :)
 
Sob
Forum Guru
Forum Guru
Posts: 6517
Joined: Mon Apr 20, 2009 9:11 pm

Re: PHP API Login Method Example [Help Please]

Sun Mar 22, 2020 1:45 am

Are you sure you have correct password, you are connecting to right router, etc? Because your code with mrz's update works.
Excessive quoting is useless and annoying. If you use it, please consider if you could do without it.
 
JoshSullivan20
just joined
Topic Author
Posts: 8
Joined: Fri Mar 20, 2020 9:54 am

Re: PHP API Login Method Example [Help Please]

Mon Mar 23, 2020 11:18 am

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

Cheers :)
 
Sob
Forum Guru
Forum Guru
Posts: 6517
Joined: Mon Apr 20, 2009 9:11 pm

Re: PHP API Login Method Example [Help Please]  [SOLVED]

Tue Mar 24, 2020 3:48 am

Here it is. It has slightly different formatting, but it's the same code.
You do not have the required permissions to view the files attached to this post.
Excessive quoting is useless and annoying. If you use it, please consider if you could do without it.
 
JoshSullivan20
just joined
Topic Author
Posts: 8
Joined: Fri Mar 20, 2020 9:54 am

Re: PHP API Login Method Example [Help Please]

Tue Mar 24, 2020 6:17 pm

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! :P)
 
lupael
just joined
Posts: 3
Joined: Sat Jul 21, 2018 5:58 am
Location: Bangladesh
Contact:

Re: PHP API Login Method Example [Help Please]

Sun Aug 02, 2020 2:25 am

Here it is. It has slightly different formatting, but it's the same code.
Getting login failure massage
 
seanfilipin
just joined
Posts: 2
Joined: Wed Oct 21, 2015 7:44 am

Re: PHP API Login Method Example [Help Please]

Tue Feb 23, 2021 9:44 pm

Here it is. It has slightly different formatting, but it's the same code.
Thanks mate! I was trying to make the connection work finally, due to your code, it works!
Thank you very much!

Sean.

Who is online

Users browsing this forum: No registered users and 22 guests