2011-03-24 37 views
4

我正在編寫一個基於Web的iPhone模擬器,我正在尋找一種方法來欺騙iPhone的Safari瀏覽器,以便在模擬器(iframe)中加載使用移動版本的網頁。根據我的理解,我需要修改用戶代理。創建一個代理來欺騙PHP中的iPhone用戶代理?

我該如何去創建一個PHP代理腳本來欺騙iPhone的用戶代理?

回答

1

您可以使用類似cURL的庫來向iPhone用戶代理請求頁面,並將該頁面返回給您的網站(確保使用DOMDocument將相對URL擴展爲絕對網址)。

但是,您可能遇到CSS/JavaScript /圖像通過用戶代理進行不同服務的邊緣情況。這很可能不值得爲這些資產提出要求。您可以通過向您的用戶代理請求一次,然後iPhone用戶代理請求md5_file(),然後查看它們是否不同,來限制工作。我不會理會,雖然:P

您也可以嘗試這個JavaScript ...

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

navigator.userAgent; // 'foo' 

Source

另外請記住,如果您的用戶沒有使用Safari,它可能會最接近模擬Mobile Safari。

+0

有沒有一種簡單的方法擴大關係我的網址是絕對的,還是遍歷DOM樹並逐個修改URL的問題? – Camsoft 2011-03-25 00:19:51

+0

@Camsoft你可以用'str_replace()'猜測*,但我寧願安全並遍歷元素並替換'href','src'等屬性。 – alex 2011-03-25 00:22:18

0

如果您想爲最終用戶欺騙標頭,則不能使用User Agent更改瀏覽器插件。我的建議是寫一個簡單的PHP腳本,其中傳遞的URL中獲取,並朝着那個URL一個PHP捲曲請求,但事先應設置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'));

我認爲這應該工作,但它會殺了你的服務器在任何時候...

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.