Creating a proxy to replace the iPhone user agent in PHP?

I am writing an iPhone web simulator and I am looking for a way to trick the iPhone Safari browser so that the web pages loaded in the iframe use the mobile versions. In my opinion, I need to change the user agent.

How do I create a PHP script proxy to fake an iPhone user agent?

+4
source share
3 answers

You can use a library such as cURL to request a page using an iPhone user agent and return that page to your site (remember to expand relative URLs to absolute using DOMDocument).

However, you may encounter extreme cases where CSS / JavaScript / images are served differently using a user agent. It is probably not worth it to request each of these assets on time. You can limit the work by requesting one time with your user agent and then with your iPhone user agent by running md5_file() and seeing if they are different. I would not worry: P

You can also try this javascript ...

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

Source

Also remember that you can give a warning if your users are not using Safari, which will be closest to Mobile Safari modeling.

+1
source

You can use a PHP class like Ben Alman Simple PHP Proxy / Github .

This allows you to redirect the cross-domain URL in a variety of ways, including the following way to β€œchange” 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 embed an iFrame - from the iPhone version of google voice - to any page, for example ...

Some of the many other ways that you can modify a query using this script, ...

  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. 
+3
source

If you want to trick the headers for your end users, then using a user agent that modifies the browser add-on is not an option. My suggestion is to write a simple PHP script to which you pass the URL you want to extract and make a PHP cURL request to that URL, but you must set the User Agent header with the following call in advance:

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

0
source

Source: https://habr.com/ru/post/1345170/


All Articles