Aneal Khimani

Launch any file on your PC via your iPhone (with some PHP help)

by Aneal on Nov.17, 2010, under PHP, iPhone

I was reading one of my favorite blogs today and saw a post where some guy was able to rig up some software on his machine that he could launch via his iPhone. I thought this sounded cool to try so I gave it a shot. After a couple hours, I was able to create a custom icon on my iPhone springboard that will launch any file on my machine (executable, mp3, you name it).

I will assume you know PHP, basic HTML and how to work an iPhone.

Here’s how I got it working:
I’m running a Windows XP machine as my primary PC. I installed a WAMP stack (Apache Web Server, MySQL, PHP) first. I’m not going to get into the details of how to do this, but if you Google XAMPP, you’ll be able to have your very own full fledged web server running on your machine. This is super overkill for this project, but I also do some web development in my spare time so I needed this setup anyhow.
Once I had that running, I wrote a simple PHP script to run an external file on my machine. The code is simple:

1
2
3
<?php
    exec('C:\\phonelaunch\\mst.m3u');
?>

This bit of PHP code is calling the built-in function exec(). This function takes a string argument that equals the full path to an executable file (in this case, I’m calling a Winamp Playlist; Mystery Science Theater 3000 via Shoutcast to be exact).

You might notice that the path includes double backslashes as opposed to the actual path which only has single backslashes. This is because of the way that PHP interprets slashes. They basically tell PHP to escape the next character in the string, so since our string includes backslashes, we have to include them twice to escape them.

Save this PHP file as anything you like in your WAMP wwwroot folder and launch it in your browser (http://localhost/your/www/root/path/yourfilename.php). Blamo, you should have launched the file that your script pointed to.

Next up is to create a shortcut on your iPhone to launch this PHP script. Keep in mind that you’ll need to have your phone connected via Wifi to the same network that your PC is on. First you’ll need to determine what the inside IP address of your computer is. If you’re running Windows, you can simply launch the command line and type “ipconfig” and hit enter.
This will show you a brief outline of your computers IP configurations (go figure). At any rate, write down your computers IP address. Crank up your iPhone and launch the Safari browser. Type your PC’s IP address into the address bar followed by “:80/your/www/root/path/yourfilename.php”. In my case it looks like this:

192.168.1.68:80/xampp/Sandbox/mstlaunch.php

Basically you’re telling the iPhone browser to look for a file at IP address XXX.XXX.XXX.XXX, communicate via port 80, and the file location is YYY/YYY/somefile.php. I’m not going to get into the finer details of ports and http access. I’m assuming you know this stuff.

If you’ve played your cards right, Blamo again! Your PC should launch the file you specified in your script above.

Now, click the little “+” icon at the bottom of your Safari browser and then choose “Add to Home Screen”. The iPhone will prompt you for a name for the new shortcut. Click “Add” when you’re done naming it and Double Blamo! You’ll have an icon on your iPhone springboard that will launch this PHP script on your machine in one key press.

Some other considerations…

I suggest that you add a little header data to the PHP script before you create the shortcut on your iPhone. Adding a page title and a favicon link will greatly enhance your shortcut-making adventures. Add a little bit of code just before your PHP script that goes something to this effect:

1
2
3
4
<html>
<head><title>Some Title</title>
<link rel="apple-touch-icon" href="someicon.png">
</head>

If you’ve ever coded HTML you should recognize what’s going on above. We’re simply giving the PHP page a title. Doing so will automatically name the bookmark you save on your iPhone to whatever you put between the title tags.

The link tag is specially suited for your iPhone. It points to an icon file that will become the image icon for the bookmark that exists on your springboard when you create it. Basically it works the same way as a favicon. Just create a 57px X 57px png file, and save it in the same folder as the PHP script. When you create the bookmark on your iPhone, the icon will automatically match this png file. Nifty huh?

Eventually you’re gonna want to be able to have this functionality no matter where you are in the world. This is pretty simple to achieve. All you have to do is access your network router, open port 80 (http port) and forward all traffic on that port to your PC’s IP address (of course you’ll need a static IP on your PC). This way, you can hit the outside IP address and the path to your PHP script and it’ll work via 3G as well (and from anywhere on the internet, using any PC, Mac, Linux etc. machine).

Going forward…

I got me one of them nifty Arduinos. I’ve already interfaced with the real world via PHP and my Arduino (haven’t you read my other posts about the Arduino Webcam?).

I might consider using this same setup to turn on my coffee maker in the morning, turn on/off the lamp on my desk etc… The sky’s the limit!

Hope this helps you make awesome stuff! Hit me up and let me know how you use this.

:, ,

Leave a Reply

Blogroll

A few highly recommended websites...