GET file contents with PHP API call

hello,

i am trying to read file contents through PHP API but i am a little bit confused.
I want to get a string with numbers from a file called test.txt but i can’t so far.

<?php

require('routeros_api.class.php');

$API = new RouterosAPI();

$API->debug = false;

if ($API->connect('192.168.10.1', 'alfa', 'beta')) {   // Change this as necessery

$API->write('/file/get/test.txt/contents');

   $ARRAY = $API->read();

   print_r($ARRAY);

   $API->disconnect();

}

?>

relpy

Array
(
    [!trap] => Array
        (
            [0] => Array
                (
                    [message] => no such command prefix
                )

        )

)

Also, i am wondering if i can get this value from a global variable and not from the file, is this possible?

I don’t know much PHP, but I can at least help with the raw API calls.

In the CLI you can do it using:

:put [/file get [/file find name="test.txt"] contents]

That as the baseline, the API equivalent should be:

/file/print
=.proplist=contents
?name=test.txt

Reading a globel variable also easy, but remember that they are cleared on reboot.
Again the CLI version first:

:put [/system script environment get [/system script environment find name="test"] value]

Which should make the API equivalent:

/system/script/environment/print
=.proplist=value
?name=test

Both works nicely on my end using the C# class.

Here are a few things to read up on:

Thank you very much for your help!

I am sorry to say that but i don’t know nothing about PHP. I can easily do things in CLI but i cannot translate them in the PHP format. Your CLI works like a charm! And your php code as well!

Just to inform, to access a global variable from API, user should have full access privilege and not just ‘read’. With read only access, you get a permission error!

Result

Array
(
    [!trap] => Array
        (
            [0] => Array
                (
                    [category] => 4
                    [message] => not enough permissions (9)
                )

        )

)

Thank you again!

What I wrote was just the raw API calls, not actual PHP. ^^
But good to hear you made it work.

I have only been working with the API for a little over a week, I learned something new too when trying to do it, so thank you too!