Sorry, I need a great help to write a small PHP script as I'm really a noob.
The idea is just to retrieve the Outside temperature from the Netatmo API to write it in an XML file every 10 mn.
I made something like it but it only works one out of 10 tries (and I don't have the debug view with this script).
Can someone help me and correct it, please ?
Code: Select all
<?php
$app_id = 'XXXXXXXXXXXXXXXX';
$app_secret = 'YYYYYYYYYYYYYYYYYYYYYY';
$username = 'ZZZZZZZZZZZZZZZZZZZZZZZ';
$password = 'NNNNNNNNNNNNNNNNN';
$token_url = "https://api.netatmo.net/oauth2/token";
$postdata = http_build_query(
array(
'grant_type' => "password",
'client_id' => $app_id,
'client_secret' => $app_secret,
'username' => $username,
'password' => $password,
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$response = file_get_contents($token_url, false, $context);
$params = null;
$params = json_decode($response, true);
$api_url = "https://api.netatmo.net/api/getuser?access_token=" . $params['access_token'];
$requete = @file_get_contents($api_url);
$url_devices = "https://api.netatmo.net/api/devicelist?access_token=" . $params['access_token'];
$resulat_device = @file_get_contents($url_devices);
$json_devices = json_decode($resulat_device,true);
$module_externe = $json_devices["body"]["modules"][0]["_id"];
$url_mesures_externes = "https://api.netatmo.net/api/getmeasure?access_token=" . $params['access_token'] . "&module_id=" . $module_externe . "&scale=max&type=Temperature&date_end=last";
$mesures_externes = @file_get_contents($url_mesures_externes);
$json_mesures_externes = json_decode($mesures_externes, true);
$temperature_externe = $json_mesures_externes["body"][0]["value"][0][0];
echo '<?xml version="1.0" encoding="utf8" ?>';
echo "<netatmo>";
echo "<temperature_exterieure>" . $temperature_externe . "</temperature_exterieure>";
echo "</netatmo>";
?>
Thanks