Fritz!Box API für PHP (inkl. Login-Sessions)

In der Richtung wird Dir die API nicht helfen, weil sie keine von der Box initiierten Signale empfangen kann. In dem Fall würde ich einfach einen SIP-Client installieren und als Nebenstelle einrichten und als Klingelton die gewünschte WAV-Datei einstellen. Klingeln bei Anrufen ist genau das, wofür er gedacht ist.
 
Hallo ich komm leider nicht weiter.
Ich möchte gerne einfach nur mein telefonbuch exportieren.

Wenn ich das Script aufrufe, bekomme ich folgende Fehlermeldungen:

PHP Warning: simplexml_load_string(): Entity: line 1: parser error : AttValue: " or ' expected in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): <META HTTP-EQUIV=Refresh CONTENT="0; URL=/login.lua?requestedpage=../html/login_ in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): ^ in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : attributes construct error in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): <META HTTP-EQUIV=Refresh CONTENT="0; URL=/login.lua?requestedpage=../html/login_ in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): ^ in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Couldn't find end of Start Tag META line 1 in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): <META HTTP-EQUIV=Refresh CONTENT="0; URL=/login.lua?requestedpage=../html/login_ in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): ^ in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Extra content at the end of the document in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): <META HTTP-EQUIV=Refresh CONTENT="0; URL=/login.lua?requestedpage=../html/login_ in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Warning: simplexml_load_string(): ^ in /home/markus/Fritzbox/fritzbox_api.class.php on line 397
PHP Notice: Trying to get property of non-object in /home/markus/Fritzbox/fritzbox_api.class.php on line 400
PHP Notice: Trying to get property of non-object in /home/markus/Fritzbox/fritzbox_api.class.php on line 408
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : AttValue: " or ' expected in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): <META HTTP-EQUIV=Refresh CONTENT="0; URL=/login.lua?requestedpage=../html/login_ in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): ^ in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : attributes construct error in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): <META HTTP-EQUIV=Refresh CONTENT="0; URL=/login.lua?requestedpage=../html/login_ in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): ^ in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Couldn't find end of Start Tag META line 1 in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): <META HTTP-EQUIV=Refresh CONTENT="0; URL=/login.lua?requestedpage=../html/login_ in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): ^ in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Extra content at the end of the document in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): <META HTTP-EQUIV=Refresh CONTENT="0; URL=/login.lua?requestedpage=../html/login_ in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Warning: simplexml_load_string(): ^ in /home/markus/Fritzbox/fritzbox_api.class.php on line 420
PHP Notice: Trying to get property of non-object in /home/markus/Fritzbox/fritzbox_api.class.php on line 421
PHP Notice: Trying to get property of non-object in /home/markus/Fritzbox/fritzbox_api.class.php on line 423
Phonebook exported to /home/markus/Fritzbox/exported_pb.xml


Vielen Dank!
 
Das sind nur Warnings, die auf einen zu niedrigen Error-Level bei den Einstellungen des PHP-Parsers hindeuten. Angeblich wird aber die Datei unter /home/markus/Fritzbox/exported_pb.xml erstellt. Befindet sie sich dort auch?
 
Das sieht mir eher nach einem Login-Problem aus. Mehr kann ich dazu jetzt auch nicht sagen.
 
Datei wird schon erstellt, aber ist leider leer
 
Moin

Die Fehlermeldung: attributes construct error
...bezieht sich wohl auf: <META HTTP-EQUIV=Refresh CONTENT="0;
Und da fehlen wohl Gänsefüßchen (HTTP-EQUIV = "Refresh").
Oder der XML Parser kann mit dem HTML Attribut CONTENT="0;URL= nichts anfangen.
Ich würde auch alles kleinschreiben, oder groß, aber nicht gemixt.
Wenn sich diese Seite sofort selber laden soll, dann:
HTML:
<meta http-equiv="refresh" content="0"/>
 
Zuletzt bearbeitet:
Hallo zusammen!
Wir haben nun vor zwei Tagen eine nagelneue Fritzbox 7490 installiert. Darauf haben wir 4 verschiedene ABs konfiguriert.
Nun suche ich eine Möglichkeit, dass man ohne Login auf die Fritzbox-eigene Oberfläche diese 4 Anrufbeantworter je nach Bedarf ein- und ausschalten kann.
Ideal wäre es, wenn in einem kleinen Fenster der Zustand der Anrufbeantworter angezeigt würde.
Ich bin bei der Suche nach einer Lösung auf diesen Thread gestoßen. Dank schon mal an alle, die sich hier beteiligen.

!! Leider ist php etc. für mich bisher ein Fremdwort !!

Dennoch habe ich Notepad++ und PHP 5.4 (5.4.36) und auch die fritzbox_api_php_v0.5.0b7.zip heruntergeladen.
PHP und zip-Datei in ein UVZ kopiert und die php.ini erstellt/modifiziert, so wie in der readme angegeben.

Wenn ich nun php.exe fritzbox_tam_on_off.php in einem DOS-Fenster eingebe, dann bekomme ich die Meldung:
"PHP Notice: Undefined variable: message in C.\php\fritzbox_tam_on_off.php on line 96"
"ERROR: Login failed with an unknown Response"
In der php-Datei steht in Zeile 96: $message .= $e->getMessage();

Könntet Ihr mal bitte helfen? Herzlichen Dank!

Und dann noch diese Frage: Ist es mit diesen PHP-Scripten nicht erforderlich, die Benutzerdaten einzugeben?
Die werden zumindest bei mir nicht abgefragt. Das kann ich mir aber nicht vorstellen. Könnte mir das bitte auch noch mal jemand erklären? Danke auch dafür.
 
Zuletzt bearbeitet:
Moin

In PHP inkludiert der Skripter gern andere PHP Skripte...
Code:
grep 'require' *api*.php
 *   require_once('fritzbox_api.class.php');
        require_once(__DIR__ . '/fritzbox_user' . $config_version . '.conf.php');
        require_once(__DIR__ . '/fritzbox_user.conf.php');
...so auch die Fritz!Box Zugangsdaten aus...
fritzbox_user.conf.php
PHP:
<?php
date_default_timezone_set('Europe/Berlin');
if ( !isset($this->config) )
{
  die(__FILE__ . ' must not be called directly');
}

####################### central API config ########################
# notice: you only have to set values differing from the defaults #
###################################################################

# use the new .lua login method in current (end 2012) labor and newer firmwares (Fritz!OS 5.50 and up)
$this->config->setItem('use_lua_login_method', true);

# set to your Fritz!Box IP address or DNS name (defaults to fritz.box), for remote config mode, use the dyndns-name like example.dyndns.org
$this->config->setItem('fritzbox_ip', 'fritz.box');

# if needed, enable remote config here
#$this->config->setItem('enable_remote_config', true);
#$this->config->setItem('remote_config_user', 'user');
#$this->config->setItem('remote_config_password', 'secret');

# set to your Fritz!Box username, if login with username is enabled (will be ignored, when remote config is enabled)
$this->config->setItem('username', 'HIER_DER_BENUTZER');

# set to your Fritz!Box password (defaults to no password, will be ignored, when remote config is enabled)
$this->config->setItem('password', 'HIER_DAS_PASSWORT');

# set the logging mechanism (defaults to console logging)
$this->config->setItem('logging', 'console'); // output to the console
#$this->config->setItem('logging', 'silent');  // do not output anything, be careful with this logging mode
#$this->config->setItem('logging', 'tam.log'); // the path to a writeable logfile

# the newline character for the logfile (does not need to be changed in most cases)
$this->config->setItem('newline', (PHP_OS == 'WINNT') ? "\r\n" : "\n");


############## module specific config ###############

# set the path for the call list for the foncalls module
$this->config->setItem('foncallslist_path', __DIR__ . '/anrufsliste.csv');
?>

HIER_DER_BENUTZER = ersetzen mit deinem Fritz!Box Benutzernamen
HIER_DAS_PASSWORT = ersetzen durch Passwort obigen Benutzernamens

Wichtig: Zeilen mit einem # am Anfang sind Kommentare und werden nicht abgearbeitet.
Achte also auch darauf.
 
Zuletzt bearbeitet:
OK, das wusste ich nicht.
Danke für die schnelle Antwort.

Ich habe jetzt die Zeile
require_once(__DIR__ . '/fritzbox_user.conf.php');
an der entsprechenden Stelle eingefügt und in der
fritzbox_user.conf.php
auch Benutzername und Passwort eingetragen.
Nun kommt allerdings die Fehlermeldung:
"PHP Datal error: Using $this when not in object context in C:\php\fritzbox_user.conf.php on line 2"
Und in der Zeile 2 steht:
if ( !isset($this->config) )

Dann wollte ich noch fragen, ob der Benutzername zwingend erforderlich ist, oder ob man im Heimnetz auch das Skript ausführen kann, wenn man nur den Zugang mit Passwort nutzt?
 
Ich habe jetzt die Zeile
require_once(__DIR__ . '/fritzbox_user.conf.php');
an der entsprechenden Stelle eingefügt
...brauchst du nicht, weil die fritzbox_api.class.php das schon macht.
Das Skript fritzbox_tam_on_off.php inkludiert wiederum die fritzbox_api.class.php.
Daten also schon vorhanden.
Deswegen auch die Fehlermeldung.

Dann wollte ich noch fragen, ob der Benutzername zwingend erforderlich ist, oder ob man im Heimnetz auch das Skript ausführen kann, wenn man nur den Zugang mit Passwort nutzt?
Einfach ein # vor die Zeile mit...
PHP:
# set to your Fritz!Box username, if login with username is enabled (will be ignored, when remote config is enabled)
$this->config->setItem('username', 'HIER_DER_BENUTZER');
...also so
PHP:
# set to your Fritz!Box username, if login with username is enabled (will be ignored, when remote config is enabled)
# $this->config->setItem('username', 'HIER_DER_BENUTZER');

Bei dieser bin ich mir nicht sicher...
PHP:
# use the new .lua login method in current (end 2012) labor and newer firmwares (Fritz!OS 5.50 and up)
$this->config->setItem('use_lua_login_method', true);
...einfach mal ausprobieren (true/false oder # am Anfang der Zeile). ;)
 
Zuletzt bearbeitet:
Danke!
So, das Skript läuft nun und ändert auch den Status.
Aber:
Es erscheint die Meldung: "Notice: TAM0 status change could have failed, should be 1 now, but I don't now if it actually is. Check your check section in the script."

In dem entsprechenden Bereich von dem Skript steht:
// check if the update succeded
$formfields = array(
'getpage' => '../html/de/menus/menu2.html',
'var:menu' => 'home',
'var: pagemaster' => 'fondevices',
'var: pagename' => 'fondevices',
);
$output = $fritz->doPostForm($formfields);

preg_match('@name="tam:settings/TAM' . $tam . '/Active" value="([10])"@i', $output, $matches);
if (isset($matches[1]) && $matches[1] == $mode)
{
$message .= $mode == 1 ? 'TAM' . $tam . ' enabled' : 'TAM' . $tam . ' disabled';
}
else if (isset($matches[1]))
{
$message .= 'ERROR: TAM' . $tam . ' status change failed, should be ' . $mode . ', but is ' . $matches[1];
}
else
{
$message .= 'NOTICE: TAM' . $tam . ' status change could have failed, should be ' . $mode . ' now, but I don\'t now if it actually is. Check your check section in the script.';
}

Noch eine Idee, was ich daran evt. ändern muss? In dem Bereich habe ich bisher nichts gegenüber dem Original verändert.
Zwischen "var:" und "pagemaster" und "var:" und "pagename" habe ich oben nur ein Leerzeichen eingefügt, weil sonst an der Stelle ein Smiley angezeigt wird.:rolleyes:

Die Variation mit / ohne Benutzername probiere ich später aus. Danke auch dafür!
 
Zuletzt bearbeitet:
Zwischen "var:" und "pagemaster" und "var:" und "pagename" habe ich oben nur ein Leerzeichen eingefügt, weil sonst an der Stelle ein Smiley angezeigt wird.
...dafür gibt es die PHP Tags.
Klick beim Posten unten rechts auf erweitert, klick auf den PHP Code Button und füg den PHP Code dazwischen ein.

Ignorier mal die Meldung und kontrolliere im AVM Webinterface ob geschaltet wurde.
Code:
/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 0
2015-01-19 12:38 NOTICE: TAM0 status change could have failed, should be 0 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 1
2015-01-19 12:39 NOTICE: TAM0 status change could have failed, should be 1 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # vi fritzbox_tam_on_off.php
/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 0 0
2015-01-19 12:41 NOTICE: TAM0 status change could have failed, should be 0 now, but I don't now if it actually is. Check your check section in the script
./var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 0 1
2015-01-19 12:41 NOTICE: TAM1 status change could have failed, should be 0 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 0 2
2015-01-19 12:41 NOTICE: TAM2 status change could have failed, should be 0 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 0 3
2015-01-19 12:41 NOTICE: TAM3 status change could have failed, should be 0 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 0 4
2015-01-19 12:41 NOTICE: TAM4 status change could have failed, should be 0 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 1 4
2015-01-19 12:41 NOTICE: TAM4 status change could have failed, should be 1 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 1 3
2015-01-19 12:41 NOTICE: TAM3 status change could have failed, should be 1 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 1 2
2015-01-19 12:42 NOTICE: TAM2 status change could have failed, should be 1 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 1 1
2015-01-19 12:42 NOTICE: TAM1 status change could have failed, should be 1 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDi/var/media/ftp/SanDisk-Cruzer-01/7360SL # php-cgi -f fritzbox_tam_on_off.php 1 0
2015-01-19 12:42 NOTICE: TAM0 status change could have failed, should be 1 now, but I don't now if it actually is. Check your check section in the script.
/var/media/ftp/SanDisk-Cruzer-01/7360SL #
...geschaltet wird trotzdem.
 
Zuletzt bearbeitet:
Ja, geschaltet wird bei mir auch einwandfrei.
Ich hatte nach Lesen des Threads auch mal mit Firefox mir den Quelltext von der Fritzbox-Oberfläche angeschaut.
Das erschlägt mich total, aber ich hatte die Idee, ob in dem Ursprungsskript für diese "Check-Routine" vielleicht nicht die Daten richtig ausgelesen können, weil vielleicht der Pfad nicht stimmt im Skript?
Nur so eine Idee eben...
 
@omnicom

Teste mal das fritzbox_tam_on_off.php hier:
PHP:
<?php
/**
 * Fritz!Box PHP tools CLI script to enable or disable answering machines
 *
 * Must be called via a command line, shows a help message if called without any or an invalid argument
 * Can log to the console or a logfile or be silent
 * new in v0.2: Can handle remote config mode via https://example.dyndns.org
 * new in v0.3: Refactored code to match API version 0.5
 *
 * Check the config file fritzbox.conf.php!
 * 
 * @author   Gregor Nathanael Meyer <Gregor [at] der-meyer.de>
 * @license  http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
 * @version  0.4 2013-01-02
 * @package  Fritz!Box PHP tools
 * 
 * Geändert: 19.01.2015 mit query.lua ab Firmware xxx.04.88 
 */

try
{
  // load the fritzbox_api class
  require_once('fritzbox_api.class.php');
  $fritz = new fritzbox_api();

  $fritz->config->setItem('sid', $fritz->getSID());
  
  // init the output message
  $message = date('Y-m-d H:i') . ' ';

  // handle the CLI arguments or give a help message
  if (isset($argv[1]) && ($argv[1] == 0 || $argv[1] == 1) )
  {
    $mode = (int)$argv[1];
  }
  else
  {
    if ( $fritz->config->getItem('logging') == 'console' )
    {
      echo '
  Enables or disables an answering machine (TAM) of a Fritz!Box
  
  Usage on UNIX systems:
    /path/to/php ' .  $argv[0] . ' {0|1} [optional: TAM]
  
  Usage on Windows systems:
    c:\path\to\php.exe ' .  $argv[0] . ' {0|1} [optional: TAM]
  
  0 disable the TAM
  1 enable the TAM
  
  The optional argument TAM and addresses the nth answering machine
  Defaults to 0, which is the TAM **600; 1 is the TAM **601 and so on
  
  Warning: A non existent TAM will not lead to an error message but adds a
  new TAM to the Fritz!Box, which will answer all incoming calls by default!
      ';
    }
    else
    {
      $fritz->logMessage($message . 'ERROR: Script was called without or with an invalid argument');
    }
    exit;
  }
  $tam = (isset($argv[2]) && $argv[2] >= 0 && $argv[2] <= 9) ? (int)$argv[2] : 0;


  // update the setting
  $URL = $fritz->config->getItem('fritzbox_url').'/cgi-bin/webcm';
  $POST = 'sid='.$fritz->config->getItem('sid').'&tam:settings/TAM' . $tam . '/Active=' . $mode;
  
  $dummy = http_post($URL, $POST);
  
  // check if the update succeded
  $outputi = $fritz->config->getItem('fritzbox_url').
  '/query.lua'.'?sid='.$fritz->config->getItem('sid').
  '&tam_'.$tam.'=tam:settings/TAM'.$tam.'/Active';
  
  $output = http_get($outputi);
  
  preg_match('@"tam_' . $tam . '": "([10])"@i', $output, $matches);

  if (isset($matches[1]) && $matches[1] == $mode)
  {
    $message .= $mode == 1 ? 'TAM' . $tam . ' enabled' : 'TAM' . $tam . ' disabled';
  }
  else if (isset($matches[1]))
  {
    $message .= 'ERROR: TAM' . $tam . ' status change failed, should be ' . $mode . ', but is ' . $matches[1];
  }
  else
  {
    $message .= 'NOTICE: TAM' . $tam . ' status change could have failed, should be ' . $mode . ' now, but I don\'t now if it actually is. Check your check section in the script.';
  }
}
catch (Exception $e)
{
  $message .= $e->getMessage();
}

// log the result
if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' )
{
  $fritz->logMessage($message);
}
else
{
  echo($message);
}
$fritz = null; // destroy the object to log out

exit;

function http_post($URL, $POST)
{
	$ch = curl_init($URL);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$login = curl_exec($ch);
	curl_close($ch);
	return $login;
}

function http_get($GET)
{
	$ch = curl_init($GET);
	curl_setopt($ch, CURLOPT_HTTPGET, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$login = curl_exec($ch);
	curl_close($ch);
	return $login;
}

?>
hat bei mir unter Windows nach ein paar Anpassungen Funktioniert.

20.01,2015: Kleine Änderung hatte curl_close($ch); vergessen

Erstellt mit eclipse-php ;)
 
Zuletzt bearbeitet:
Prima, herzlichen Dank! Das klappt wunderbar.
jetzt muss ich mir nur noch Gedanken machen, wie ich ein kleines Tool hinbekomme, so dass man aus einer Dialogbox heraus die einzelnen ABs ein- und ausschalten kann und vielleicht sogar optisch anzeigen kann, ob der AB an- oder ausgeschaltet ist.
Ich schaue mir gerade verschiedene Macrotools wie MacroX u.ä. an. Vielleicht kann ich es damit lösen.
 
Zuletzt bearbeitet:
Teste mal das fritzbox_tam_on_off.php hier:
PHP:
<?php
/**
 * Fritz!Box PHP tools CLI script to enable or disable answering machines
 *
 * Must be called via a command line, shows a help message if called without any or an invalid argument
 * Can log to the console or a logfile or be silent
 * new in v0.2: Can handle remote config mode via https://example.dyndns.org
 * new in v0.3: Refactored code to match API version 0.5
 *
 * Check the config file fritzbox.conf.php!
 * 
 * @author   Gregor Nathanael Meyer <Gregor [at] der-meyer.de>
 * @license  http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
 * @version  0.4 2013-01-02
 * @package  Fritz!Box PHP tools
 * 
 * Geändert: 19.01.2015 mit query.lua ab Firmware xxx.04.88 
 */

try
{
  // load the fritzbox_api class
  require_once('fritzbox_api.class.php');
  $fritz = new fritzbox_api();

  // init the output message
  $message = date('Y-m-d H:i') . ' ';

  // handle the CLI arguments or give a help message
  if (isset($argv[1]) && ($argv[1] == 0 || $argv[1] == 1) )
  {
    $mode = (int)$argv[1];
  }
  else
  {
    if ( $fritz->config->getItem('logging') == 'console' )
    {
      echo '
  Enables or disables an answering machine (TAM) of a Fritz!Box

  Usage on UNIX systems:
    /path/to/php ' .  $argv[0] . ' {0|1} [optional: TAM]

  Usage on Windows systems:
    c:\path\to\php.exe ' .  $argv[0] . ' {0|1} [optional: TAM]

  0 disable the TAM
  1 enable the TAM

  The optional argument TAM and addresses the nth answering machine
  Defaults to 0, which is the TAM **600; 1 is the TAM **601 and so on
  
  Warning: A non existent TAM will not lead to an error message but adds a
  new TAM to the Fritz!Box, which will answer all incoming calls by default!
      ';
    }
    else
    {
      $fritz->logMessage($message . 'ERROR: Script was called without or with an invalid argument');
    }
    exit;
  }
  $tam = (isset($argv[2]) && $argv[2] >= 0 && $argv[2] <= 9) ? (int)$argv[2] : 0;


  // update the setting
  $formfields = array(
  	'tam:settings/TAM'.$tam .'/Active' => $mode
  );

  $dummy = $fritz->doPostForm($formfields);

  // check if the update succeded
  $formfields = array(
  	'getpage' => '/query.lua',
  	'tam_'.$tam => 'tam:settings/TAM'.$tam.'/Active'
  );

  $output = $fritz->doGetRequest($formfields);
  
  preg_match('@"tam_' . $tam . '": "([10])"@i', $output, $matches);

  if (isset($matches[1]) && $matches[1] == $mode)
  {
    $message .= $mode == 1 ? 'TAM' . $tam . ' enabled' : 'TAM' . $tam . ' disabled';
  }
  else if (isset($matches[1]))
  {
    $message .= 'ERROR: TAM' . $tam . ' status change failed, should be ' . $mode . ', but is ' . $matches[1];
  }
  else
  {
    $message .= 'NOTICE: TAM' . $tam . ' status change could have failed, should be ' . $mode . ' now, but I don\'t now if it actually is. Check your check section in the script.';
  }
}
catch (Exception $e)
{
  $message .= $e->getMessage();
}

// log the result
if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' )
{
  $fritz->logMessage($message);
}
else
{
  echo($message);
}
$fritz = null; // destroy the object to log out

?>

habe hier nach ein paar Tests jetzt http_get und http_post wieder Entfernt,
nach dem es mir jetzt auch mit doGetRequest / doPostForm gelungen
ist nach einigen Fehlversuchen ;)

Erstellt mit eclipse-php ;)
 
Abend

Klappt gut, dankeschön.
Code:
# php-cgi -f fritzbox_tam_on_off.php 0 0
2015-01-20 18:23 TAM0 disabled
# php-cgi -f fritzbox_tam_on_off.php 0 1
2015-01-20 18:24 TAM1 disabled
# php-cgi -f fritzbox_tam_on_off.php 0 2
2015-01-20 18:24 TAM2 disabled
# php-cgi -f fritzbox_tam_on_off.php 0 3
2015-01-20 18:24 TAM3 disabled
# php-cgi -f fritzbox_tam_on_off.php 0 4
2015-01-20 18:24 TAM4 disabled
# php-cgi -f fritzbox_tam_on_off.php 1 4
2015-01-20 18:25 TAM4 enabled
# php-cgi -f fritzbox_tam_on_off.php 1 3
2015-01-20 18:25 TAM3 enabled
# php-cgi -f fritzbox_tam_on_off.php 1 2
2015-01-20 18:25 TAM2 enabled
# php-cgi -f fritzbox_tam_on_off.php 1 1
2015-01-20 18:25 TAM1 enabled
# php-cgi -f fritzbox_tam_on_off.php 1 0
2015-01-20 18:25 TAM0 enabled
 
Sieht schon Aufgeräumter aus als das letzte von dir,
aber nur mal so nebenbei hier:
PHP:
  $formfields = array(
  		'getpage' => '/query.lua',
  		'tam_'.$tam.'_Active' => 'tam:settings/TAM'.$tam.'/Active',
  		'tam_'.$tam.'_Name' => 'tam:settings/TAM'.$tam.'/Name',
  		'tam_'.$tam.'_NumNewMessages' => 'tam:settings/TAM'.$tam.'/NumNewMessages',
  		'tam_'.$tam.'_NumOldMessages' => 'tam:settings/TAM'.$tam.'/NumOldMessages'
  );
  
  $output = $fritz->doGetRequest($formfields);

  echo "\n";
  echo $output;

  echo "\n";

  $output_o = json_decode($output);

  echo "\n"; echo "TAM".$tam." Active: "; echo $output_o->{'tam_'.$tam.'_Active'};
  echo "\n"; echo "TAM".$tam." Name: "; echo $output_o->{'tam_'.$tam.'_Name'};
  echo "\n"; echo "TAM".$tam." NumNewMessages: "; echo $output_o->{'tam_'.$tam.'_NumNewMessages'};
  echo "\n"; echo "TAM".$tam." NumOldMessages: "; echo $output_o->{'tam_'.$tam.'_NumOldMessages'};
nur zum Test einfach mal vor der Zeile
PHP:
$fritz = null; // destroy the object to log out
am Ende Einfügen,
vielleicht kann das auch einer gebrauchen.

;)
 
Zuletzt bearbeitet:
OK, logisch, das ist hilfreich für reine Statusmeldungen.
...ohne was schalten zu wollen, oder davor, oder doch lieber hinterher. :gruebel:
 
Habe oben noch das hinzugefügt

PHP:
  echo "\n";

  $output_o = json_decode($output);

  echo "\n"; echo "TAM".$tam." Active: "; echo $output_o->{'tam_'.$tam.'_Active'};
  echo "\n"; echo "TAM".$tam." Name: "; echo $output_o->{'tam_'.$tam.'_Name'};
  echo "\n"; echo "TAM".$tam." NumNewMessages: "; echo $output_o->{'tam_'.$tam.'_NumNewMessages'};
  echo "\n"; echo "TAM".$tam." NumOldMessages: "; echo $output_o->{'tam_'.$tam.'_NumOldMessages'};

damit bekommt man eine bessere Ausgabe wenn man den Json zerlegt

Code:
TAM0 Active: 0
TAM0 Name: Anrufbeantworter 1
TAM0 NumNewMessages: 0
TAM0 NumOldMessages: 1

Json
Code:
{
 "tam_0_Active": "0",
 "tam_0_Name": "Anrufbeantworter 1",
 "tam_0_NumNewMessages": "0",
 "tam_0_NumOldMessages": "1"
}

;)
 
  • Like
Reaktionen: zbgwek

Zurzeit aktive Besucher

Neueste Beiträge

Statistik des Forums

Themen
245,095
Beiträge
2,224,312
Mitglieder
371,939
Neuestes Mitglied
ceburos
Holen Sie sich 3CX - völlig kostenlos!
Verbinden Sie Ihr Team und Ihre Kunden Telefonie Livechat Videokonferenzen

Gehostet oder selbst-verwaltet. Für bis zu 10 Nutzer dauerhaft kostenlos. Keine Kreditkartendetails erforderlich. Ohne Risiko testen.

3CX
Für diese E-Mail-Adresse besteht bereits ein 3CX-Konto. Sie werden zum Kundenportal weitergeleitet, wo Sie sich anmelden oder Ihr Passwort zurücksetzen können, falls Sie dieses vergessen haben.