Trying to parse XML prom PMS remotely with PHP

I'm trying to access the XML api externaly but seem to have come to a problem.

 

$xml_url = "http://EXT_IP:PORT/status/sessions?X-Plex-Token=MYTOKEN";

$content = file_get_content($url);

if (empty($content)) {
die(“XML is empty”);
}

$xml = simplexml_load_string($content);

When I put the $xml_url into my browser it works fine, I can see the XML data for what is being watched on the server.

However the script returns that the XML is empty.

 

Probably something obvious I'm doing wrong. 

 

The end result should be a array with the names of the poeple streaming and whats being watched.

Here's the Perl Script I use. I run it every 5 mins using cron and redirect its output to a log file.

#!/usr/bin/perl -w

use strict;
use warnings;
use Date::Calc qw(check_date Decode_Month Delta_Days English_Ordinal Month_to_Text);
use DateTime;
use LWP;
use POSIX qw(strftime);
use XML::Simple;

binmode(STDOUT, ":utf8");

my $CURDATE;
$CURDATE->{year}  = strftime "%Y", localtime;
$CURDATE->{month} = strftime "%m", localtime;
$CURDATE->{day}   = strftime "%d", localtime;
$CURDATE->{short} = $CURDATE->{year}.'-'.$CURDATE->{month}.'-'.$CURDATE->{day};
$CURDATE->{nice}  = English_Ordinal(int($CURDATE->{day})) . " " . Month_to_Text($CURDATE->{month}, 1) . ' ' . $CURDATE->{year};
my $CURTIME = sprintf(
    "%02d:%02d:%02d",
    (strftime "%H", localtime),
    (strftime "%M", localtime),
    (strftime "%S", localtime));
my $CURUSER = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);

# Newline string, keeps things tidy
my $NL = "
";
my $VERSION = "1.0";
my $obj_lwp = new LWP::UserAgent || &die("Failed to create LWP object" . $NL);
my $obj_xml = new XML::Simple || &die("Failed to create XML object" . $NL);
my $np_raw = $obj_lwp->get("http://10.0.0.4:32400/status/sessions") || &die("Failed to retrieve data from library" . $NL);
my $np_xml = $obj_xml->XMLin($np_raw->content) || &die("Failed to import XML data" . $NL);
undef($np_raw);
&processNowPlaying($np_xml);

sub processNowPlaying() {
	if ($_[0]->{size} > 1 ) { 
		foreach my $key (keys %{$_[0]->{Video}}) {
			foreach my $e ($_[0]->{Video}{$key}) {
				my $user = $e->{User}->{title};
				my $player = $e->{Player}->{title};
				my $platform = $e->{Player}->{platform};
				my $show = $e->{grandparentTitle};
				my $title = $e->{title};
				my $type = $e->{type};
		
				if ($type eq "episode") {
					print $CURDATE->{nice} . " " . $CURTIME . " [PLEX] " . $user . " watching " . $show . " - " . $title . " on " . $player . " (" . $platform . ")" . $NL;
				} else {
					print $CURDATE->{nice} . " " . $CURTIME . " [PLEX] " . $user . " watching " . $title . " on " . $player . " (" . $platform . ")" . $NL;
				}
			}
		}
	} else {
		my $e = $_[0]->{Video};
		if ($e) {
			my $user = $e->{User}->{title};
			my $player = $e->{Player}->{title};
			my $platform = $e->{Player}->{platform};
			my $show = $e->{grandparentTitle};
			my $title = $e->{title};
			my $type = $e->{type};
			if ($type eq "episode") {
				print $CURDATE->{nice} . " " . $CURTIME . " [PLEX] " . $user . " watching " . $show . " - " . $title . " on " . $player . " (" . $platform . ")" . $NL;
			} else {
				print $CURDATE->{nice} . " " . $CURTIME . " [PLEX] " . $user . " watching " . $title . " on " . $player . " (" . $platform . ")" . $NL;
			}
		}
	}
	
}

sub die() {
    # Print an error message then exit with error code 1
    print "ERROR: $_[0]$NL";
    exit 1;
}

If there is any data returned in the call to /status/sessions its gets displayed as follows to standard out:

13th June 2013 18:15:00 [PLEX] DrZeuss watching Captain America on Z's iPad (iOS)

Would this script be able to keep a log and go back over a date period that you either set or does it just report on what is playing with a minute is sees at the moment

cheers

The PHP function name is file_get_contents, with an s at the end.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.