Skip to content

Instantly share code, notes, and snippets.

@oliv-j
Last active February 23, 2023 15:13
Show Gist options
  • Save oliv-j/4b4bdad9469e51385831b1a16861e96a to your computer and use it in GitHub Desktop.
Save oliv-j/4b4bdad9469e51385831b1a16861e96a to your computer and use it in GitHub Desktop.
Medication reminder web service: a rest request for iOS medication tracker shortcut/automation - the shortcut can then be integrated with Pushover for alerts to any device.
//will retrieve the status from file and return it as json to the client
<?php
$filename = "mdstatus.txt";
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
//get the file
// Open the file
$fp = @fopen($filename, 'r');
// extract each line to an array
if ($fp) {
$text = explode("\n", fread($fp, filesize($filename)));
#echo 'file opened. ';
} else {
#echo 'file failed to open. ';
}
$data["m1"] = $text[0];
$data["m2"] = $text[1];
$data["m3"] = $text[2];
//the following line is then redundant
//$data = array("m1" => date("Y-m-d"), "m2" => 0, "m3" => 0);
header("Content-Type: application/json");
echo json_encode($data);
exit();
}
?>
//will receive updated status from iOS shortut and update the txt file to store it
<?php
$filename = "mdstatus.txt";
//get the file
// Open the file
$fp = @fopen($filename, 'r');
// extract each line to an array
if ($fp) {
$text = explode("\n", fread($fp, filesize($filename)));
#echo 'file opened. ';
} else {
#echo 'file failed to open. ';
}
// foreach($text as $element){
// echo $element."<br>";
// }
$data["m1"] = $text[0];
$data["m2"] = $text[1];
$data["m3"] = $text[2];
//parse contents into $data
$medicine = htmlspecialchars($_GET["type"]);
#echo 'type retrieved. ';
//update $data with new info
$data[$medicine] = date("Y-m-d");
$output = $data["m1"]."\n";
$output .= $data["m2"]."\n";
$output .= $data["m3"]."\n";
file_put_contents($filename, "");
file_put_contents($filename, $output);
?>
@oliv-j
Copy link
Author

oliv-j commented Feb 23, 2023

  1. The txt file on the server should be writable by the script and needs to start with the following contents:
0
0
0
  1. Create shortcuts to call
    mdupdate.php?type=var
    Var could be m1, m2 or m3. It will then update one of the lines in the text file above (e.g. for morning, noon or night). The script will replace the relevant line with the current date.

  2. Next create an iOS shortcut that fetches the contents of the file and parses the json into variables m1, m2 & m3. Compare the content of a variable to the current date. If it doesn't match fire an alert to remind you to take your medication.

  3. Create automations to run the shortcut at specific times during the day for m1, m2 or m3.

_Note: I have this running in the cloud using free Google Cloud Platform services. You could also do this on a raspberry pi or locally on a computer but it would need to be powered on and accessibly from the internet.

Oh - yes, I know I haven't configured REST properly because it was easier to test it in a browser by using GET each time instead of POST, but I understand that might make some people feel uncomfortable!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment