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

**URL:** <https://forum.ewelink.cc/t/i-have-created-a-php-script-for-accessing-the-coolkit-v2-api-using-oauth-2-0/46408>\
**Category:** Free discussion\
**Tags:** developer\
**Created:** [August 13, 2024, 4:23pm UTC](https://forum.ewelink.cc/t/i-have-created-a-php-script-for-accessing-the-coolkit-v2-api-using-oauth-2-0/46408 "2024-08-13T16:23:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mre](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mre](https://forum.ewelink.cc/u/mre)\
**Post date:** [August 13, 2024, 4:23pm UTC](https://forum.ewelink.cc/t/i-have-created-a-php-script-for-accessing-the-coolkit-v2-api-using-oauth-2-0/46408/1 "2024-08-13T16:23:08Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![mre](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mre](https://forum.ewelink.cc/u/mre)\
**Post date:** [August 16, 2024, 4:45pm UTC](https://forum.ewelink.cc/t/i-have-created-a-php-script-for-accessing-the-coolkit-v2-api-using-oauth-2-0/46408/2 "2024-08-16T16:45:49Z")

</div>

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.
