Return value to a variable with php by fetch

Hi all, i’m having some problems to understand how to do what is described in the wiki: https://wiki.mikrotik.com/wiki/Manual:Tools/Fetch, what i need to to is to pass a value from a webserver (php script) to th Routerboard Device. in the wiki there is a good example about the script, the thing i do not understand is what is need to be written in the php file to pass the variable on the routerboard, the wiki use a php example as i see: /tool fetch url=http://10.0.0.1/disable_ether2.php as-value output=user.
Everything about the fetch component is clear but, for example a very simple : echo $example in the php file will not return the value of $example in the routerboard variable called data. Is it possible to have a very simple php example code to pass a variable value using the fetch command ? Also, this is my first post so hi to everyone.

Any help will be appreciated

Thanks to everyone

Americo

Hi,

A simple echo should do the trick.
Did you tried to run the script from the terminal window or did you run as a script with the command /system script run?

If you are running the code from the terminal window use the global variable or use the local variable but be sure to put all the code in the brackets { }.

You can verify the value of the variable with the put command.
:global result [/tool fetch url=http://10.0.0.1/disable_ether2.php as-value output=user];
:put $result

You should see a result like this <data=0;downloaded=0;duration=00:00:01;status=finished;total=0>.

Andrej

I’ve done that, by script, by termina, but nothing happen at all, no usual fetch output, at least by having the php script simply posting a variable. BUT if i try po use the same fetch command:
/tool fetch url=“https://api.ipify.org” as-value output=user, i have the correct output and the response show me the content of the variable data as it should do. So of course there is something i do wrong in the php file simply because i do not know what is need to be passed i also wrote a json response tha works in the web browser, but do not start the classic dowload in the mikrotik comd line. Again, is possible to know the content of the disable_ether2.php file that mikrotik used for example in the wiki ? i really do not know how to do

Thanks a lot

Hi,

I think you maybe have some misconfiguration on the server. I tried to setup a simple server with apache24 and php. With the fetch command I get the output to the data variable. As for the disable_ether2.php I don’t know the contents of the PHP file. But as you can see I can get the output of the variable.

SERVER FreeBSD jail 11.2
Apache24
PHP7.1
RouterOS 6.43.12

index.php content:

<?php $test = "test!"; echo "This is a $test"; ?>



Mikrotik command and output:

[andrej@MikroTik] > :global result [/tool fetch url=http://192.168.105.10/index.php as-value output=user];
[andrej@MikroTik] > :put $result
data=This is a test!;downloaded=0;duration=00:00:01;status=finished;total=0
[andrej@MikroTik] > :if ($result->“data” = “This is a test!”) do={:put “We successfully get the output”};
We successfully get the output

If you access the URL from the browser do you get the output of the variable?

Best regards,
Andrej