[graphicport]

Kasa API using PHP

What is KASA?

KASA is a product by the company TP-Link that allows you to control devices remotely. These are often called smart devices, as they connect to WIFI and have many more features than your standard switch or outlet. KASA devices include light switches, electrical outlets, smart plugin devices, etc.

You can control the KASA devices using the TP-Link KASA application on your phone, but if you want to automate devices your own way, without using something like IFTTT, you can use the TP-Link Web API. Here I am providing a sample way to do this using PHP.

By using PHP, you can create web portals to control your devices. You could turn on a light when your ESP8266 sensor detects something, just as an example.

What you need to start

Please note, I do not make any commission from you buying anything linked here.

Getting Started

This is actually a very easy project. All you need to do is generate a UUIDv4, it doesn't matter what it is as long as it is version 4. [Generate Here]. Just copy the UUID, and paste it into the code below.

Also, you need to create a PHP file on your PHP server. Call it whatever you like. I used kasa.php for the device page and garageLight.php for controlling my garage light device. When the garage door opens, I like having my light turn on automatically. It also turns off when the door closes.

Last, this won't work if you don't have a KASA device. I assume since you are interested in controlling KASA using PHP, you probably have at least one device so far.

The code to find your devices

The code below only needs you to fill in the $password, $email, $UUID. The purpose of the code is to find all the registered devices on your account and give you the device ID. With the Device ID, we can use the second code section to actually control the device.

$ch = curl_init();

$password = "YOUR_KASA_PASSWORD";
$email = "YOUR_KASA_EMAIL";
$UUID = "YOUR_GENERATED_UUIDv4";

curl_setopt($ch, CURLOPT_URL, 'https://wap.tplinkcloud.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n\"method\": \"login\",\n\"params\": {\n\"appType\": \"Kasa_Android\",\n\"cloudUserName\": $email,\n\"cloudPassword\": $password,\n\"terminalUUID\": $UUID\n}\n}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

$values = json_decode($result, true);
$token = $values["result"]["token"];

$ch = curl_init();

$url = "https://wap.tplinkcloud.com?token={$token}";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"method\":\"getDeviceList\"}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

$devices = json_decode($result, true);

//print_r($devices["result"]["deviceList"]);

foreach ($devices["result"]["deviceList"] as $device)
{
    print_r ($device["alias"]);
    print ("</br>");
    
    print_r ($device["deviceType"]);
    print ("</br>");
    
    print_r ($device["deviceId"]);
    print("</br></br>");    
}

/*
##### Possible Data you can retreive:
[deviceType],[role],[fwVer],[appServerUrl],[deviceRegion],[deviceId],[deviceName],[deviceHwVer],[alias],
[deviceMac],[oemId],[deviceModel],[hwId],[fwId],[isSameRegion],[status]
*/

curl_close ($ch);

The code to control your devices

Here is the second code section. By taking the Device ID for the device you wish to control from above, we can now turn things on and off. We have a few new variables here, $deviceId and $mode. You grabbed the $deviceId using the first code block, so just copy the ID of the device you wish to control. $mode is used to toggle the device on or off.

$ch = curl_init();

$password = "YOUR_KASA_PASSWORD";
$email = "YOUR_KASA_EMAIL";
$UUID = "YOUR_GENERATED_UUIDv4";
$deviceId= "YOUR_DEVICE_ID";
$mode = 1; // 1 = on; 0 = off

curl_setopt($ch, CURLOPT_URL, 'https://wap.tplinkcloud.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n\"method\": \"login\",\n\"params\": {\n\"appType\": \"Kasa_Android\",\n\"cloudUserName\": $email,\n\"cloudPassword\": $password,\n\"terminalUUID\": $UUID\n}\n}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

$values = json_decode($result, true);
$token = $values["result"]["token"];

$url = "https://wap.tplinkcloud.com/?token={$token}";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"method\" : \"passthrough\", \"params\" : { \"deviceId\" : $deviceId , \"requestData\" : '{ \"system\" : { \"set_relay_state\" : { \"state\" : $mode }}}'}}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

print_r($result);