Run php script from HA to set time for brother pinter

So I have sent my brother network printer from standby to off via a HA controlled smart plug for more than 76% of the time.

Now that sorry sample of engineering (a MFC 7360N) does not pull its time from a NTP server and on top of that does forget it if its disconnected from the grid after some hours. ingenious, brother at its best. Who the heck would make a network printer, that cant get its time from the grid? Obviously brother.

I thought, I might not be the only one admiring the brother design: and the famous google proved me right. Lucky me, I would have no clue how to fix that by myself. A big thanks to Stefan Korn. I also checked, my brother uses the same URL to set its time. See here:

fix brother time issue after disconnect form grid

So how do I get my HA to perform this task, I currently run HA on a virtualbox VM and will soon move to a docker container. It needs to be triggered when ever the plug gets switched on, either manually from a user or by HA, but with a time delay.

This is the PHP script: (courtesy Stefan Korn)

<?php

brothermfc9840CDW_datumsetzen("192.168.1.10");

function brothermfc9840CDW_datumsetzen($ipadresse, $benutzername = "admin", $passwort = "access")
{
$datum_1980 = mktime(0, 0, 0, 1, 1, 1980);
$datum_heute = mktime();
$datum_brother = $datum_heute - $datum_1980;
$chandle = curl_init();
curl_setopt($chandle, CURLOPT_URL, "http://". $ipadresse . "/fax/general_setup.html?kind=item");
curl_setopt($chandle, CURLOPT_POST, TRUE);
curl_setopt($chandle, CURLOPT_POSTFIELDS, "DateTime=".$datum_brother);
curl_setopt($chandle, CURLOPT_HTTPAUTH, "CURLAUTH_BASIC");
curl_setopt($chandle, CURLOPT_USERPWD, "".$benutzername.":".$passwort."");
curl_setopt($chandle, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($chandle);
curl_close($chandle);
}
?>

Any recommendations how to handle this?

Thanks

So I found a way to call the script form HA

automation:
- alias: "SM_Brother_set_time"
  trigger:
    # Trigger when smart plug goes on
    - platform: state
      entity_id: swicht.smartplug_printer
  action:
    service: shell_command.update_brother_time

shell_command:
  update_brother_time: '/usr/bin/php/home/homeassistant/custom/scripts/update_brother_time.php'

I got that idea from here: how to run script form HA triggered on automation so thanks @joe248 for sharing it.

And then @wgjhstt247 explained here how to get php working in a container

I’ll try to get it to run, and will be back to report results.

so I found that my php8 wont stick through a reboot, and I also found that I have an access right problem, the php script executed first with error 126 and now since I changed it to 777 it exits with error 2 (both from HA log)

So its not jet finished, but Im working on it.