This is a very simple a straight forward UPNP play function. It searches a file on a mediaserver and plays it on the mediaplayer. It does not include search of mediaplayer/server URLs as the UPNP discovery take some time. Use the script below to find the correct urls.
<?php $mediaserver_url = 'http://192.168.1.1:49200/ctl/ContentDir'; $mediaplayer_url = 'http://192.168.1.22:9020/AVTransport/Control'; // From http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/ function do_post_request($url, $data, $optional_headers = null) { $params = array('http' => array( 'method' => 'POST', 'content' => $data)); if ($optional_headers !== null) { $params['http']['header'] = $optional_headers; } $ctx = stream_context_create($params); $fp = fopen($url, 'rb', false, $ctx); //$fp = @fopen($url, 'rb', false, $ctx); //if (!$fp) { throw new Exception("Problem with $url, $php_errormsg");} $response = stream_get_contents($fp); //$response = @stream_get_contents($fp); //if ($response === false) { throw new Exception("Problem reading data from $url, $php_errormsg"); } return $response; } function upnp_play_film($mediaserver_url, $mediaplayer_url, $film) { // Get nasename to be searched $info = pathinfo($film); $film = @basename($film,'.'.$info['extension']); // Search on the mediaserver $upnp_search = '<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ns0:Search xmlns:ns0="urn:schemas-upnp-org:service:ContentDirectory:1"><ContainerID>0</ContainerID><SearchCriteria>dc:title="%s"</SearchCriteria><Filter></Filter><StartingIndex>0</StartingIndex><RequestedCount>0</RequestedCount><SortCriteria /></ns0:Search></s:Body></s:Envelope>'; $upnp_search_headers = 'SOAPACTION: "urn:schemas-upnp-org:service:ContentDirectory:1#Search"\ncontent-type: text/xml ;charset="utf-8"\nconnection: close'; $ret = do_post_request($mediaserver_url, sprintf($upnp_search,htmlspecialchars($film)),$upnp_search_headers); // Extract meta and url $r = simplexml_load_string($ret); $res = $r->xpath("//Result"); $r = simplexml_load_string($res[0]); $film_meta = ltrim(str_replace('<?xml version="1.0"?>','',$r->asXML())); $film_url = $r->item->res; // Play on the mediaplayer if (strlen($film_url) > 4) { // SetAVTransportURI $upnp_setav = '<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ns0:SetAVTransportURI xmlns:ns0="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><CurrentURI>%s</CurrentURI><CurrentURIMetaData>%s</CurrentURIMetaData></ns0:SetAVTransportURI></s:Body></s:Envelope>'; $upnp_setav_headers = 'SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI"\ncontent-type: text/xml ;charset="utf-8"\nconnection: close'; $ret = do_post_request($mediaplayer_url, sprintf($upnp_setav,htmlspecialchars($film_url),htmlspecialchars($film_meta)),$upnp_setav_headers); // Play $upnp_play = '<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ns0:Play xmlns:ns0="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Speed>1</Speed></ns0:Play></s:Body></s:Envelope>'; $upnp_play_headers = 'SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#Play"\ncontent-type: text/xml ;charset="utf-8"\nconnection: close'; $ret = do_post_request($mediaplayer_url, $upnp_play,$upnp_play_headers); } return $film_url; } $film = "Your film"; // Full name, without path, and without extension print upnp_play_film($mediaserver_url, $mediaplayer_url, $film); ?>
Discover UPNP devices on your network (and small tutorial for gupnp).
<table> <?php $aimRenderer['name'] = "BboxTV"; // urn:schemas-upnp-org:service:AVTransport:1 $aimRenderer['udn'] = "uuid:56076f6e-6b79-4d65-6435-e8be81811ddd"; $aimRenderer['Service'] = 0; $aimServer['name'] = "Server"; // urn:schemas-upnp-org:service:ContentDirectory:1 $aimServer['udn'] = "uuid:4d696e69-444c-164e-9d41-001e8c6f8837"; $aimServer['Service'] = 0; function device_proxy_available_cb($proxy, $arg) { global $context,$cp,$aimRenderer, $aimServer; $info = gupnp_device_info_get($proxy); printf("<tr>"); printf("<td>%s</td>", $info['device_type']); printf("<td><a href='%s'>%s</a></td>", $info['location'],$info['friendly_name']); printf("<td>%s</td>", $info['udn']); printf("</tr>\n"); flush(); if ($aimRenderer == $info['friendly_name']) { printf("Found Renderer !"); } if ($aimServer == $info['friendly_name']) { printf("Found Server !"); $aimServerService = gupnp_device_info_get_service($proxy,"urn:schemas-upnp-org:service:ContentDirectory:1"); } if (($aimRendererService) && ($aimServerService)) { printf("Stop\n"); gupnp_control_point_browse_stop($cp); } } function service_proxy_available_cb($proxy, $arg) { global $context,$cp,$aimRenderer, $aimServer; $info = gupnp_service_info_get($proxy); //var_dump($info); printf("<tr>"); printf("<td>%s</td>", $info['service_type']); printf("<td><a href='%s'>%s</a></td>", $info['location'],$info['friendly_name']); printf("<td>%s</td>", $info['udn']); printf("</tr>\n"); flush(); if (($info['udn'] == $aimRenderer['udn']) && ($info['service_type'] == "urn:schemas-upnp-org:service:AVTransport:1")) { printf("Found Renderer !"); $aimRenderer['service'] = $proxy; } if (($info['udn'] == $aimServer['udn']) && ($info['service_type'] == "urn:schemas-upnp-org:service:ContentDirectory:1")) { printf("Found Server !"); $aimServer['service'] = $proxy; } if (($aimRenderer['service']) && ($aimServer['service'])) { printf("Stop\n"); gupnp_control_point_browse_stop($cp); } } $context = gupnp_context_new(); if (!$context) { printf("Error creating the GUPnP context\n"); exit(-1); } $cp = gupnp_control_point_new($context, "ssdp:all"); gupnp_control_point_callback_set($cp, GUPNP_SIGNAL_DEVICE_PROXY_AVAILABLE, 'device_proxy_available_cb'); gupnp_control_point_callback_set($cp, GUPNP_SIGNAL_SERVICE_PROXY_AVAILABLE, 'service_proxy_available_cb'); gupnp_context_timeout_add($context, 5000, "gupnp_control_point_browse_stop", $cp); gupnp_control_point_browse_start($cp); ?> </table> Fini !