Wink API v2 Hello World in PHP

600 Views Asked by At

Wink API is currently on version 2.

My Question: How can you do a simple "Hello World" with the Wink API V2 via PHP?

Notes:

Related Links:

1

There are 1 best solutions below

0
On BEST ANSWER

Information regarding this is extremely limited, so I'll answer my own question hoping to help others. (It took a long time since there wasn't any good info out there.) This example has a user interface (Login required by Wink). I'm hoping someone can post a non-user-interface version (for background scripting, etc).

This will give you raw json output, for you to do with as you wish. This single php page will initially load, take you to Wink's login (you need an account with your devices if this wasn't obvious), after logging it, it will take you back to this same page with a code, call for a token, then use that token to get the device resources.

Create: //[YourServer]/wink_helloworld.php on your http/php server.

wink_helloworld.php:

//Make sure to add this exact URL to your Wink Developer Portal! (https://developer.wink.com/clients)
$redirect_uri = "http://[YourServer]/wink_helloworld.php";
// This is from Wink Developer Portal
$client_id = "abcdefg";
$wink_oauth_url = "https://api.wink.com/oauth2/token";
$client_secret = "hijklmnop";
$devices_url = "https://api.wink.com/users/me/wink_devices";
//need to create a state variable, like a session id. should actually be random tho!!
$randomstring="xyzABC123";
$state = base64_encode($randomstring);
/*_____________________________________________________________________________________________________________________________________ */

echo "<h2>Wink Hello World - Show Devices</h2>";

//If we don't have a code, then send user to login page
if($_GET['code'] == null | $_GET['code'] == ""){
    echo "<a href='https://api.wink.com/oauth2/authorize?response_type=code&client_id=".$client_id."&redirect_uri=$redirect_uri&state=".$state."'>Login</a>";
    return;
}
$code = $_GET['code'];
//if we dont have a token, lets get one
if($access_token == null | $access_token == ""){
    $access_token = getAccessToken();
}
// lets get some data from our devices!
getResource($access_token);
/*_____________________________________________________________________________________________________________________________________ */
// Get token
function getAccessToken() {
    global $wink_oauth_url, $code, $client_secret;
    echo "<b>getAccessToken()</b> Using Code: $code<br>";
    $curl = curl_init();    
    curl_setopt($curl, CURLOPT_URL, $wink_oauth_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_HEADER, FALSE);
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "{
        \"client_secret\": \"$client_secret\",
        \"grant_type\": \"authorization_code\",
        \"code\": \"$code\"
    }");
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));    
    $response = curl_exec($curl);
    //var_dump($response);
    formatResults($response);       //debug output
    curl_close($curl);
    return json_decode($response)->access_token;
}
/*_____________________________________________________________________________________________________________________________________ */
// Get Resource(s) with our code & token
function getResource($access_token) {
    global $devices_url;
    echo "<b>getResource()</b> Using Token: $access_token<p>";
    $header = array("Authorization: Bearer {$access_token}");
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $devices_url,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true
    ));
    $response = curl_exec($curl);
    curl_close($curl);  
    formatResults($response);       //debug output
}

/*_____________________________________________________________________________________________________________________________________ */
//debug formatted output functions
function formatResults($json){
    echo "<pre>";
    echo json_encode(json_decode($json), JSON_PRETTY_PRINT);
    echo "</pre>";
}

?>