SID请求没有回显任何内容-PHP

时间:2019-05-28 18:39:44

标签: php simplexml mbstring

我使用FritzBox路由器(我不知道它是否在德国以外很常见-如果不是,这是指向我的特定路由器的链接:https://avm.de/produkte/fritzbox/fritzbox-7590/),该路由器具有集成的SmartHome功能,因为它自己的阵容使用DECT。

要集成和控制这些智能家居设备(尤其是智能插头),可以使用API​​(AHA-API)。通过互联网上的一篇文章(来源:https://www.heise.de/select/ct/2016/7/1459414791794586),我找到了一个脚本,该脚本可以通过将其包含在其他脚本中来打开和关闭该脚本,这要感谢先前的回复。

在文章中说我应该安装php5和两个扩展名mbstring和SimpleXML。下面的脚本应从路由器中查找SID,并将其用于其他用途。但是,每次我运行脚本并想要回显SID时,我什么都没有。只有一个空行:

pi@raspberrypi:~/AVM $ sudo php SIDauslesen.php

pi@raspberrypi:~/AVM $ 

如果我将此脚本包含在其他php脚本中,则会出现错误。有人知道为什么它不回显SID以及如何解决它吗?

P.S。这是我的代码:

<?php
//Login
$username="username"; //username
$password="password"; //password
$loginurl="http://fritz.box/login_sid.lua"; //Host, change if needed

//Login-Function
function get_sid ($loginurl,$username,$password) {
  //Send initial Request to Fritzbox
  $http_response = @file_get_contents($loginurl);
  //Parse answer XML
  $xml = @simplexml_load_string($http_response);
  //check if there is an XMl object with Challange-tag
  if (!$xml || !$xml->Challenge ) {
    die ("Error: unexpected answer or communication error!\n");
  }
  //extract Challange and SID Tags of XML
  $challenge=(string)$xml->Challenge;
  $sid=(string)$xml->SID;
  if (preg_match("/^[0]+$/",$sid) && $challenge ) {
    $sid="";
    //create Password string
    $pass=$challenge."-".$password;
    //UTF-16LE encoding of Password was succesful
    $pass=mb_convert_encoding($pass, "UTF-16LE");
    //md5hash
    $md5 = md5($pass);
    //create Response String
    $challenge_response = $challenge."-".$md5;
    //send Response to Fritzbox
    $url=$loginurl."?username=".$username."&response=".$challenge_response;
    $http_response = file_get_contents($url);
    //parse Antwort XML
    $xml = simplexml_load_string($http_response);
    $sid=(string)$xml->SID;
    if ((strlen($sid)>0) && !preg_match("/^[0]+$/",$sid)) {
      //is not null, bingo!
      return $sid;
    }
  }else {
    //use exisiting SID if $sid is a hex string
    if ((strlen($sid)>0) && (preg_match("/^[0-9a-f]+$/",$sid))) return $sid;
  }
return null;
}
?>

0 个答案:

没有答案