-
Notifications
You must be signed in to change notification settings - Fork 0
/
inverter.php
75 lines (61 loc) · 2.31 KB
/
inverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
$energyApiUsername = 'your@email.com';
$energyApiPassword = 'top_secret_password';
$energyApiPlantId = <your plant id>;
$baseUrl = 'https://www.solarkcloud.com';
$plantId = $energyApiPlantId;
$url = $baseUrl . '/oauth/token';
$headers = [
'Content-Type: application/json;charset=UTF-8', // Set Content-Type header to application/json
'origin: ' . $baseUrl . '/oauth/token',
'refer: ' . $baseUrl . '/oauth/token'
];
$params = [
'client_id' => 'csp-web',
'grant_type' => 'password',
'password' => $energyApiPassword,
'username' => $energyApiUsername,
];
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Set custom headers
$response = curl_exec($ch);
//var_dump($response);
echo "<HR>";
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
$bodyData = json_decode($response, true);
$data = $bodyData["data"];
$access_token = $data['access_token'];
echo $access_token ;
$currentDateTime = new DateTime('now', new DateTimeZone('America/New_York'));
//echo $currentDateTime->format('Y-m-d h:i:s');
$currentDate = $currentDateTime->format('Y-m-d');
$actionUrl = $baseUrl . '/api/v1/plant/energy/' . $plantId . '/day?date=' . $currentDate . "&id=" . $plantId . "&lan=en";
$actionUrl = $baseUrl . '/api/v1/plant/energy/' . $plantId . '/flow?date=' . $currentDate . "&id=" . $plantId . "&lan=en";
$actionUrl = $baseUrl . '/api/v1/plant/energy/' . $plantId . '/flow';
$userParams = [
'date' => $currentDate,
'id' => $energyApiPlantId,
'lan' => 'en'
];
// Make GET request to user endpoint
$queryString = http_build_query($userParams);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $actionUrl . "?" . $queryString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer " . $access_token, // Set the Authorization header with your token
"Accept: application/json",
));
$dataResponse = curl_exec($ch);
var_dump($dataResponse);
curl_close($ch);
$dataBody = json_decode($dataResponse, true);
var_dump($dataBody["data"]);