Community discussions

MikroTik App
 
binhorp
newbie
Topic Author
Posts: 39
Joined: Wed Mar 05, 2008 4:40 pm

import and fetch via API

Sun Jul 28, 2013 7:03 pm

Hello everyone!
Do I need to dowload a aquivo and run an import that file all this via php api eg
/ tool fetch url = "192.168.1.1/script.rsc"
/ import file-name = script.rsc

I'll be very grateful if someone can help me
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: import and fetch via API

Sun Jul 28, 2013 11:07 pm

Here it is with the client from my signature:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

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

$client(new Request('/tool fetch url="192.168.1.1/script.rsc"'));
$client(new Request('/import file-name=script.rsc')); 
Assuming you have the API service enabled, and have your firewall allow PHP to make outgoing connections... yes, it's as trivial as that.
 
binhorp
newbie
Topic Author
Posts: 39
Joined: Wed Mar 05, 2008 4:40 pm

Re: import and fetch via API

Mon Jul 29, 2013 3:16 am

I am very grateful for your help, but I could not install this API in my php, when I try to use it I only get a white screen, like something like
$ API-> write ('/ tool fetch url = "http192.168.1.1 / / script.rsc"', true);
$ ARRAY = $ API-> read ();
very grateful to all
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: import and fetch via API

Mon Jul 29, 2013 3:09 pm

Like I said, the above is with the client from my signature, i.e. this one. Do not use this one. It's different with it.
 
binhorp
newbie
Topic Author
Posts: 39
Joined: Wed Mar 05, 2008 4:40 pm

Re: import and fetch via API

Mon Jul 29, 2013 3:27 pm

I tried using the following way
<?php require_once("$DOCUMENT_ROOT/lib/PEAR2_Net_RouterOS-1.0.0b3.phar");
use PEAR2\Net\RouterOS;?>
but it did not work I think something is missing in my php, thanks for the reply
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: import and fetch via API

Mon Jul 29, 2013 5:14 pm

You must use it in the way shown above.

Instead of
<?php require_once("$DOCUMENT_ROOT/lib/PEAR2_Net_RouterOS-1.0.0b3.phar");
use PEAR2\Net\RouterOS;?>
use
<?php
use PEAR2\Net\RouterOS;
require_once("$DOCUMENT_ROOT/lib/PEAR2_Net_RouterOS-1.0.0b3.phar");
?>
If that doesn't work, please show the error message you get.
 
binhorp
newbie
Topic Author
Posts: 39
Joined: Wed Mar 05, 2008 4:40 pm

Re: import and fetch via API

Mon Jul 29, 2013 6:10 pm

Oops sorry was pretty gross error on my part, but still just keep getting white screen when I add the lines
<? Phpuse PEAR2 \ Net \ RouterOS;
require_once ("$ DOCUMENT_ROOT/lib/PEAR2_Net_RouterOS-1.0.0b3.phar");?>
I must be missing something in my php but have not figured out what it is, still in the battle, thanks
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: import and fetch via API

Mon Jul 29, 2013 6:31 pm

Your PHP must be at least 5.3.0.

If it's earlier, it would cause a parse error.

In a separate file, run
<?php phpinfo(); ?>
and check the top of the outputted page to see your PHP version.


P.S. Please stop using "Google Translate" on your code.
 
binhorp
newbie
Topic Author
Posts: 39
Joined: Wed Mar 05, 2008 4:40 pm

Re: import and fetch via API

Mon Jul 29, 2013 10:01 pm

PHP Version 5.3.3-7+squeeze16

System Linux dns1 2.6.32-5-686 #1 SMP Thu Nov 3 04:23:54 UTC 2011 i686
Build Date Jul 17 2013 17:02:01
Server API Apache 2.0 Handler
Additional .ini files parsed /etc/php5/apache2/conf.d/gd.ini, /etc/php5/apache2/conf.d/mcrypt.ini, /etc/php5/apache2/conf.d/mysql.ini, /etc/php5/apache2/conf.d/mysqli.ini, /etc/php5/apache2/conf.d/pdo.ini, /etc/php5/apache2/conf.d/pdo_mysql.ini, /etc/php5/apache2/conf.d/pdo_pgsql.ini, /etc/php5/apache2/conf.d/pgsql.ini, /etc/php5/apache2/conf.d/ssh2.ini, /etc/php5/apache2/conf.d/suhosin.ini
PHP API 20090626
PHP Extension 20090626
Zend Extension 220090626
Zend Extension Build API220090626,NTS
PHP Extension Build API20090626,NTS
Debug Build no
Thread Safety disabled
Zend Memory Manager enabled
Zend Multibyte Support disabled
IPv6 Support enabled
Registered PHP Streams https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip, ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp
Registered Stream Socket Transports tcp, udp, unix, udg, ssl, sslv3, sslv2, tls
Registered Stream Filters zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, mcrypt.*, mdecrypt.*
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: import and fetch via API

Tue Jul 30, 2013 2:03 pm

In that case, $DOCUMENT_ROOT is probably undefined in that file.

Try to be explicit the first time around, and also add a message to let you know if things are OK, e.g.
<?php
use PEAR2\Net\RouterOS;
require_once '/home/username/public_html/lib/PEAR2_Net_RouterOS-1.0.0b3.phar';

echo 'OK so far.';
?>
 
binhorp
newbie
Topic Author
Posts: 39
Joined: Wed Mar 05, 2008 4:40 pm

Re: import and fetch via API

Tue Jul 30, 2013 2:53 pm

<?php
use PEAR2\Net\RouterOS;
require_once '/home/bakanas/sistema/web/html/lib/PEAR2_Net_RouterOS-1.0.0b3.phar';
echo 'Test OK';
?>
Only white screen, is there any command that I can check if all the requisites are met in my php?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: import and fetch via API

Tue Jul 30, 2013 3:31 pm

Well, strictly speaking, you can run the ".phar" file itself, and you'll get diagnostics, but the only thing the PHAR will tell you is if your version is right, and we already established that's not the problem.

But the only problems you can have with a file that contains only these lines are:
1. Version earlier than 5.3.0 - We've already established that's not the case.
2. Wrong path to the ".phar" file.
3. Path is right, but not readable.


OK, let's see which one it is. Create a new file, and run the following in it:
<?php
$phar = '/home/bakanas/sistema/web/html/lib/PEAR2_Net_RouterOS-1.0.0b3.phar';

if (is_file($phar)) {
    if (is_readable($phar)) {
        if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
            echo 'WTF? All requirements are OK...';
        } else {
            echo 'Path to PHAR file is correct and readable, but PHP version is earlier than 5.3.0. Maybe your earlier phpinfo() run was from a different SAPI.';
        }
    } else {
        echo 'Path to PHAR file is correct, but unreadable by PHP.';
    }
} else {
    echo 'Wrong path to PHAR file.';
} 
 
binhorp
newbie
Topic Author
Posts: 39
Joined: Wed Mar 05, 2008 4:40 pm

Re: import and fetch via API

Wed Jul 31, 2013 12:07 am

WTF? All requirements are OK...
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: import and fetch via API

Wed Jul 31, 2013 2:52 pm

Then there's something else you're having in addition to the previous codes and are not showing, and that is what is causing the error.

Run only the following code in a new file:
<?php
use PEAR2\Net\RouterOS;
require_once '/home/username/public_html/lib/PEAR2_Net_RouterOS-1.0.0b3.phar';

echo 'OK so far.';
?>
And make sure "<?php" is the very first thing in that file - no whitespace or anything of the sort, and also don't have anything after that code, and do not "Google Translate" it.



If even that doesn't work... try
<?php
require_once '/home/username/public_html/lib/PEAR2_Net_RouterOS-1.0.0b3.phar';

echo 'Inclusion OK.';
try {
    $client = new \PEAR2\Net\RouterOS\Client('192.168.0.1', 'admin', 'password');

    echo 'Connection OK';
} catch (Exception $e) {
    echo 'Connection FAIL.';
} 
(it's ugly, but we'll eliminate the "use" as a possible parser hick-up)
 
binhorp
newbie
Topic Author
Posts: 39
Joined: Wed Mar 05, 2008 4:40 pm

Re: import and fetch via API

Fri Aug 02, 2013 3:17 am

Buddy I am very grateful for your attention, solved the problem of white screen was allowed in the file, I now have the following problems you have no idea what can be?
Fatal error: Uncaught exception 'PharException' with message 'manifest cannot be larger than 100 MB in phar "/home/bakanas/sistema/web/html/lib/PEAR2_Net_RouterOS-1.0.0b3.phar"' in /home/bakanas/sistema/web/html/lib/PEAR2_Net_RouterOS-1.0.0b3.phar:3 Stack trace: #0 /home/bakanas/sistema/web/html/lib/PEAR2_Net_RouterOS-1.0.0b3.phar(3): Phar::mapPhar() #1 /home/bakanas/sistema/web/html/teste_pear.php(8): require_once('/home/bakanas/s...') #2 {main} thrown in /home/bakanas/sistema/web/html/lib/PEAR2_Net_RouterOS-1.0.0b3.phar on line 3
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: import and fetch via API

Fri Aug 02, 2013 7:11 am

Whoa.

That's the first time ever I've seen such an error.

It seems then that what's happening here is a bug in this particular PHP version, perhaps occurring only on your particular platform/build, as the changelog for PHP versions later than 5.3.3 don't seem to contain any fixes for that sort of thing.

If you're on a web host, ask them to upgrade PHP, or upgrade it yourself if they allow to do that over cPanel or whatever. Any PHP 5.4 should be fine if they don't allow specific 5.3 variants.


There's also another way, if upgrading PHP is not an option - instead of using the PHAR file, download and extract one of the other two files (the TGZ or ZIP). Copy the contents of the "src" folder wherever you'd place PEAR related stuff, and include the "PEAR2/Autoload.php" file.
 
Beelze
Frequent Visitor
Frequent Visitor
Posts: 60
Joined: Tue Mar 04, 2014 12:21 pm

Re: import and fetch via API

Tue May 27, 2014 2:27 pm

I have done the same thing, but then with the 2nd API you listed.

Separately, both commands work perfectly. But after putting them together after each other, nothing works.
//fetch configuration file from server
   	$API->write('/tool/fetch', false);
	$API->write('=address=address', false);
    $API->write('=src-path='.$_SESSION['load_ConfigFile'].'', false);
    $API->write('=user=marijn', false);
    $API->write('=mode=https', false);
    $API->write('=password=password', false);
    $API->write('=dst-path=config.rsc', false);
    $API->write('=port=443', false);
    $API->write('=keep-result=yes');
	   
	// import and execute configuration file
   
	$API->write('/import', false);
	$API->write('=file-name=config.rsc');
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: import and fetch via API

Tue May 27, 2014 7:05 pm

At the time of my first post, I really thought that's all there is to it, but upon some more recent tests, I also know that you need to give the router 2 seconds between the supposed creation of the file, and the time it is actually made available for reading or writing, including being readable by other commands.

So... do a read() after the first sentence, do
sleep(2);
and it should work then.
 
int21
just joined
Posts: 23
Joined: Thu Apr 16, 2009 1:31 am
Location: Brazil
Contact:

Re: import and fetch via API

Thu Nov 25, 2021 5:46 pm

man can you helpme?
in this case:

$client = new Client('xx.xx.xx.xx', 'admin', '1234');
$client(new Request('/tool fetch url="xx.xx.xx.xx/nt2/x.rsc"'));
$client(new Request('/import file-name=x.rsc'));

I have:
Fatal error: Uncaught Exception: Class Client could not be loaded from Client.php, file does not exist (registered paths="/var/www/html/nt2") [PEAR2_Autoload-@PACKAGE_VERSION@] in /var/www/html/nt2/PEAR2/Autoload.php:305 Stack trace: #0 [internal function]: PEAR2\Autoload::load('Client') #1 /var/www/html/nt2/teste2.php(10): spl_autoload_call('Client') #2 {main} thrown in /var/www/html/nt2/PEAR2/Autoload.php on line 305

There ara solution for this please?
tks a lot.
 
User avatar
honeyfairy
newbie
Posts: 35
Joined: Sat Nov 21, 2020 1:25 am
Contact:

Re: import and fetch via API

Thu Feb 09, 2023 2:08 am

I
man can you helpme?
in this case:

$client = new Client('xx.xx.xx.xx', 'admin', '1234');
$client(new Request('/tool fetch url="xx.xx.xx.xx/nt2/x.rsc"'));
$client(new Request('/import file-name=x.rsc'));

I have:
Fatal error: Uncaught Exception: Class Client could not be loaded from Client.php, file does not exist (registered paths="/var/www/html/nt2") [PEAR2_Autoload-@PACKAGE_VERSION@] in /var/www/html/nt2/PEAR2/Autoload.php:305 Stack trace: #0 [internal function]: PEAR2\Autoload::load('Client') #1 /var/www/html/nt2/teste2.php(10): spl_autoload_call('Client') #2 {main} thrown in /var/www/html/nt2/PEAR2/Autoload.php on line 305

There ara solution for this please?
tks a lot.
I am having the exact same issue, any ideas?

Who is online

Users browsing this forum: No registered users and 19 guests