Guest User

dS JSON

a guest
Aug 25th, 2018
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. //http://192.168.178.59/ds2.php?/json/zone/callScene&name=Schlafzimmer&groupID=1&sceneNumber=18
  4. //http://192.168.178.59/ds2.php?/json/event/raise&name=highlevelevent&parameter=id%3D1535231363
  5.  
  6. $Server = "192.168.178.28";
  7. $tcpPort = "8080";
  8. $AppToken = "41d4e0a9...cd09688c";
  9. $Call = "";
  10. $i = 0;
  11. $arrContextOptions=array(
  12.     "ssl"=>array(
  13.         "verify_peer"=>false,
  14.         "verify_peer_name"=>false,
  15.     ),
  16. );
  17. $filename = 'sessionToken.txt';
  18. $debug = true;
  19.  
  20. foreach($_GET as $key => $value){
  21.     if ($i == 0)
  22.         $Call = $Call.$key;
  23.     elseif ($i == 1)
  24.         $Call = $Call."?".$key."=".$value;
  25.     else
  26.         $Call = $Call."&".$key."=".$value;
  27.     $i++;
  28. }
  29.  
  30. if ($debug) echo "\$Call: ". $Call."<br>";
  31.  
  32. if (file_exists($filename)) {
  33.     if ((mktime() - filemtime($filename)) > 175)   //sessionToken ist 180 Sekunden gültig
  34.     {
  35.         $sessionToken = fdSLogin();       // get a new digitalSTROM Sessiontoken
  36.         if ($debug) {echo "New Sessiontoken: ".$sessionToken."<br>";}
  37.     } else {
  38.         $fp = fopen($filename , 'r');
  39.         $sessionToken = fread($fp, filesize($filename));
  40.         if ($debug) {echo "Reused Sessiontoken: ".$sessionToken."<br>";}
  41.         fclose($fp);
  42.     }
  43. } else {
  44.         $sessionToken = fdSLogin();       // get the first digitalSTROM Sessiontoken
  45.         if ($debug) {echo "First Sessiontoken: ".$sessionToken."<br>";}
  46. }
  47.  
  48. $fp = fopen($filename , 'w');
  49. fwrite($fp, $sessionToken);
  50. fclose($fp);
  51.  
  52. echo fdSGetJson($Call, $sessionToken);
  53.  
  54. /************************************************* Funktionen definieren  **********************************/
  55.  
  56. /* digitalSTROM Login Prozedure Function */
  57. function fdSLogin(){
  58.     global $Server;
  59.     global $tcpPort;
  60.     global $AppToken;
  61.     global $arrContextOptions;
  62.  
  63.     if($sessionToken = file_get_contents("https://" . $Server . ":" . $tcpPort . "/json/system/loginApplication?loginToken=" . $AppToken, false, stream_context_create($arrContextOptions))){
  64.         $sessionToken = json_decode($sessionToken);
  65.         return $sessionToken->result->token;
  66.     }
  67.     else{
  68.         return "<font color='red'><b>ERROR:</b> unable to get loginToken from digitalSTROM Server</font><br>";
  69.     }
  70. }
  71.  
  72. /* digitalSTROM get JSON content Function */
  73. function fdSGetJson($call, $sessionToken){
  74.     global $Server;
  75.     global $tcpPort;
  76.     global $arrContextOptions;
  77.     $sHelp;
  78.     global $debug;
  79.  
  80.     $sHelp = "https://" . $Server . ":" . $tcpPort . $call . "&token=" . $sessionToken;
  81.     if ($debug) echo "Call: ".$sHelp."<br>";
  82.  
  83.     if($jsonOutput = file_get_contents($sHelp , false, stream_context_create($arrContextOptions))){
  84.         return $jsonOutput; // plain output
  85.     }
  86.     else{
  87.         return "<font color='red'><b>ERROR:</b> unable to get JSON Content from digitalSTROM Server</font><br>";
  88.     }
  89. }
  90.  
  91. ?>
RAW Paste Data