Dynamic step count to my website

Recently I saw a website (portfolio) where the designer had yesterday’s health statistics on a page – which I thought was pretty interesting. So I set out to discover a way to do this myself. I have it fully implemented on my website, but it’s not visible in the user interface. I don’t think it’s important for my website, but I love that I worked it out. Here are the steps.

Exporting the health data I want from Apple Health on my phone

I found an app called Health Auto Export – which you can configure with automator to perform the export and run a shortcut. So let’s say at 11:00 PM every day you want specific health data to be uploaded to your website, this exports the data (as XML or JSON). I chose JSON. The next step is a Shortcut.

Running a Shortcut to POST the JSON to my website

Once the export file is created, I needed a Shortcut to be called by Automator that reads the contents of my local JSON export and POST that to my website. I need to POST the JSON to a script on my website so that the file can be stored there, and when a page loads, it can read the contents on the server and use it.

Above is a screenshot of the result of the Shortcut running – it posted a key of “data” as a form to my PHP script on my website. It reads that data and then overwrites a JSON file on the server – making it available for page loads. Here is console output showing the JSON for step count being read and printed out.

Below is the PHP used to receive the POST. Its parsed depending on how your original JSON is exported.

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['data'])) {
        $json_data = json_decode($_POST['data'], true);
		// The exported JSON will dictate this.
        if (json_last_error() === JSON_ERROR_NONE) {
            if (isset($json_data['data']['metrics'][0]['data'][0]['qty'])) {
                $qty = $json_data['data']['metrics'][0]['data'][0]['qty'];
                $output_data = json_encode(['qty' => $qty], JSON_PRETTY_PRINT);\
                if ($output_data !== false) {
                    $file_path = 'mySteps.json';
                    if (file_put_contents($file_path, $output_data)) {
                        echo "Data written successfully.";
                    } else {
                        echo "Failed to write data to file.";
                    }
                } else {
                    echo "Failed to encode data to JSON.";
                }
            } else {
                echo "Key 'qty' not found.";
            }
        } else {
            echo "Invalid JSON data.";
        }
    } else {
        echo "No data received.";
    }
} else {
    echo "Please submit the form using POST method.";
}
?>

So, in short, it’s working and automated. Every day I can have this Automation kick off and generate the data, upload the data, and have it ready for page visits every day as long as the Automation runs. Pretty slick. If my website was more health-oriented, this might be interesting to visitors. For me, it was simply a technical challenge using Shortcuts and Automator to make it work. Who doesn’t like tinkering? Perhaps this could allow me to prototype something like this again in the future.

What did I learn?

For all of the effort put into Automator and Shortcuts – they are both difficult to use – even for someone like myself. Unless you live in the tools or trust that someone else’s compositions can get things done well enough for you, this is where Apple could lean HARD on AI. How?

Ask Shortcuts what you want to do – using natural language.

You: “Every night at 11:00 PM I want you to export my health data from the App XYZ to my health folder on my iCloud Desktop. And then open that file and send the content of it as a POST to my script URL.”

Siri: “Here is what I have so far (displays the flow). Make sure I have the file identified correctly. Also, please fill out the URL so that I can test the script for you.”

You’re allowed a back and forth, but the main aspects are composed for edit without having to know which exact recipe ingredients you need to drag out and use. That’s a HUGE stumbling block – for me, and especially for those who are less technically inclined. Could your parents use Automator or Shortcuts? No way. Apple could change that and really unlock the existing power of the tools that are already out there. Make it even easier to use!

One thought on “Dynamic step count to my website

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.