2013-11-14 48 views
0

控制的Sonos时,这是我的错误:错误405试图通过PHP

exception 'Exception' with message 'Error sending command: HTTP/1.1 405 Method Not Allowed Allow: GET, HEAD Content-type: text/html Server: Linux UPnP/1.0 Sonos/24.0-69180 (BR100) Connection: close 
Error 405 

Method Not Allowed 
' in C:\wamp\www\PHPSonos.inc.php:240 Stack trace: #0 C:\wamp\www\PHPSonos.inc.php(148): PHPSonos->sendPacket('POST /MediaRend...') #1 C:\wamp\www\demo.php(9): PHPSonos->SetRadio('x-rincon-mp3rad...') #2 {main} nope. 

我主持过WAMP的8080端口上的最新版本的服务器每当我尝试访问我的主页面,演示.php,它应该向SONOS播放器发送SOAP/UPNP请求。不幸的是,它总是出现错误405.我需要帮助解决这个尽快。

这是我的代码: 主页:

<?php 
include("PHPSonos.inc.php"); 

$sonos = new PHPSonos("192.168.1.5"); //Sonos ZP IPAdresse 

//Grundfunktionen 
try { 
//$sonos->Pause(); 
$sonos->SetRadio("x-rincon-mp3radio://players.creacast.com/creacast/klassik/playlist.pls"); 
$sonos->Play(); 
} 
catch(Exception $e) 
{ 
echo $e; 
} 
//$sonos->Play(); 
//$sonos->Next(); 
//$sonos->Previous(); 
//$sonos->Rewind(); 
//$sonos->SetVolume(1); //0-100 in % 
//$sonos->SetPlayMode("NORMAL"); //REPEAT_ALL, SHUFFLE, NORMAL 
//$sonos->SetMute(false); //True = MUTE, False = KEIN MUTE 

//Klassik Radio abspielen 
//$sonos->SetRadio("x-rincon-mp3radio://players.creacast.com/creacast/klassik/playlist.pls"); 
//$sonos->Play(); 

//Neue MP3 abspielen 
//$sonos->ClearQueue(); //Playlist löschen 
//$sonos->AddToQueue("x-file-cifs://ipsserver/Public/test.mp3"); //Datei hinzufügen 
//$sonos->SetQueue("x-rincon-queue:RINCON_"."HIER DIE MAC DES PLAYERS ZB: FFEEDDCCBBAA"."01400#0"); //Playlist auswählen (Nötig, wenn Radio vorher ausgewählt war) 
//$sonos->Play(); 
?> 
<html> 
<body> 
nope. 
</body> 
</html> 

脚本它导入:

<?php 

//Sonos PHP Script 
//Copyright: Michael Maroszek 
//Version: 1.0, 09.07.2009 

class PHPSonos { 
    private $address = ""; 

    public function __construct($address) { 
     $this->address = $address; 
    } 

    public function Pause() 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: 252 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#Pause" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Pause xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:Pause></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function Play() 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: 266 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#Play" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Play xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Speed>1</Speed></u:Play></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function Next() 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: 250 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#Next" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Next xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:Next></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function Previous() 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: 258 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#Previous" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Previous xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:Previous></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function Rewind() 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: 296 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#Seek" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Seek xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Unit>REL_TIME</Unit><Target>00:00:00</Target></u:Seek></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function SetVolume($volume) 
    { 

$content='POST /MediaRenderer/RenderingControl/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: 32'.strlen($volume).' 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:RenderingControl:1#SetVolume" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"><InstanceID>0</InstanceID><Channel>Master</Channel><DesiredVolume>'.$volume.'</DesiredVolume></u:SetVolume></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function SetMute($mute) 
    { 

     if($mute) { $mute = "1"; } else { $mute = "0"; } 

$content='POST /MediaRenderer/RenderingControl/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: 314 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:RenderingControl:1#SetMute" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetMute xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"><InstanceID>0</InstanceID><Channel>Master</Channel><DesiredMute>'.$mute.'</DesiredMute></u:SetMute></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function SetPlayMode($mode) 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: '.(291+strlen($mode)).' 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#SetPlayMode" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetPlayMode xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><NewPlayMode>'.$mode.'</NewPlayMode></u:SetPlayMode></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function SetRadio($radio) 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: '.(974+strlen(htmlspecialchars($radio))).' 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetAVTransportURI xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><CurrentURI>'.htmlspecialchars($radio).'</CurrentURI><CurrentURIMetaData>&lt;DIDL-Lite xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot; xmlns:r=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot; xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;&gt;&lt;item id=&quot;R:0/0/0&quot; parentID=&quot;R:0/0&quot; restricted=&quot;true&quot;&gt;&lt;dc:title&gt;IP-Symcon Radio&lt;/dc:title&gt;&lt;upnp:class&gt;object.item.audioItem.audioBroadcast&lt;/upnp:class&gt;&lt;desc id=&quot;cdudn&quot; nameSpace=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot;&gt;SA_RINCON65031_&lt;/desc&gt;&lt;/item&gt;&lt;/DIDL-Lite&gt;</CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function SetQueue($queue) 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: '.(342+strlen(htmlspecialchars($queue))).' 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetAVTransportURI xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><CurrentURI>'.htmlspecialchars($queue).'</CurrentURI><CurrentURIMetaData></CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function ClearQueue() 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: 290 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#RemoveAllTracksFromQueue" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:RemoveAllTracksFromQueue xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:RemoveAllTracksFromQueue></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function AddToQueue($file) 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: '.(438+strlen(htmlspecialchars($file))).' 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:AddURIToQueue xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><EnqueuedURI>'.htmlspecialchars($file).'</EnqueuedURI><EnqueuedURIMetaData></EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>1</EnqueueAsNext></u:AddURIToQueue></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function RemoveFromQueue($track) 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: '.(307+strlen($track)).' 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#RemoveTrackFromQueue" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:RemoveTrackFromQueue xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><ObjectID>Q:0/'.$track.'</ObjectID></u:RemoveTrackFromQueue></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    public function SetTrack($track) 
    { 

$content='POST /MediaRenderer/AVTransport/Control HTTP/1.1 
CONNECTION: close 
HOST: '.$this->address.':1400 
CONTENT-LENGTH: '.(288+strlen($track)).' 
CONTENT-TYPE: text/xml; charset="utf-8" 
SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#Seek" 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Seek xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Unit>TRACK_NR</Unit><Target>'.$track.'</Target></u:Seek></s:Body></s:Envelope>'; 

     $this->sendPacket($content); 
    } 

    private function sendPacket($content) 
    { 
     $fp = fsockopen($this->address, 1400 /* Port */, $errno, $errstr, 10); 
     if (!$fp) 
      throw new Exception("Error opening socket: ".$errstr." (".$errno.")"); 

     fputs ($fp, $content); 
     $ret = ""; 
     while (!feof($fp)) { 
      $ret.= fgets($fp,128); 
     } 
     fclose($fp); 

     if(strpos($ret, "200 OK") === false) 
      throw new Exception("Error sending command: ".$ret); 
    } 

} 

?> 

回答

0

假设根据UPnP规范的渲染工作(我说的是明确的,因为它是一个相当大假设)。

根据规范控制消息应发送这样的:

  1. 发送控制消息定期POST,不要用肥皂MAN
  2. 如果答案是405,发送再次使用M-POST和包括值为“http://schemas.xmlsoap.org/soap/envelope/”的MAN标头

您正在使用的PHP脚本未实现第二部分。参见UPnP Device Architecture pdf,第3.2.1节。