[CODE] PHP Rest parsing for forum signatures!

Tutorials, tips and tricks.

Moderator: moderators

[CODE] PHP Rest parsing for forum signatures!

Postby lucron » Fri Sep 24, 2010 9:13 pm

The idea is to create a dynamic forum signature showing currently playing information provided by Subsonic through the awesome REST API :)

Since this was requested in a thread a while back and nobody has gotten around to making a tutorial, I thought I'd post my code with some comments included. If you have any questions, post here :)

Example (not live):
Image

Code: Select all
<?

//Subsonic Account Information
$username = "admin";
$password = "";

//Path to your Subsonic
$subsonicpath = "http://www.domain.com/subsonic";

//Path to your signature image
$signaturepath = "images/signature.jpg";

//Font settings
$size = 4;
$x = 5; //X Position
$y = 97; //Y Position
$color = imagecolorallocate($img,255,255,255);

//Retrieve the XML output from REST
$xmlstr = simplexml_load_file("$subsonicpath/rest/getNowPlaying.view?u=$username&p=$password&v=1.3.0&c=forumsig");

//Retrieve Now Playing Information
$artist = $xmlstr->nowPlaying->entry[artist];
$title = $xmlstr->nowPlaying->entry[title];
$year = $xmlstr->nowPlaying->entry[year];
$bitrate = $xmlstr->nowPlaying->entry[bitRate];
$minutesago = $xmlstr->nowPlaying->entry[minutesAgo];

//Change header to jpeg, so browsers know that this is an image
header('Content-Type: image/jpeg');


$img = @imagecreatefromjpeg($signaturepath);


if ($artist && $title) //Check if a song is playing or not, if not, don't write anything!
   imagestring($img, $size,$x,$y, "Subsonic: $artist - $title ($minutesago min. ago)",$color);

imagejpeg($img);
imagedestroy($img);

?>
User avatar
lucron
 
Posts: 1
Joined: Fri Sep 24, 2010 8:51 pm

Postby peher » Wed Feb 02, 2011 5:20 pm

thanks !
very useful !
peher
 
Posts: 9
Joined: Thu Oct 11, 2007 9:34 am

Postby donpearson » Wed Feb 23, 2011 3:51 pm

One thing to think of with this code is that it will just show the top player in the xml file, it will show what your playing if your the only one playing music or you was the first to start playing music.

here is the one im working on atm: http://forum.subsonic.org/forum/viewtop ... 2056#22056
Facebook Group:
http://www.facebook.com/groups/subsonic.group/

Image
donpearson
 
Posts: 361
Joined: Wed Oct 15, 2008 3:53 pm
Location: UK


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 10 guests