2014-04-03 24 views
0

因此,我使用cURL在此處登錄到網站並欺騙用戶代理。我有兩個常量:一個叫做IOS,它是一個iOS用戶代理,另一個叫做CHROME,它是一個Chrome用戶代理。下面是登錄代碼:使用cURL在多個請求中多次切換用戶代理

public function signIn($username, $password) 
    { 
     $url = "https://www.site.net/post/Index.page"; 
     $cookie = "cookie.txt"; 

     $postdata = "screenName=$username&kclq=$password&submitEvent=1&TCNK=authenticationEntryComponent&enterClicked=true&ajaxSupported=yes"; 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
     curl_setopt($ch, CURLOPT_USERAGENT, IOS); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 200); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_COOKIE, 1); 
     curl_setopt($ch, CURLOPT_COOKIESESSION, 1); 
     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
     curl_setopt($ch, CURLOPT_REFERER, "https://www.site.net/Index.page"); 

     curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     $result = curl_exec($ch); 

     curl_close($ch); 
    } 

在隨後的請求,我想用戶代理更改從IOSCHROME,它似乎並不工作(我更改爲CHROME和網站仍然提供打開手機頁面)。當我運行這個請求:

curl -L -X POST -b "cookie.txt" --user-agent " . CHROME . " https://site.net

它不會成爲一個桌面網站,但移動一個。登錄時是否可以更改用戶代理?

回答

1

服務器正在檢測您的基於cookie的訪問。因此,在函數signIn()的第一行中,清空cookie文件或刪除它。

例如:

file_put_contents($cookie, ""); 

或刪除它,捲曲將創建一個新的:

unlink($cookie); 
0

該網站似乎是在登錄時將用戶代理程序保存爲全局會話變量。在這種情況下,在稍後的curl請求中更改用戶代理程序不會更改會話變量中的用戶代理程序。