I'm writing a web based iPhone simulator and I'm looking for a way to spoof iPhone's Safari browser so that web pages loaded within the simulator (iframe) to use the mobile versions. From my understanding I need to modify the user-agent.
How do I go about creating a PHP proxy script to spoof the iPhone's user-agent?
You could use a PHP class, such as Ben Alman's Simple PHP Proxy / Github.
It let's you redirect cross-domain URL's in a variety of ways, including the following method to "change up" your User Agent....
I use it to insert an iFrame - with the iPhone version of google voice - into any page, for example...
Some of the many other ways you can alter the request by using this script are...
If you would like to spoof headers for your end users, then using a User Agent altering browser add-on is not an option. My suggestion is to write a simple PHP script to which you pass the URL to be fetched, and make a PHP cURL request towards that URL, but beforehand you should set the User Agent header with the following call:
curl_setopt($ch,CURLOPT_HTTPHEADER,array('User-Agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3'));
I think this should work, but it will kill your server in no time...
You could use a library like cURL to request the page with the iPhone user agent, and return that page to your site (be sure to expand relative URLs to absolute, with DOMDocument).
However, you may run into edge cases where CSS/JavaScript/images are served differently via the user agent. This is probably not worth requesting each of these assets on the off chance. You could limit the work by requesting once with your user agent, and then the iPhone user agent, doing
md5_file()
and seeing if they are different. I wouldn't bother though :PYou could also try this JavaScript...
Source.
Also remember you may want to give a warning if your users aren't using Safari, which will be the closest to simulate Mobile Safari.