Creating a proxy to spoof iPhone user-agent in PHP?

后端 未结 3 1364
别跟我提以往
别跟我提以往 2020-12-17 04:52

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 mobi

相关标签:
3条回答
  • 2020-12-17 05:24

    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...

    0 讨论(0)
  • 2020-12-17 05:34

    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 :P

    You could also try this JavaScript...

    navigator.__defineGetter__('userAgent', function(){
        return 'foo' // customized user agent
    });
    
    navigator.userAgent; // 'foo'
    

    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.

    0 讨论(0)
  • 2020-12-17 05:39

    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....

    user_agent - This value will be sent to the remote URL request as the
         `User-Agent:` HTTP request header. If omitted, the browser user agent
         will be passed through.
    

    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...

       url - The remote URL resource to fetch. Any GET parameters to be passed
         through to the remote URL resource must be urlencoded in this parameter.
       mode - If mode=native, the response will be sent using the same content
         type and headers that the remote URL resource returned. If omitted, the
         response will be JSON (or JSONP). <Native requests> and <JSONP requests>
         are disabled by default, see <Configuration Options> for more information.
       callback - If specified, the response JSON will be wrapped in this named
         function call. This parameter and <JSONP requests> are disabled by
         default, see <Configuration Options> for more information.
       send_cookies - If send_cookies=1, all cookies will be forwarded through to
         the remote URL request.
       send_session - If send_session=1 and send_cookies=1, the SID cookie will be
         forwarded through to the remote URL request.
       full_headers - If a JSON request and full_headers=1, the JSON response will
         contain detailed header information.
       full_status - If a JSON request and full_status=1, the JSON response will
         contain detailed cURL status information, otherwise it will just contain
         the `http_code` property.
    
    0 讨论(0)
提交回复
热议问题