[SH->PHP] E-Thermostaat

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • Toppe
  • Registratie: Januari 2004
  • Laatst online: 14:40

Toppe

Oké ✅

Topicstarter
Goedemiddag,

Ik doe wat fout maar ik zie de fout niet!:(

code:
1
2
3
4
5
#!/bin/bash

TOKEN=`curl -s --request POST 'https://portal.icy.nl/login' --data 'username={username}&password={password}' | awk -F":" '{print $12}' | awk -F'"' '{print $2}'`
TEMP=`curl -s -H "Session-token:$TOKEN" --request GET 'https://portal.icy.nl/data' --data 'username={username}&password={password}' |awk -F":" '{print $11}' | awk -F',' '{print $1}'`
print $TEMP


Ik probeer bovenstaande code om te zetten naar PHP, appeltje eitje zou je denken? niet dus!:(

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

class Temp {

    function getToken($return){
        $url = "https://portal.icy.nl/login";
        $postData = "username={USERNAME}&password={PASSWORD}";
     
     
        $ch = curl_init();  
     
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_HEADER, false); 
        curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    
     
        $output=curl_exec($ch);
     
        curl_close($ch);
        
        $json = json_decode($output);
    
            return $json->{'token'};
    }
    
    function getTemp(){
        $token = $this->getToken();
          $url = "https://portal.icy.nl/data?username={USERNAME}&password={PASSWORD}";
    
          $ch = curl_init();

          /* om te testen */
          $token1 = "Session-token:".$token;
          echo $token.'<br>'.$token1;
          /* om te testen */
          
           curl_setopt($ch,CURLOPT_URL,$url);
           curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
           curl_setopt($ch,CURLOPT_HEADER, "Session-token:".$token); 

          $output = curl_exec($ch);
          curl_close($ch);
    
        print_r($output);

    }
}
?>


Als ik even print_r($output) doe bij de eerste functie krijg ik een hoop informatie (INCLUSIEF TOKEN!!) en het volgende: {"code":200,"message":"OK"}

Alles goed dus, maar als ik nou inlog met exact de zelfde gegevens en m'n token: {"body":"UnauthorizedException: Session expired or invalid.","status":{"code":401,"message":"Unauthorized"}}

Iemand enig idee wat het zou kunnen zijn? Ik kijk er ongetwijfeld overheen.

De bovenste code werkt overigens wel... Dat is het gekke...

Alvast bedankt!

Donstil: Je moet kopen wat je wilt hebben. Niet wat je nodig hebt!


Acties:
  • 0 Henk 'm!

  • THPSrulez
  • Registratie: December 2001
  • Laatst online: 06-10 10:56

THPSrulez

Kijk uit malloot een kokosnoot

PHP:
1
curl_setopt($ch,CURLOPT_HEADER, "Session-token:".$token);


Je gebruikt hier de verkeerde optie om Session-token in je header te plaatsen, zie: http://php.net/manual/en/function.curl-setopt.php

De CURLOPT_HEADER optie verwacht een boolean waarde:
CURLOPT_HEADER TRUE to include the header in the output.
Gebruik in plaats daarvan CURLOPT_HTTPHEADER
CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

Acties:
  • 0 Henk 'm!

  • Toppe
  • Registratie: Januari 2004
  • Laatst online: 14:40

Toppe

Oké ✅

Topicstarter
Lol, zal dat t zijn? Ik ga thuis meteen testen!! Thanks!!

Edit: momenteel zit ik op z'n 70 uur programmeren per week, dan zie je dingen niet meer helder😉

[ Voor 48% gewijzigd door Toppe op 08-04-2015 16:48 ]

Donstil: Je moet kopen wat je wilt hebben. Niet wat je nodig hebt!


Acties:
  • 0 Henk 'm!

  • Toppe
  • Registratie: Januari 2004
  • Laatst online: 14:40

Toppe

Oké ✅

Topicstarter
Werkt, thanks!

Donstil: Je moet kopen wat je wilt hebben. Niet wat je nodig hebt!


Acties:
  • 0 Henk 'm!

  • Toppe
  • Registratie: Januari 2004
  • Laatst online: 14:40

Toppe

Oké ✅

Topicstarter
Voor de geïnteresseerde:

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
class Thermostat {

    function __construct($username, $password){
        $this->username = $username;
        $this->password = $password;
    }

    function getData($return='token'){
        $url = "https://portal.icy.nl/login";
        $postData = "username=".$this->username."&password=".$this->password;
     
     
        $ch = curl_init();  
     
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_HEADER, false); 
        curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    
     
        $output=curl_exec($ch);
     
        curl_close($ch);
        
        $json = json_decode($output);
        return $json->{''.$return.''};
    }
    
    function getTemp($temp="temperature2"){
        $token = $this->getData();
          $url = "https://portal.icy.nl/data?username=".$this->username."&password=".$this->password;
    
          $ch = curl_init();
          
           curl_setopt($ch,CURLOPT_URL,$url);
           curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
           curl_setopt($ch,CURLOPT_HTTPHEADER, array("Session-token:".$token)); 

          $output = curl_exec($ch);
          curl_close($ch);
          
          $json = json_decode($output);

          return $json->{''.$temp.''};

    }
    
    function setTemp($temp){
        $token = $this->getData();
        $uid = $this->getData('serialthermostat1');
        $url = "https://portal.icy.nl/data";
        $postData = "uid=".$uid."&temperature1=".$temp;
     
     
        $ch = curl_init();  
     
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_HEADER, false); 
        curl_setopt($ch,CURLOPT_HTTPHEADER, array("Session-token:".$token)); 
        curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    
     
        $output=curl_exec($ch);
     
        curl_close($ch);
        
        $json = json_decode($output);
            if($json->{'status'}->{'code'} == "200"){
                return true;
            }
    }
}
?>


Oproepen met:
PHP:
1
2
3
4
5
6
7
8
9
10
<?php
//Start een nieuwe class
$Thermostat = new Thermostat('gebruikersnaam', 'wachtwoord');

//Wat is de huidige temperatuur? Let op! Links = temperature1, Rechts = temperature2. Standaard staat hij op 1
$Thermostat->getTemp();

//Pas de temperatuur aan
$Thermostat->setTemp('temperatuur');
?>

Donstil: Je moet kopen wat je wilt hebben. Niet wat je nodig hebt!