I have created a PHP script for accessing the Coolkit v2 API using OAuth 2.0

I had a need to read the temperature from my Sonoff TH20 temperature probe in my duck pond and post it on my weather website. I needed to run it as a simple cron job. Searching on the web, I found no example code written in PHP to accomplish this task and I had no desire to install Node JS and a big application just to read the temperature. So, I set out to write a PHP script to do the job. Hopefully it will serve as an example for how to access this API using PHP.

I have posted the script and instructions on my website at: https://mresoftware.com/Coolkit_API.htm This is new code that was hacked together yesterday so it may not be perfect, but it is so far running fine on my weather station web page. Anyway, I just wanted to make the example available for other old guys like me who still love PHP.

3 Likes

I have made a few changes to the script and cleaned it up some. I also had a math error that was making it refresh tokens every 2 days instead of 28 days. Around line 55 it should read:
if($ATXT - (time() * 1000) <= 1728000000){ // Token is good for 30 days. I refresh it every 28 days

1728000000 instead of 2419200000

If anyone has downloaded it, you might want to get a fresh copy and check back occasionally. I am putting the last update time on the page and in the script comments.