Wie Constructor aus einem Skript aufrufen

Servus. In einem anderen Post habe ich schonmal darüber diskutiert ein Internetradio (Musicpal von Freecom) über PHP anzusteuern.

Dank „jonofe“ aus dem Freecom-Forum gibt es jetzt so ein Skript.

Ansich könnte ich ja jetzt zufrieden sein, nur bekomme ich es nicht zum laufen.
Man muss dazu sagen, dass ich aus der Delphi/Pascal Welt komme und bei PHP einiges anders ist.
Wie kann ich denn den Constructor von einem Script aufrufen. Bei dem oben erwähnten Skript müssen da nämlich IP, Logindaten … übergeben werden.
ich habe da schonmal so angefangen (nur so ansatzweise).

<?

require(MusicPal.ips.php)
$mp_cfg = array(ip="192.168.178.41",user="Admin",password="admin");
musicpal($mp_cfg);

// Funktionsaufruf ????
if musicpal.is_on ( ... mach irgendwas

?>

und hier das Skript welches ich verwenden will …

<?php


//
// The class musicpal defines an musicpal object to control a musicpal device.
// It consists of several control functions like power_down, power_up, etc.
// The constructor expects an associative array containing the keys "ip", "user", "password"
// with the appropriate values
//

class musicpal
{
   var $ip;
   var $user;
   var $password;
   var $state;
   
   //
   // musicpal(): creates the object and saves the IP address, user name and password
   // to access the musciapl device
   //
   function musicpal($mp_cfg)
   {
      $this->ip = $mp_cfg["ip"];
      $this->user = $mp_cfg["user"];
      $this->password = $mp_cfg["password"];
      $this->get_state();
   }
   
   //
   // power_up(): switches the musicpal on from standby
   //   
   function power_up()
   {
      $get_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/ipc_send?power_up";
      http_get($get_url);
   }
   
   //
   // power_down(): switches the musicpal down to standby
   //   
   function power_down()
   {
      $get_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/ipc_send?power_down";
      http_get($get_url);
   }
   
   function volume($vol)
   {
      $get_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/admin.cgi?f=volume_set&v=".$vol;
      http_get($get_url);
   }
   
   function favorite($no)
   {
      $get_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/admin.cgi?f=favorites&n=../favorites.html&a=p&i=".$no;
      http_get($get_url);
   }
      
   function play_pause()
   {
      $get_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/admin.cgi?f=play_pause";
      http_get($get_url);
   }
   
   function get_state()
   {
      $get_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/state.cgi?fav=0";
      $state_xml = http_get($get_url);
      $state_xml = substr($state_xml,strpos($state_xml,"<volume>"));
      $this->state = simplexml_load_string("<state>".$state_xml);
   }

   function is_on()
   {
      $this->get_state();
      if ($this->state->power_state == 1)
         return TRUE;
      else    
         return FALSE;
   }

   function is_off()
   {
      $this->get_state();
      if ($this->state->power_state == 1)
         return FALSE;
      else    
         return TRUE;
   }

   //
   // This functions sets the brightness of the MusicPal display depending of its status
   //
   // $brightness: value in range from 0 to 10
   //
   // $displayStatus indicates which of the following 3 status you want to change
   // "active" - brightness when MP is in active mode
   // "sleeping" - brightness when MP is in dleeping mode
   // "sleep_timer" - brightness when sleep timer of MP is active
   //
   function brightness($brightness,$mpStatus="active")
   {
      $post_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/admin.cgi?f=display_brightness_".$mpStatus."&n=../display_brightness_".$mpStatus.".html";
      $post_data = array('brightness'=>$brightness, 'apply' => 'Verwenden');
      http_post_fields($post_url,$post_data);
   }
   
   function is_playing()
   {
      $this->get_state();
      if ($this->state->player_state == 1)
         return TRUE;
      else    
         return FALSE;
   }
   
   function pause()
   {
         if ($this->is_playing())
            $this->play_pause();
   }

   function play()
   {
         if (!$this->is_playing())
            $this->play_pause();
   }
   
   function get_volume()
   {
      $this->get_state();
      return $this->state->volume;
   }

   function is_radio()
   {
      if ($this->state->is_internet_radio == 1)
         return TRUE;
      else    
         return FALSE;
      
   }
   
   function send_message($rows)
   {
      $msg = rawurlencode(implode("§",$rows));
      $get_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/ipc_send?show_list%20".$msg;
      echo "URL: ".$get_url."
";
      http_get($get_url);
   }
   
   function confirm_message()
   {
      $get_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/ipc_send?menu_collapse";
      if (!$this->is_on())
      {
         $this->power_up();
         sleep(1);
         http_get($get_url);
         $this->power_down();
      } else
         http_get($get_url);
      
   }
} 

?>

siehe hier: PHP: Klassen und Objekte (PHP 5) - Manual


//Konfig anlegen
$mp_cfg["ip"]="192.168.178.41";
$mp_cfg["user"]="Admin";
$mp_cfg["password"]="admin";

//objekt erzeugen
$mp=new musicpal($mp_cfg);
//objekt verwenden
if ($mp->is_on()  ){
//tue etwas
}

Dein Script verwendet noch die PHP4-Syntax, evtl muss man das noch einmal anpassen.

Tommi

ich frage mich gerade ob es in der function is_off() nicht power_state == 0 sein sollte :confused:

Grüße Andreas

Das ist schon korrekt so, wenn Du mal auf das TRUE und FALSE danach achtest ;).

Ohhhhh … war wohl noch ein bissel früh :wink:

Ich habe jetzt immer noch eine Fehlermeldung, und bekomme es einfach nicht hin. Kann das was damit zu tun haben, was Tommi geschrieben hat?

Was ist denn die Variable $this->???. Was genau macht die denn?

Im Anhang die Fehlermeldung (bezieht sich auf das Skript oben):

<?
require("MusicPal.ips.php");
//Konfig anlegen
$mp_cfg["ip"]="192.168.178.41";
$mp_cfg["user"]="Admin";
$mp_cfg["password"]="admin";

//objekt erzeugen
$mp=new musicpal($mp_cfg);

//MusicPal anschalten
$mp->power_on


?>
Warning:  simplexml_load_string() [function.simplexml-load-string]: Entity: line 11: parser error : Premature end of data in tag state line 1 in C:\IP-Symcon\scripts\MusicPal.ips.php on line 71

Warning:  simplexml_load_string() [function.simplexml-load-string]:  in C:\IP-Symcon\scripts\MusicPal.ips.php on line 71

Warning:  simplexml_load_string() [function.simplexml-load-string]: ^ in C:\IP-Symcon\scripts\MusicPal.ips.php on line 71

Wenn Du was über Klassen lernen willst, solltest Du Dir schon die Grundlagen in der verlinkten PHP-Doku durchlesen. Gleich oben wird auch $this erwähnt (PHP: Die Grundlagen - Manual). $this verweist auf i.d.R. das aufrufende Objekt, d.h. wenn Du ein instanziiertes Objekt der Klasse Mensch bist bekommst Du über $this eine Referenz auf dich selbst. Oder auf gut Deutsch: $this->esse() entspräche „Ich befehlige mir hiermit zu essen“.

Ersetze mal Zeile 71 mit

$this->state = simplexml_load_string("<state>".$state_xml."</state>");

Der Fehler besagt nämlich, dass da ein Tag nicht richtig abgeschlossen worden ist. Ich vermute mal, das müsste damit behoben sein.

Hi, wo bekomme ich denn die Funktion http_get her ???

Googeln führte mich nur zu pecl.php.net … aber da gibt nur den Quellcode, keine dll`s :confused::confused::confused:

Grüße

Andreas

Der Sinn von PECL ist ja auch das Bereitstellen von Funktionen als PHP-Code. Daher: Einfach den Code reinkopieren.

Hi Horst,

ich blick das im Moment gerade garnicht …

Wenn ich mir hier PECL :: Package :: pecl_http dieses Package runterlade http://pecl.php.net/get/pecl_http-1.6.5.tgz - ich finde da nur C-Quellcode …

Sorry, wenn ich auf dem Schlach stehe, aber welchen Code muss ich „einfach“ reinkopieren :confused::confused:

Schonmal Danke & Grüße Andreas

Hallo zusammen,

da ich keine Lust hatte, mit jeder neuen PHP-Version die in IPS benutzt wird, mit wieder gedanken über die PECL zu machen, hier eine Version die mit CURL auskommt. Die php_curl.dll ist im Standart-PHP-Paket enthalten.

Ich habe auch gleich noch eine Funkton ergänzt, welche die Favouriten in einem Array zurück liefert.


<?

//
// The class musicpal defines an musicpal object to control a musicpal device.
// It consists of several control functions like power_down, power_up, etc.
// The constructor expects an associative array containing the keys "ip", "user", "password"
// with the appropriate values
//

function gethttp($url,$user,$pass)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$pass);
	$data=curl_exec($ch);
	curl_close($ch);
	return($data);
}
	
class musicpal
{
   var $ip;
   var $user;
   var $password;
   var $state;


   //
   // musicpal(): creates the object and saves the IP address, user name and password
   // to access the musciapl device
   //
   function musicpal($mp_cfg)
   {
      $this->ip = $mp_cfg["ip"];
      $this->user = $mp_cfg["user"];
      $this->password = $mp_cfg["password"];
		$this->get_state();
   }
   

	
   //
   // power_up(): switches the musicpal on from standby
   //
   function power_up()
   {
      $get_url = "http://".$this->ip."/admin/cgi-bin/ipc_send?power_up";
      gethttp($get_url,$this->user,$this->password);
   }

   //
   // power_down(): switches the musicpal down to standby
   //
   function power_down()
   {
      $get_url = "http://".$this->ip."/admin/cgi-bin/ipc_send?power_down";
      gethttp($get_url,$this->user,$this->password);
   }

   function volume($vol)
   {
      $get_url = "http://".$this->ip."/admin/cgi-bin/admin.cgi?f=volume_set&v=".$vol;
      gethttp($get_url,$this->user,$this->password);
   }

   function favorite($no)
   {
      $get_url = "http://".$this->ip."/admin/cgi-bin/admin.cgi?f=favorites&n=../favorites.html&a=p&i=".$no;
      gethttp($get_url,$this->user,$this->password);
   }

   function get_favorites()
   {
      $get_url = "http://".$this->ip."/admin/cgi-bin/favorites.cgi";
      $data = gethttp($get_url,$this->user,$this->password);
      $zeilen = explode("
",$data);
      foreach($zeilen as $key => $value)
		{
		   $teile = explode(",", $value);
		   if ( $teile[0] == "#EXTINF:-1" )
			{
		      $favorites[]= $teile[1];			}
		}
		return($favorites);
	}
   
   function play_pause()
   {
      $get_url = "http://".$this->ip."/admin/cgi-bin/admin.cgi?f=play_pause";
      gethttp($get_url,$this->user,$this->password);
   }

   function get_state()
   {
      $get_url = "http://".$this->ip."/admin/cgi-bin/state.cgi?fav=0";
      $state_xml = gethttp($get_url,$this->user,$this->password);
      $state_xml = substr($state_xml,strpos($state_xml,"<volume>"));
      $this->state = simplexml_load_string("<state>".$state_xml);
   }

   function is_on()
   {
      $this->get_state();
      if ($this->state->power_state == 1)
         return TRUE;
      else
         return FALSE;
   }

   function is_off()
   {
      $this->get_state();
      if ($this->state->power_state == 1)
         return FALSE;
      else
         return TRUE;
   }

   //
   // This functions sets the brightness of the MusicPal display depending of its status
   //
   // $brightness: value in range from 0 to 10
   //
   // $displayStatus indicates which of the following 3 status you want to change
   // "active" - brightness when MP is in active mode
   // "sleeping" - brightness when MP is in dleeping mode
   // "sleep_timer" - brightness when sleep timer of MP is active
   //
   //function brightness($brightness,$mpStatus="active")
   //{
   //   $post_url = "http://".$this->user.":".$this->password."@".$this->ip."/admin/cgi-bin/admin.cgi?f=display_brightness_".$mpStatus."&n=../display_brightness_".$mpStatus.".html";
   //   $post_data = array('brightness'=>$brightness, 'apply' => 'Verwenden');
   //   http_post_fields($post_url,$post_data);
   //}

   function is_playing()
   {
      $this->get_state();
      if ($this->state->player_state == 1)
         return TRUE;
      else
         return FALSE;
   }

   function pause()
   {
         if ($this->is_playing())
            $this->play_pause();
   }

   function play()
   {
         if (!$this->is_playing())
            $this->play_pause();
   }

   function get_volume()
   {
      $this->get_state();
      return $this->state->volume;
   }

   function is_radio()
   {
      if ($this->state->is_internet_radio == 1)
         return TRUE;
      else
         return FALSE;

   }

   function send_message($rows)
   {
      $msg = rawurlencode(implode("§",$rows));
      $get_url = "http://".$this->ip."/admin/cgi-bin/ipc_send?show_list%20".$msg;
      echo "URL: ".$get_url."
";
      gethttp($get_url,$this->user,$this->password);
   }

   function confirm_message()
   {
      $get_url = "http://".$this->ip."/admin/cgi-bin/ipc_send?menu_collapse";
      if (!$this->is_on())
      {
         $this->power_up();
         sleep(1);
         gethttp($get_url,$this->user,$this->password);
         $this->power_down();
      } else
         gethttp($get_url,$this->user,$this->password);

   }
}

?>

Ich habe übrigens vorher PECL mit PEAR verwechselt schäm. PEAR ist natürlich die Variante in PHP-Code. Ich vermute mal, dass man auch einfach file_get_contents(„http://user:pwd@url/“) benutzen kann. Da braucht man dann auch kein CURL für.

Hi,

leider funktioniert das mit file_get_contents nicht - wäre ja zu einfach :smiley:

Grüße Andreas