Using the example PHP

The Netatmo API is a set of webservices allowing developers and third parties to access Netatmo device's data.
jtienor
Posts: 11
Joined: 27 Oct 2012, 03:14

Using the example PHP

Post by jtienor » 28 Oct 2012, 03:07

I just received my Netatmo device and got it up and running. I've been trying to access it via a web script using the PHP examples and have been having no success. Would you be able to provide any assistance with why this script isn't functioning?

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

Re: Using the example PHP

Post by iTuilerie » 28 Oct 2012, 12:52

Yes, same problem for me

It's possible to have a complete sample for php

Tank's

fpotter
Site Admin
Posts: 423
Joined: 27 Dec 2011, 12:40

Re: Using the example PHP

Post by fpotter » 29 Oct 2012, 08:37

Hi,

Can you please give us some more information on the error you get ? We have the PHP example up & running without any problem here...

thanks !

fred

jtienor
Posts: 11
Joined: 27 Oct 2012, 03:14

Re: Using the example PHP

Post by jtienor » 01 Nov 2012, 21:59

That's part of the problem Fred. The examples don't provide any type of debugging or error checking so right now a black page is returned to me. If there is something I could try to determine the issue I would but the continuous post/response of the script removes any type of debugging that I try. I really would appreciate any help you can provide as the API and remote access to this data is the reason I purchased the solution. Thanks again and feel free to contact me directly to determine what can be done next.

trosenblatt
Posts: 233
Joined: 18 Sep 2012, 12:18

Re: Using the example PHP

Post by trosenblatt » 02 Nov 2012, 11:24

Hello Jtienor,

Which php exemple do you use ?
Could you please post your code, I will be pleased to help you debugging the script.
Thomas.
Thomas Rosenblatt, Netatmo Team.

jtienor
Posts: 11
Joined: 27 Oct 2012, 03:14

Re: Using the example PHP

Post by jtienor » 03 Nov 2012, 16:42

I'm using two different ones, they are shown below with my username and password removed:

<?php

echo "0";
require_once("NAApiClient.php");

$config = array();
$config['client_id'] = '508bfdb01877593998000aba';
$config['client_secret'] = 'GxlcoqYwufwWne29Mr5cQPldoKk0O2MiJpv9a7';
$client = new NAApiClient($config);


$username = "";
$pwd = "";
$client->setVariable("username", $username);
$client->setVariable("password", $pwd);
try
{
$tokens = $client->getAccessToken();
$refresh_token = $tokens["refresh_token"];
$access_token = $tokens["access_token"];
}
catch(NAClientException $ex)
{
echo "An error happend while trying to retrieve your tokens\n";
}

// First retrieve user device list

try
{
$deviceList = $client->api("devicelist");
if(isset($deviceList["devices"][0]))
{
$device_id = $deviceList["devices"]["_id"];
// Ok now retrieve last temperature and humidity
$params = array("scale" =>"max,
"type"=>"Temperature,Humidity",
"date_end"=>"last",
"device_id"=>$device_id);
$res = $client->api("getmeasure");
if(isset($res[0]) && isset($res[0]["beg_time"]))
{
$time = $res[0]["beg_time"];
$t = $res[0]["value"][0][0];
$h = $res[0]["value"][0][1];
echo "Temperature is $t Celsius @".date('c', $time)."\n";
echo "Humidity is $h Celsius @".date('c', $time)."\n";
}
}
}
catch(NAClientException $ex)
{
echo "User does not have any devices\n";
}


?>
-----------------------------------------------------------------

<?php

// This is just an example to illustrate the documentation
// Prefer the PHP SDK

$app_id = '508bfdb01877593998000aba';
$app_secret = 'GxlcoqYwufwWne29Mr5cQPldoKk0O2MiJpv9a7';
$username = '';
$password = '';
$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'];

$user = json_decode(file_get_contents($api_url));
echo("It worked. Hello <".$user->body->mail.">\n");

?>

jtienor
Posts: 11
Joined: 27 Oct 2012, 03:14

Re: Using the example PHP

Post by jtienor » 05 Nov 2012, 22:32

Do you have any questions about the code posted?

trosenblatt
Posts: 233
Joined: 18 Sep 2012, 12:18

Re: Using the example PHP

Post by trosenblatt » 06 Nov 2012, 10:07

Hello jtienor,

The code you use has some bugs, i send you a revision of this code that worked perfectly for me.

Thomas.


echo "0";
require_once("NAApiClient.php");

$config = array();
$config['client_id'] = '508bfdb01877593998000aba';
$config['client_secret'] = 'GxlcoqYwufwWne29Mr5cQPldoKk0O2MiJpv9a7';
$client = new NAApiClient($config);


$username = "";
$pwd = "";
$client->setVariable("username", $username);
$client->setVariable("password", $pwd);
try
{
$tokens = $client->getAccessToken();
$refresh_token = $tokens["refresh_token"];
$access_token = $tokens["access_token"];
}
catch(NAClientException $ex)
{
echo "An error happend while trying to retrieve your tokens\n";
}

// First retrieve user device list

try
{
$deviceList = $client->api("devicelist");
if(isset($deviceList["devices"][0]))
{
$device_id = $deviceList["devices"][0]["_id"];
// Ok now retrieve last temperature and humidity
$params = array("scale" =>"max",
"type"=>"Temperature,Humidity",
"date_end"=>"last",
"device_id"=>$device_id);
$res = $client->api("getmeasure", $params);
if(isset($res[0]) && isset($res[0]["beg_time"]))
{
$time = $res[0]["beg_time"];
$t = $res[0]["value"][0][0];
$h = $res[0]["value"][0][1];
echo "Temperature is $t Celsius @".date('c', $time)."\n";
echo "Humidity is $h Celsius @".date('c', $time)."\n";
}
}
}
catch(NAClientException $ex)
{
echo "User does not have any devices\n";
}
Thomas Rosenblatt, Netatmo Team.

jtienor
Posts: 11
Joined: 27 Oct 2012, 03:14

Re: Using the example PHP

Post by jtienor » 06 Nov 2012, 17:41

After repopulating the username and password and running the script, the following error is returned:

An error happend while trying to retrieve your tokens User does not have any devices

Any direction is appreciated as this chain of authentication is quite confusing to follow.

trosenblatt
Posts: 233
Joined: 18 Sep 2012, 12:18

Re: Using the example PHP

Post by trosenblatt » 06 Nov 2012, 17:59

Ok, could you replace with this line in the exception handling in order to have more explanation

catch(NAClientException $ex)
{
echo "An error happend while trying to retrieve your tokens : ".$ex->getMessage()."\n";
}


PS : The Netatmo client requires php libcurl http://curl.haxx.se/libcurl/php/install.html.
Perhaps you don't have this library and that is why your code fail.
Thomas Rosenblatt, Netatmo Team.

Post Reply

Return to “Netatmo API”