Controlling iTunes from PHP 5
-
If you are running PHP 5 on Windows, there is support for COM interfaces built right into the PHP core. The COM support in PHP 5 allows you to automate all kinds of software that expose COM interfaces, such as Microsoft Office. Everyone’s favorite music player, iTunes, also exposes a very nice set of COM interfaces that are easily controlled from PHP 5.
To get started, instantiate a new COM client to iTunes. If iTunes is not currently running, it will be opened automatically.
// Instantiate a new COM client to iTunes $iTunes = new COM('iTunes.Application');
This creates an object
$iTuneswhich is a client to theIiTunesinterface, the top-level interface to the iTunes application. From here you can directly access a number of useful methods and properties for controlling iTunes, or you can access the other interfaces that it provides.// Easy Controls $iTunes->Play(); $iTunes->Pause(); $iTunes->Stop(); $iTunes->PreviousTrack(); $iTunes->NextTrack(); $iTunes->SoundVolume = 50;
To monitor what’s currently playing in iTunes, you can use the
CurrentTrackproperty ofIiTunesto return anIITrack.// What's playing now? $currentTrack = $iTunes->CurrentTrack; $trackName = $currentTrack->Name; $trackNumber = $currentTrack->TrackNumber; $albumName = $currentTrack->Album; $artistName = $currentTrack->Artist;
While the capitalization of the methods and properties isn’t the normal way for PHP objects, I’d recommend doing it this way to stay consistent with the naming conventions of the iTunes COM classes. They are actually not case sensitive.
These examples only scratch the surface of what you can do with the iTunes interfaces. To learn more about controlling iTunes with COM, you can get the complete iTunes SDK from Apple which includes documentation in CHM (Windows Help) format.
Note: While these examples look like they should work on PHP 4, they consistently caused my PHP 4.3.10 interpreter to crash. COM support was completely rewritten for PHP 5 and all of the examples here were tested under the CLI version of PHP 5.0.3 without any problems. Please note that these examples will most likely not run under a webserver.

20 comments
comment by Jesse Dyck 24 Jun 05
Mike, your entry is the only thing that I can find on google that has anything to do with controlling iTunes with PHP. Hell, most people I talk to tell me it’s not even possible, but I beleive it is, and won’t quit till I get it to work!
So here is my deal. I just bought an iBook, and I want to be able to control iTunes on my PC (with the 5.1 surround sound) from my iBook. I know a fair bit of PHP so I figured I would tackle this one. I have PHP 5.0.4 and Apache installed on the PC. Web server works great. The problem lies in creating the iTunes COM object. I get the following error:
Fatal error: Uncaught exception ‘com_exception’ with message ‘Failed to create COM object `iTunes.Application’: Server execution failed ‘ in E:\http\test.php:3 Stack trace: #0 E:\http\test.php(3): com->com(‘iTunes.Applicat…’) #1 {main} thrown in E:\http\test.php on line 3
when creating the object as you specified. I spent the last 2 days hunting around the web before resulting to this. Any help would be appreciated.
comment by Ben 29 Jun 05
I too have just tried the Apache2+PHP5+iTunes+COM with no luck on my XP desktop. Someone is offering $20 to get the solution! (http://answers.google.com/answers/threadview?id=528671).
I think the problem is Apache 2, because I can run the script from the commandline.
When I try this script in Firefox while iTunes is running and playing music:
I get this error:
Fatal error: Maximum execution time of 30 seconds exceeded
When I tried the script in Firefox after killing iTunes.exe and iTunesHelper.exe, TaskManager showed that iTunes.exe launches immediately and uses ~21mb of memory but 0% CPU.
I’ve tried giving the Apache service Administrator rights and plain Guest rights, enabling ‘interactions with the desktop’ and nothing has helped.
My “iTunes Music Library.xml” is 14.7MB, and when loading iTunes normally (e.g. interactively), iTunes takes about 30 seconds to load its interface.
COM only works when I run the .php script from the command line with php.exe: C:\Program Files\php>php.exe com-test.php
I’m thinking that in the short term, I will write my COM logic in a seperate file from the display logic, and use system(“C:/Program Files/php/php.exe com-logic.php”); to control iTunes.
comment by Ben 29 Jun 05
Well that didn’t work. The iTunes window will appear in the foreground, but the script hangs.
My Event Viewer shows this DCOM event happening:
Event Type: Error
Event Source: DCOM
Event Category: None
Event ID: 10010
Date: 6/30/2005
Time: 12:28:47 AM
User: ONO-SENDAI\Administrator
Computer: ONO-SENDAI
Description:
The server {DC0C2640-1415-4644-875C-6F4D769839BA} did not register with DCOM within the required timeout.
I’ve given Apache the Administrator rights but that didn’t help.
Searching for DC0C2640-1415-4644-875C-6F4D769839BA didn’t lead to anything fruitful.
I think it’s time to email Apple.
comment by Jesse Dyck 29 Jun 05
That is exactly what I was thinking. I’m going to write my own class that will basically just call php.exe example.php and do different functions.
When I get around to it, Ill even post my code up for the world to see and built upon. By no means am I the best PHP coder around, so I’m sure someone will be able to post some fixes to my code.
Anyways, I’ll update when I’ve got some code underway.
comment by Chris 12 Sep 05
The COM object only works through a command line script.
It won’t work through apache.
And it only works on Windows – which is why it doesn’t work on your iBook Jesse (says this on the http://www.php.net/com site).
comment by Raishad 14 Sep 05
I figured out the problem with the
The server {DC0C2640-1415-4644-875C-6F4D769839BA} did not register with DCOM within the required timeout.
error.
You need to add the IUSR and/or IWAM (one or both, didn’t test) accounts to your Users group.
To do that, go in Control Panel->Administrative Tools->Computer Management->Local Users and Groups->Groups
Right click on Users and select Add to Group, and then add your IUSR_MACHINENAME and IWAM_MACHINENAME accounts
comment by Clint 15 Sep 05
Chris,
COM objects do work through apache, I just did it through apach 1.3.
Ben, COM objects are windows only. One option for controlling iTunes from a mac is to call apple scripts from php. Check out this site: http://www.whatsmyip.org/itunesremote/
comment by James 20 Sep 05
Raishad, can you tell me more about the IUSR_MACHINENAME and IWAM_MACHINENAME accounts? I tried to add them like you said but windows couldn’t find them. I’m using Apache2/php5 on windows xp if it helps. Do I need to create them?
comment by Ray 5 Oct 05
I think something like this needs to happen:
http://www.phpbuilder.com/columns/venkatesan20030501.php3?page=1
Site talks about setting permissions in the php.ini and windows com settings
comment by Ray 6 Oct 05
Apache2.0/PHP5.0.4/iTunes5
UPDATE… I followed the instuctions on the phpbuilder site listed above, with a few alterations…
as I am running Apace, the ISUR and IWAM accounts are not available. I took a leap and added the account EVERYONE to the permissions instead of the ISU/IWAM. Now when I execute a simple PHP script:
$iTunes = new COM(‘iTunes.Application’);
$iTunes->Play();
iTunes becomes the active window and pops to the foreground. However, it fails to play as the script suggests.
comment by Ferenc 10 Oct 05
IUSR_MACHINENAME IWAM_MACHINENAME are IIS accounts set up on install, if you use apache they mean nothing.
basicly you need the itunes COM object to start under the same user apache does.
On my set up I have apache starting on my user account(NON ADMIN) and have the itunes COM service identity set to INTERACTIVE thus allowing all server request logged in on one account. iTunes also runs on my desktop when called.
winXP home
Apache 2
php5
mysql 4
comment by Jesse Dyck 27 Dec 05
Chris: Just wanted to point out that I am setting up this server on a Windows XP computer, but I will be using safari to conrol iTunes (on my PC) from my iBook. I left this issue alone for a while, hoping that there will be some progress (and working on other projects) but recently I have been resurecting the issue. I am going to spend some time on this in the next few days and I’ll be sure to post any findings.
comment by Bryant Mairs 5 Feb 06
I would just like to say that this looks very interesting as an addition to the PHP script I made to control iTunes directly from a webpage and the commandline. My commandline script is available here http://bronson-25.resnet.brown.edu/posts/40/ though it’ll need to be downloaded as the source posted is old.
I also have a web-GUI AJAX version here: http://bronson-25.resnet.brown.edu/itunes/
Unfortunately, the AppleScripts seem to be failing when they run on my computer, so I don’t know what happened there, but it used to do everything that my command-line interface could do. I’m continually working on it, so hopefully I can get it up and running here soon.
comment by Nathan 30 Apr 06
Figured I’d leave a comment; this worked perfectly for me. I’m running Windows XP Home (SP2), PHP 5.1.2, and the OmniHTTPd webserver.
comment by Peter Daniel 2 Jul 06
I have got it working on Windows XP with IIS. It is very important to set the com permissions and php.ini settings correctly.
Download my scripts here: http://www.pjdaniel.org.uk/webtunes/
comment by darri 20 Oct 06
Has anyone succeeded in playing a particular playlist yet (not the LibraryPlaylist)? Been looking for a way to do this without much luck. I’m hoping somebody will set up a wiki with all the PHP methods spelled out in plain english.
comment by Peter Powell 13 Apr 07
tried it.. crashed apache (twice)
comment by Lee 14 Oct 07
Wow, this is just what I was after, now why can’t google return this result more easily!
comment by Jack 28 Nov 07
Is there any way I could get it to play a specific song? Namely the Track id?
I checked the SDK and the most I could make out of it was that I need IITTRACK, but I have no idea how to implement it.
comment by derek 29 Jul 08
I installed php 5.2.6.6 with apache 2.2 and had no luck with COM. It just wouldn’t start up. I messed with the permissions and all that for the itunes object, but after a bit of frustration and no success I gave up on that.
…So I installed the BRS Webweaver server (per the old school engadget instructions) and COM worked perfectly.
Now I’ve just gotta write a fancy little interface.
Post a comment