Community discussions

MikroTik App
 
User avatar
metikus
just joined
Topic Author
Posts: 2
Joined: Wed Oct 19, 2022 8:16 pm
Location: ur heart♥

Sending String to mysql from fetch url

Fri Oct 21, 2022 1:04 pm

I was found code in here https://labkom.co.id/mikrotik/monitorin ... t-telegram and viewtopic.php?t=151953
I wanna modified that code for sending string data to mysql on fetch url.

On mikrotik
# BEGIN SETUP Edit Here
:local myserver ([/system identity get name])
:local scheduleName "LogFilter"
:local startBuf [:toarray [/log find message~" failure" || message~"loop" || message~"down" || message~"fcs" || message~"excessive"]]
:local removeThese [:toarray ""]
# END SETUP

# warn if schedule does not exist and create it
:if ([:len [/system scheduler find name="$scheduleName"]] = 0) do={
    /log warning "[LogFilter] Alert : Schedule does not exist. Creating schedule …."
    /system scheduler add name=$scheduleName interval=60s start-date=Jul/05/2019 start-time=startup on-event=LogFilter
    /log warning "[LogFilter] Alert : Schedule created ."
}

# get last time
:local lastTime [/system scheduler get [find name="$scheduleName"] comment]
# for checking time of each log entry
:local currentTime
:local message
:local output

:local keepOutput false
# if lastTime is empty, set keepOutput to true
:if ([:len $lastTime] = 0) do={
    :set keepOutput true
}

:local counter 0
# loop through all log entries that have been found
    :foreach i in=$startBuf do={

    # loop through all removeThese array items
    :local keepLog true
    :foreach j in=$removeThese do={
        # if this log entry contains any of them, it will be ignored
        :if ([/log get $i message] ~ "$j") do={
            :set keepLog false
        }
    }
    :if ($keepLog = true) do={

        :set message [/log get $i message]

        # LOG DATE
        # depending on log date/time, the format may be different. 3 known formats
        # format of jan/01/2002 00:00:00 which shows up at unknown date/time. Using as default
        :set currentTime [ /log get $i time ]
        # format of 00:00:00 which shows up on current day’s logs
        :if ([:len $currentTime] = 8 ) do={
            :set currentTime ([:pick [/system clock get date] 0 11]." ".$currentTime)
        } else={
            # format of jan/01 00:00:00 which shows up on previous day’s logs
            :if ([:len $currentTime] = 15 ) do={
                :set currentTime ([:pick $currentTime 0 6]."/".[:pick [/system clock get date] 7 11]." ".[:pick $currentTime 7 15])
            }
        }

        # if keepOutput is true, add this log entry to output
        :if ($keepOutput = true) do={
            :set output ($output.$message)
        }
        :if ($currentTime = $lastTime) do={
            :set keepOutput true
            :set output ""
        }
    }
    :if ($counter = ([:len $startBuf])-1) do={
        :if ($keepOutput = false) do={
            :if ([:len $message] > 0) do={
                :set output ($output.$message)
            }       
        }
    }
    :set counter ($counter + 1)
}
if ([:len $output] > 0) do={
/system scheduler set [find name="$scheduleName"] comment=$currentTime
/tool fetch url="https://api.telegram.org/boxxx:xxx/sendMessage\?chat_id=xxx&text=$output" keep-result=no;
}


and i changed fetch url to
if ([:len $output] > 0) do={
/system scheduler set [find name="$scheduleName"] comment=$currentTime
/tool fetch mode=https url="https://thewebsite.com/register.php?text=$output"
}

in register.php
$text = $_GET['text'];

// Create connection
$conn = new mysqli('servername', 'username', 'password', 'dbname');
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO teks (val) VALUES ('$text')";

if ($conn->query($sql) === TRUE) {
    echo "Records inserted successfully.";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

It doesn't work. But if I submit manually from browser, that's work. Anybody help?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Sending String to mysql from fetch url

Fri Oct 21, 2022 2:47 pm

Find the differencies:

Original:
/tool fetch url="http s://api.telegram.org/boxxx:xxx/sendMessage\?chat_id=xxx&text=$output" keep-result=no;

Modified:
/tool fetch mode=https url="http s://thewebsite.com/register.php?text=$output"

(a space is added on https for not auto-change the text to URL on post)

I wonder why you didn't just change the URL and leave the rest...
 
User avatar
metikus
just joined
Topic Author
Posts: 2
Joined: Wed Oct 19, 2022 8:16 pm
Location: ur heart♥

Re: Sending String to mysql from fetch url

Fri Oct 21, 2022 3:42 pm

I've tried to use mode=https but still doesn't work. I use ros 6.46.8 version
Last edited by BartoszP on Sat Oct 29, 2022 11:37 pm, edited 1 time in total.
Reason: unneeded quotation
 
andy1989
just joined
Posts: 5
Joined: Sat Jun 05, 2021 9:20 am

Re: Sending String to mysql from fetch url

Sat Oct 29, 2022 11:00 pm

Find the differencies:

Original:
/tool fetch url="http s://api.telegram.org/boxxx:xxx/sendMessage\?chat_id=xxx&text=$output" keep-result=no;

Modified:
/tool fetch mode=https url="http s://thewebsite.com/register.php?text=$output"

(a space is added on https for not auto-change the text to URL on post)

I wonder why you didn't just change the URL and leave the rest...
Isn't it didn't work because of the back slash before the question mark ?

Who is online

Users browsing this forum: No registered users and 19 guests