RouterOS v7.6 php api changed

Hello, I need some advice of php developer. Recently I have upgrade my RB4011 to v7.6
Now I am unable to retrieve the voltage and temperature of my router in php.
This code properly worked on my RouterOS v6 .

<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';
require_once 'email.php';
try {
    $client = new RouterOS\Client('10.2.3.3', 'admin', 'admin771');
} catch (Exception $e) {
   
 die('Unable to connect to the router.');
    //Inspect $e if you want to know details about the failure.
}

$responses = $client->sendSync(new RouterOS\Request('/system/health/print'));
foreach ($responses as $response) {
    if ($response->getType() === RouterOS\Response::TYPE_DATA) {
      $voltage=  $response->getProperty('voltage');
	   $temp=  $response->getProperty('temperature');
    }
}
echo 'Voltage: ',  $voltage, 'V';
echo "<br>";
echo 'Temperature: ', $temp, 'C';

The output of RouterOS v6.49

 /system health print 
voltage: 11.9V
temperature: 28C

Now in RouterOS v7.6
the output is bit different

/system/health/print 
Columns: NAME, VALUE, TYPE
#  NAME         VALUE  TYPE
0  voltage      27.2   V   
1  temperature  44     C

If someone could help with php code how I can “echo” the voltage and temperature on RouterOS v7.6

thank you

Here is one way to do it. (I guess rextended will comment the on-error :smiley: )
On RouterOS v7 temperatur and volt are stored differently, so this script get it from both version.
Look at my complete script for Splunk here:
http://forum.mikrotik.com/t/tool-using-splunk-to-analyse-mikrotik-logs-4-0-graphing-everything/153043/1

:do {
	# New version
	:foreach id in=[/system health find] do={
		:local health "$[/system health get $id]"
		:set ( "$health"->"script" ) "health"
		:log info message="$health"
	}
} on-error={
	# Old version
	:if (!([/system health get]~"(state=disabled|^\$)")) do={
		:local health "$[/system health get]"
		:set ( "$health"->"script" ) "health"
		:log info message="$health"
	}
}

Updated version that checks for main RouterOS version, to select the correct part to run:


{
:local train [:tonum [:pick [/system resource get version] 0 1]] 
:if ($train > 6) do={
	# New version (RouterOS >6)
	:foreach id in=[/system health find] do={
		:local health "$[/system health get $id]"
		:set ( "$health"->"script2" ) "health"
		:log info message="$health"
	}
} else={
	# Old version (RouterOS 6 or older)
	:if (!([/system health get]~"(state=disabled|^\$)")) do={
		:local health "$[/system health get]"
		:set ( "$health"->"script2" ) "health"
		:log info message="$health"
	}
}
}

@jotne thank you but I need php code.

Hello Everyone,
I have two mikrotik routers version 6.42 and 6.49.My php api codes works for 6.42 but for 6.49,it shows an error “invalid username or password”. I searched. on google and found out there’s an updated php api to access Mikrotik Router OS for higher versions
pls, how did you connect to version 7.6?

Probably you are using routeros_api.class.php and updated while ago.
Take a look at https://github.com/BenMenking/routeros-api/blob/master/routeros_api.class.php
It’s backward routeros compatible.

@id
thanks a lot for the update. I am a bit confused. I am using pear2/net/router os. So i should discard that and work with routeros_api.class.php. Do i need to install composer? because i am seeing composer.json file among the files in the link you sent to me
i await your kind response

My bad. I didn’t think that you are using pear.
Follow https://github.com/pear2/Net_RouterOS

@id
thanks for the swift response.I have checked the link. I have downloaded the files.I will try them tomorrow.
Meanwhile,i checked one of files and found this

use PEAR2\Net\RouterOS;

require_once ‘PEAR2/Autoload.php’;

try {
$client = new RouterOS\Client(‘192.168.88.1’, ‘admin’, ‘password’);
} catch (Exception $e) {
die(‘Unable to connect to the router.’);
}
The above codes are the same with the old version.Will it work with the newer versions?

@ID
sorry to bother you again. The routeros_class_api.php is connecting to mikrotik router unlike pear2/net/routeros. I want to set the password of an existing user in the usermanager. I have been able to get .id but i cant store it in a variable
see code below

<?php require('../routeros_api.class.php'); $API = new routerosAPI(); $API->debug = true; $username = 'diye'; if ($API->connect('172.16.7.3', 'admin', 'password@1')) { $API->write('/tool/user-manager/user/print', false); $API->write('=.proplist=.id', false); //print_r($edituser); $API->write('?username='.$username .' '); $API->write('/tool/user-manager/user/set', false); $API ->write('=.id=*13', false); $API->write('=password=1506'); $READ = $API->read(false); $ARRAY = $API->parseResponse($READ); print_r($ARRAY); $API->disconnect(); } ?>

Connection attempt #1 to 1.1.1.1:8728… <<< [6] /login <<< [11] =name=admin <<< [20] =password=password >>> [5/5] bytes read. >>> [5, 1]!done Connected… <<< [29] /tool/user-manager/user/print <<< [14] =.proplist=.id <<< [14] ?username=diye <<< [27] /tool/user-manager/user/set <<< [8] =.id=*13 <<< [14] =password=1506 >>> [3/3] bytes read. >>> [3, 24]!re >>> [8/8] bytes read. >>> [8, 15]=.id=*13 >>> [5/5] bytes read. >>> [5, 8]!done >>> [5/5] bytes read. >>> [5, 1]!done Array ( [0] => Array ( [.id] => *13 ) ) Disconnected… Disconnected…

I have used python paramiko lib to update users to mikrrotik usermanager.It’s easier for me