PEAR2 API Help

I have been trying to run the pear2 api for some time now and keep getting this error message.

/PEAR2/PEAR2_Net_RouterOS-1.0.0b3.phar/PEAR2_Net_RouterOS-1.0.0b3/src/PEAR2/Autoload.php on line 181

is there some way to resolve this?

best,
Patrick.

When using the PHAR archive, you only need to include the PHAR archive itself, not the Autoload.php file from it - the PHAR stub will take care of loading the Autoload.php file.

So just have

<?php

require_once 'PEAR2/PEAR2_Net_RouterOS-1.0.0b3.phar';
 

in your PHP file.

Either way, what exactly is the error message? You’re only showing the file and line it occurred at.

Here is the full error.

Fatal error: Uncaught exception ‘Exception’ with message ‘Class RouterOS\Client could not be loaded from RouterOS/Client.php, file does not exist (registered paths=“phar:///home/inkspotw/public_html/cloud/monitor/mikrotik/PEAR2_Net_RouterOS-1.0.0b3.phar/PEAR2_Net_RouterOS-1.0.0b3/src”) [PEAR2_Autoload-0.2.4]’ in phar:///home/inkspotw/public_html/cloud/monitor/mikrotik/PEAR2_Net_RouterOS-1.0.0b3.phar/PEAR2_Net_RouterOS-1.0.0b3/src/PEAR2/Autoload.php:181 Stack trace: #0 [internal function]: PEAR2\Autoload::load(‘RouterOS\Client’) #1 /home/inkspotw/public_html/cloud/monitor/mikrotik/test.php(4): spl_autoload_call(‘RouterOS\Client’) #2 {main} thrown in phar:///home/inkspotw/public_html/cloud/monitor/mikrotik/PEAR2_Net_RouterOS-1.0.0b3.phar/PEAR2_Net_RouterOS-1.0.0b3/src/PEAR2/Autoload.php on line 181

It looks like you haven’t defined the “use” declaration at the top. It needs to be like:

<?php

//NOTE THE LINE BELOW
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
 

Without the “use” line, you’re targeting a class with the FULL name “RouterOS\Client”, which, as per the error message, doesn’t exist. The actual class that exists is “PEAR2\Net\RouterOS\Client”. With the “use” line, you can make it easier on yourself - after that line, rather than typing the full name, you’d type just "RouterOS", and "PEAR2\Net\RouterOS" will be assumed. See the PHP manual page about namespace aliasing for details on the mechanism if it’s new for you.

Thank you for your response. I now seem to be getting another error?

Fatal error: Uncaught exception ‘PEAR2\Net\Transmitter\SocketException’ with message ‘Failed to initialize socket.’ in phar:///home/inkspotw/public_html/cloud/monitor/mikrotik/PEAR2_Net_RouterOS-1.0.0b3.phar/PEAR2_Net_RouterOS-1.0.0b3/src/PEAR2/Net/Transmitter/TcpClient.php:140 Stack trace: #0 phar:///home/inkspotw/public_html/cloud/monitor/mikrotik/PEAR2_Net_RouterOS-1.0.0b3.phar/PEAR2_Net_RouterOS-1.0.0b3/src/PEAR2/Net/Transmitter/TcpClient.php(118): PEAR2\Net\Transmitter\TcpClient->createException(‘Failed to initi…’, 7) #1 phar:///home/inkspotw/public_html/cloud/monitor/mikrotik/PEAR2_Net_RouterOS-1.0.0b3.phar/PEAR2_Net_RouterOS-1.0.0b3/src/PEAR2/Net/RouterOS/Communicator.php(111): PEAR2\Net\Transmitter\TcpClient->__construct(‘MYIP’, 8728, false, NULL, ‘admin/MYPASS’, NULL) #2 phar:///home/inkspotw/public_html/cloud/monitor/mikrotik/PEAR2_Net_RouterOS-1.0.0b3.phar/PEAR2_Net_RouterOS-1.0.0b3/src/PEAR2/Net/RouterOS/Client.php(121): PEAR2\Net\RouterOS\Communicator->__construct(‘MYIP’, 8728, false, NUL in phar:///home/inkspotw/public_html/cloud/monitor/mikrotik/PEAR2_Net_RouterOS-1.0.0b3.phar/PEAR2_Net_RouterOS-1.0.0b3/src/PEAR2/Net/RouterOS/Communicator.php on line 113

No problem :slight_smile: .

That new message is… better, let’s say.

If you’re sure the API service is enabled on the router, the other likely possibility is that your web server is not allowed to make outgoing connections.

Try disabling your web server’s firewall to be sure, and if it works then, it means you’d need to add “php-cgi” to the list of applications allowed to make outgoing connections, and only then turn back the firewall on. That, or the Apache executable if PHP is running as an Apache module.

On a related note… do you have that kind of a control over your web server, or is this a web host that doesn’t provide that much of a control? There’s a relatively easy workaround if you don’t have that control - move the API service to a different port, normally used for HTTP or HTTPS (i.e. 80 or 443), and specify that port as the 4th argument, after the password. Of course, doing THAT comes at the price of you sacrificing your router’s ability to serve HTTP or HTTPS pages on said port, making HTTP serving from it less convenient.

that was exactly right. made a NAT rule on the router to translate port 8728 to 443 this solved the host issue and the router will still be accessible on port 80 so this is a healthy solution to me.

Thank you for your help.

made a NAT rule on the router to translate port 8728 to 443

Wait, what?

There’s no need for a NAT rule. In fact, doing that will make things slower… just use
/ip service set api port=443from a terminal to change the ACTUAL port the API runs on. That way, there’s no NAT-ting involved (in fact, you’ll have to remove said NAT rule), and thus your router will work better.



The only scenario where a NAT could be useful is if you want HTTPS to be available locally, but not publically which… actually come to think of it is probably what you may eventually want anyway, but still - if you don’t need it NOW, it’s better to use the port directly, rather than through a NAT.