创建代理以欺骗PHP中的iPhone用户代理?

时间:2011-03-24 23:36:22

标签: php iphone proxy user-agent

我正在编写一个基于网络的iPhone模拟器,我正在寻找一种方法来欺骗iPhone的Safari浏览器,以便在模拟器(iframe)中加载的网页使用移动版本。根据我的理解,我需要修改用户代理。

如何创建PHP代理脚本以欺骗iPhone的用户代理?

3 个答案:

答案 0 :(得分:3)

您可以使用PHP类,例如Ben Alman's Simple PHP Proxy / Github

让您以多种方式重定向跨域网址,包括以下方法来“更改”您的用户代理....

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.

我用它将iFrame(带有谷歌语音的iPhone版本)插入任何页面,例如......

使用此脚本可以改变请求的其他一些方法是......

   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.

答案 1 :(得分:1)

您可以使用像cURL这样的库来请求具有iPhone用户代理的页面,并将该页面返回到您的站点(确保将相对URL扩展为绝对,使用DOMDocument)。

但是,您可能遇到通过用户代理以不同方式提供CSS / JavaScript /图像的边缘情况。这可能不值得为这些机会申请这些资产。您可以通过向用户代理请求一次,然后向iPhone用户代理请求md5_file()并查看它们是否不同来限制工作。我不会打扰:P

你也可以尝试这个JavaScript ......

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

navigator.userAgent; // 'foo'

Source

另外请记住,如果您的用户没有使用Safari,则可能需要发出警告,而Safari最接近模拟Mobile Safari。

答案 2 :(得分:0)

如果您想为最终用户欺骗标题,则无法使用用户代理更改浏览器加载项。我的建议是编写一个简单的PHP脚本,您将要传递的URL传递给该脚本,并对该URL发出PHP cURL请求,但事先应该使用以下调用设置User Agent标头:

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'));

我认为这应该可行,但它会立刻杀死你的服务器......

相关问题