Php, Exec();, and iTunes.

Moptop

Registered
Im looking for a terminal command that will return the current song / artist playing on my computer's iTunes. I intend to use it with Php.
 
Hm, it works in the terminal (Thanks!) but doesn't work in php..

Im doing - <?php exec("./nowplaying"); ?> and have tried <?php echo exec("./nowplaying"); ?>

(Exec has worked for other commands.)



Edit: Nvm, got it working. :) Had to put the nowplaying file in my sites folder. Thanks!
 
Ok, Im back. It stopped working after I messed with httpd.conf. What could I of screwed up? Everything else in PHP still works... Other commands still work in exe() too...

I had removed some virtual hosts stuff I had added in awhile ago, I even tried using the default httpd.conf, but no luck...
 
Have you altered your php.ini file also? Try having PHP report everything (notices and warnings as well as errors) by setting error_reporting in your php.ini file:

Code:
error_reporting = E_ALL

then restart the web server.

Note that you can also test PHP scripts with the "php" command. Say your script is "run.php", you could do:

Code:
php run.php

You might find it easier to track down errors, warnings etc. this way.
 
No, I haven't altered my php.ini. I checked it now, error reporting is already at that.

I tried that in the terminal, and got this...

39:44: syntax error: Expected &#8220;then&#8221;, etc. but found identifier. (-2741)

But my php script doesn't have nearly 39 lines? Could it be referring to the other thingy (nowplaying) ?
Edit: Never mind about that, I didn't open itunes. (the nowplaying script requires that.)


So, its working fine in the terminal, but not apache? Does the terminal php use mac's default php? Because I have php5.2.2 from Here.
 
So, its working fine in the terminal, but not apache? Does the terminal php use mac's default php? Because I have php5.2.2 from Here.

Yes, /usr/bin/php is the default PHP. You'd have to invoke php from the command line as follows:

Code:
/usr/local/php5/bin/php somescript.php
 
Do you want to post the "nowplaying" script, since if other commands work with exec, this might well be the culprit?
 
Its located in /bin/ (Called by exec("nowplaying"); , and is in the same folder as the php file, ( called by exec("./nowplaying"); ), I've tried both ways, both only work in the terminal..-

#! /bin/bash
osascript -e 'tell application "iTunes" to if player state is playing then "Now Playing: " & name of current track & " - " & artist of current track'
 
There's not anything wrong with it. It works fine in the terminal. We're trying to figure out why it won't work in php's exec.
 
Open two terminal windows. In one:

sudo tail -f /var/log/httpd/access_log

In the other:

sudo tail -f /var/log/httpd/error_log

Now connect to your web server to view the PHP file, and see what (if any) errors or messages are reported.
 
The error log outputs...

INIT_Processeses(), could not establish the default connection to the WindowServer../nowplaying: line 2: 1692 Abort trap osascript -e 'tell application "iTunes" to if player state is playing then "Now Playing: " & name of current track & " - " & artist of current track'


And the other log-

127.0.0.1 - - [03/Aug/2007:13:10:49 -0400] "GET /itunes.php HTTP/1.1" 200 5
 
Hmmm, maybe the osascript will only work if it's run by the user who "owns" the current login session... Try this to test (note this is unsecure so it's best to put things back the way they were once the test is done):

(1) Stop the web server
(2) Edit /etc/httpd/httpd.conf and set the User and Group to your account name and group name (the command "id" in the terminal will give you this)
(3) Start up the webserver and try the php script again.
 
Back
Top