PHP code example of dwedaz / tuya-api-wrapper

1. Go to this page and download the library: Download dwedaz/tuya-api-wrapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dwedaz / tuya-api-wrapper example snippets





use Dwedaz\TuyaApiWrapper\TuyaApi;

use Dwedaz\TuyaApiWrapper\AuthorizationManagement\GetToken;
use Dwedaz\TuyaApiWrapper\AuthorizationManagement\RefreshToken;
use Dwedaz\TuyaApiWrapper\DeviceControl\ControlDevice;
use Dwedaz\TuyaApiWrapper\DeviceControl\DeviceStatus;

$clientId = 'xxxxxxxxxxxxx';
$secret = 'xxxxxxxxxxxxx';
$deviceId = 'xxxxxxxxx';
$filename = 'token.json';

try{

    $client = new TuyaApi($clientId, $secret);
    if (!file_exists($filename)){
        $accessToken = json_decode(file_get_contents($filename), true);
        $token = $accessToken['result']['access_token'];
        $client->setAccessToken($token);
    }else{
        $accessToken = $client->call(GetToken::class, ['grant_type' => 1]);
        file_put_contents($filename, json_encode($accessToken));
    }
    print_r($client->call(DeviceStatus::class, $deviceId));
}catch (Exception $e){
    echo $e->getMessage();
}