$selectedHour ]; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $vars['API_URL'] . 'hop/' . $customerNumber . '/' . $connectionId .'/'); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $post); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $accessToken, 'Accept: application/json']); $hourDetails = curl_exec($curl); curl_close($curl); if (!$hourDetails) { throw new Exception('Failed to get selected hour details'); } $jsonHourDetails = json_decode($hourDetails, true); if ($jsonHourDetails['error']) { throw new Exception($jsonHourDetails['error']['detail'], $jsonHourDetails['error']['code']); } else { $selectedHour = $jsonHourDetails['data']['start']['interval']; } } /** * Get access and refresh tokens for a customer from their login code and cookie them. */ function authorizeWithCode($vars, $code) { $post = [ 'code' => $code, 'client_id' => $vars['CLIENT_ID'], 'client_secret' => $vars['CLIENT_SECRET'], 'grant_type' => 'authorization_code', 'scope' => $vars['SCOPES'], 'redirect_uri' => $vars['REDIRECT_URI'], ]; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $vars['TOKEN_URL']); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $post); $auth = curl_exec($curl); curl_close($curl); if (!$auth) { throw new Exception('Post to authorize failed', 500); } $jsonAuth = json_decode($auth, true); if ($jsonAuth['error']) { throw new Exception($jsonAuth['error']['detail'], $jsonAuth['error']['code']); } else { setcookie('access_token', $jsonAuth['access_token'], time() + $jsonAuth['expires_in'], "/"); setcookie('refresh_token', $jsonAuth['refresh_token'], time() + (86400 * 90), "/"); header('Location: /'); die(); } } /** * Refresh a customer's access token from their refresh token. */ function refreshToken($vars, $refreshToken) { $post = [ 'grant_type' => 'refresh_token', 'refresh_token' => $refreshToken, ]; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $vars['TOKEN_URL']); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $post); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Basic ' . base64_encode($vars['CLIENT_ID'] . ':' . $vars['CLIENT_SECRET']), 'Content-Type: multipart/form-data']); $auth = curl_exec($curl); curl_close($curl); if (!$auth) { setcookie('refresh_token', '', time() - 1000, "/"); throw new Exception('Failed to log back in automatically', 500); } $jsonAuth = json_decode($auth, true); if ($jsonAuth['error']) { setcookie('refresh_token', '', time() - 1000, "/"); throw new Exception('Failed to log back in automatically (' . $auth . ')', 500); } else { setcookie('access_token', $jsonAuth['access_token'], time() + $jsonAuth['expires_in'], "/"); setcookie('refresh_token', $jsonAuth['refresh_token'], time() + (86400 * 90), "/"); header('Location: /'); die(); } } // We've been pinged by Electric Kiwi with a new code. We need to get a token, cookie it and redirect to the homepage. if ($_GET && key_exists('code', $_GET)) { try { authorizeWithCode($vars, $_GET['code']); } catch (Exception $exception) { $message = 'There was a problem signing in. Perhaps try again.'; $isLoggedIn = false; } } // Logout has been requested so destroy access and refresh token cookies if ($_GET && key_exists('logout', $_GET)) { setcookie('access_token', '', time() - 1000, "/"); setcookie('refresh_token', '', time() - 1000, "/"); header('Location: /'); } // Set state of landing page depending on whether customer has tokens if (!isset($accessToken) && !isset($refreshToken)) { // This user doesn't have a saved access or refresh token. Prompt login $isLoggedIn = false; } elseif (isset($accessToken) && isset($refreshToken)) { // They have an access token so attempt to get customer details try { getCustomerDetails($vars, $accessToken, $customerNumber, $connectionId, $customerName); getCurrentHour($vars, $accessToken, $customerNumber, $connectionId, $selectedHour); if ($newHour) { $message = 'Done, changed to ' . $times[$newHour] . '.'; setcookie('hour', '', time() - 1000, "/"); } else { $message = 'Select your hour of power.'; } } catch (Exception $exception) { if ($exception->getCode() === 401) { try { refreshToken($vars, $refreshToken); } catch (Exception $exception) { $message = $exception->getMessage() . '. Please log in again below.'; $isLoggedIn = false; } } } } elseif (isset($refreshToken)) { try { refreshToken($vars, $refreshToken); } catch (Exception $exception) { $message = $exception->getMessage() . '. Please log in again below.'; $isLoggedIn = false; } } // The form was submitted. Update the hour of power if ($isLoggedIn && $_POST) { // Wanting to update hour of power $selectedHour = $_POST['hour']; $isValid = true; if (empty($selectedHour)) { $message = 'Please select an hour for your hour of free power'; $isValid = false; } if ($isValid) { try { setCurrentHour($vars, $accessToken, $customerNumber, $connectionId, $selectedHour); setcookie('hour', $selectedHour, time() + 30000, "/"); header('Location: /'); die(); } catch (Exception $exception) { $message = $exception->getMessage(); } } } ?> Electric Kiwi Hour Changer
Log in now
$value): ?> checked>

Logged in as . Log out

This site is not affiliated with Electric Kiwi. It uses Electric Kiwi's API to update your hour of power. None of your personal information is retained or shared.
Source code