How do I play a specific piece of media remotely through an HTTP request (playMedia command)?

Hello,

I’m looking for some help with playing a specific piece of media (TV Show episode or Movie) remotely through an HTTP request. I’m currently using the following PHP API: github.com/nickbart/php-plex.

Here is the code I use to initialize the API:

$servers = array(
    'jaszzz' => array(
        'address' => '[SERVER_IP]'
    )
);

$plex = new Plex();
$plex->registerServers($servers);

$server = $plex->getServer('jaszzz');
$client = $plex->getClient('[ROKU_UNIQUE_ID]');

$playback = $client->getPlaybackController();
$application = $client->getApplicationController();

I’m able to successfully call the Pause and Play functions:

$playback->pause();
$playback->play();

The API generates the below URLs:

http://[SERVER_IP]:[SERVER_PORT]/system/players/[ROKU_IP]/playback/pause
http://[SERVER_IP]:[SERVER_PORT]/system/players/[ROKU_IP]/playback/play

Here is the code I’m trying to use to get a movie entity within my server, and start playback at the beginning of the entity:

$section = $server->getLibrary()->getSection('Movies');
$movie = $section->getMovie('[MOVIE_NAME]');

$application->playMedia($movie);

The API generates the below URL:

http://[SERVER_IP]:[SERVER_PORT]/system/players/[ROKU_IP]/application/playMedia?key=/library/metadata/17150&path=http://[SERVER_IP]:[SERVER_PORT]/library/metadata/17150

When I go to this URL manually, I get the following XML response:

<Response code="2000" status="HTTPError: ">
<Traceback>
Traceback (most recent call last): File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/runtime.py", line 843, in handle_request result = f(**d) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/System.bundle/Contents/Code/playerservice.py", line 67, in process_legacy_remote_command result = cmd(**kwargs) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/code/sandbox.py", line 19, in _apply return apply(f, args, kwargs) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/api/playerkit.py", line 395, in f return req.content File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 265, in content return self.__str__() File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 243, in __str__ self.load() File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 181, in load f = self._opener.open(req, timeout=self._timeout) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 444, in open response = meth(req, response) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 557, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 482, in error return self._call_chain(*args) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 416, in _call_chain result = func(*args) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 565, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 400: Bad Request
</Traceback>
</Response>

I read here that “playMedia” is a “playback” command, rather than an “application” command. I swapped out the commands in the API so that application was replaced with playback - generated URL below:

http://[SERVER_IP]:[SERVER_PORT]/system/players/[ROKU_IP]/playback/playMedia?key=/library/metadata/17150&path=http://192.168.0.117:32400/library/metadata/17150

Resulting XML response:

<Response code="2000" status="BadStatusLine: ">
<Traceback>
Traceback (most recent call last): File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/runtime.py", line 843, in handle_request result = f(**d) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/System.bundle/Contents/Code/playerservice.py", line 67, in process_legacy_remote_command result = cmd(**kwargs) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/code/sandbox.py", line 19, in _apply return apply(f, args, kwargs) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/api/playerkit.py", line 395, in f return req.content File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 265, in content return self.__str__() File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 243, in __str__ self.load() File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 181, in load f = self._opener.open(req, timeout=self._timeout) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 438, in open response = self._open(req, data) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 456, in _open '_open', req) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 416, in _call_chain result = func(*args) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 1217, in http_open return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 1190, in do_open r = h.getresponse() File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/httplib_new.py", line 1030, in getresponse response.begin() File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/httplib_new.py", line 431, in begin version, status, reason = self._read_status() File "/usr/lib/plexmediaserver/Resources/Plug-ins-9394c87/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/httplib_new.py", line 419, in _read_status raise BadStatusLine(line) BadStatusLine: HTTP/1.1 0
</Traceback>
</Response>

Roku logs and Plex logs yield no helpful information that I can see. Let me know if you need any other information to help me with this.

Thanks in advance for your help!

Is this the correct forum for a question like this?

I am trying to develop a Google Assistant action for controlling Plex, so I figured Third Party Dev was the best place.

@Jaszzz said:
Is this the correct forum for a question like this?

I am trying to develop a Google Assistant action for controlling Plex, so I figured Third Party Dev was the best place.

Have you had any luck with this? I am trying to do something similar with a Google Assistant action going through to a python script and finally to Chromecast. I can get the media details screen to come up on my chromecast, but I seem to be missing the command to actually play specific media. Not sure if it’s related.

No luck so far…hoping someone else on this forum will respond. I’m hoping Plex didn’t remove the ability to execute the playMedia command.

Bump.

Is there a different forum I should post this in?

Hey, so I ended up having to abandon my chromecast project and peeked back in here. I noticed that the php api you’re using hasn’t been updated in 4 years. I don’t know if there’s a newer one (I haven’t writen in php in almost that long myself. lol.), but I think that was before plex used “playQueues” and I wonder if that’s why you’re having an issue. The python api spits out an address like:

http://{PLAYER_ID}:{PLAYER_PORT}/player/playback/playMedia?key=/library/metadata/{MEDIA_ID}&offset=0&X-Plex-Client-Identifier={CLIENT_ID}&machineIdentifier={SERVER_ID}&address={SERVER_IP}&port={SERVER_PORT}&protocol=http&path=http://{SERVER_IP}:{SERVER_PORT}/library/metadata/{MEDIA_ID}&X-Plex-Token={AUTH_TOKEN}

Quite a different url from what you’re ending up with.