예제 #1
0
    }
}
//App client configuration
$scope = NAScopes::SCOPE_READ_STATION;
$config = array("client_id" => $client_id, "client_secret" => $client_secret, "username" => $test_username, "password" => $test_password);
$client = new NAWSApiClient($config);
//Authentication with Netatmo server (OAuth2)
try {
    $tokens = $client->getAccessToken();
} catch (NAClientException $ex) {
    handleError("An error happened while trying to retrieve your tokens: " . $ex->getMessage() . "\n", TRUE);
}
//Retrieve user's Weather Stations Information
try {
    //retrieve all stations belonging to the user, and also his favorite ones
    $data = $client->getData(NULL, TRUE);
    printMessageWithBorder("Weather Stations Basic Information");
} catch (NAClientException $ex) {
    handleError("An error occured while retrieving data: " . $ex->getMessage() . "\n", TRUE);
}
if (empty($data['devices'])) {
    echo 'No devices affiliated to user';
} else {
    $users = array();
    $friends = array();
    $fav = array();
    $device = $data['devices'][0];
    $tz = isset($device['place']['timezone']) ? $device['place']['timezone'] : "GMT";
    //devices are already sorted in the following way: first weather stations owned by user, then "friend" WS, and finally favorites stations. Still let's store them in different arrays according to their type
    foreach ($data['devices'] as $device) {
        //favorites have both "favorite" and "read_only" flag set to true, whereas friends only have read_only
//API client configuration
$config = array("client_id" => $client_id, "client_secret" => $client_secret, "scope" => NAScopes::SCOPE_READ_STATION);
$client = new NAWSApiClient($config);
//if code is provided in get param, it means user has accepted your app and been redirected here
if (isset($_GET["code"])) {
    //get the tokens, you can store $tokens['refresh_token'] in order to quickly retrieve a new access_token next time
    try {
        $tokens = $client->getAccessToken();
    } catch (NAClientException $ex) {
        echo "An error occured while trying to retrieve your tokens \n";
        echo "Reason: " . $ex->getMessage() . "\n";
        die;
    }
    //retrieve user's weather station data
    try {
        $data = $client->getData();
    } catch (NAClientException $ex) {
        echo "An error occured while retrieving data: " . $ex->getMessage() . "\n";
        die;
    }
    if (!isset($data['devices']) || !is_array($data['devices']) || count($data['devices']) < 1) {
        echo "User has no devices \n";
        die;
    }
    //In this example, we will only deal with the first weather station linked to the user account
    $device = $data['devices'][0];
    //last month report
    $reports = getMonthReport($device, $client);
    //Get device timezone
    if (!is_null($device) && !empty($device)) {
        if (isset($device['place']['timezone'])) {