Help in writing a PHP script

The Netatmo API is a set of webservices allowing developers and third parties to access Netatmo device's data.
Post Reply
paqueuc
Posts: 5
Joined: 11 Apr 2014, 22:32

Help in writing a PHP script

Post by paqueuc » 08 Jun 2014, 19:30

Hello,

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

iTuilerie
Posts: 32
Joined: 12 Oct 2012, 01:25
Location: Troyes, France
Contact:

Re: Help in writing a PHP script

Post by iTuilerie » 28 Jun 2014, 09:23

Salut

Sur cette page tu pourra récupérer le script écrit par Cedric qui fait exactement ce que tu recherche

http://maison-et-domotique.com/books/sc ... domotique/

Et tout en bas dans les commentaire tu trouvera ma version légèrement modifié qui ajoute le pluviomètre

http://www.ituilerie.com/wp-content/upl ... o.php_.zip

Post Reply

Return to “Netatmo API”