Php 在多个请求中使用cURL多次切换用户代理

Php 在多个请求中使用cURL多次切换用户代理,php,curl,Php,Curl,所以我在这里使用cURL登录到一个网站,并欺骗用户代理。我有两个常量:一个叫做IOS的是IOS用户代理,另一个叫做CHROME的是CHROME用户代理。以下是要登录的代码: public function signIn($username, $password) { $url = "https://www.site.net/post/Index.page"; $cookie = "cookie.txt"; $postdata

所以我在这里使用cURL登录到一个网站,并欺骗用户代理。我有两个常量:一个叫做
IOS
的是IOS用户代理,另一个叫做
CHROME
的是CHROME用户代理。以下是要登录的代码:

    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);
    }
在随后的请求中,我想将用户代理从
IOS
更改为
CHROME
,但它似乎不起作用(我更改为
CHROME
,网站仍然提供移动页面)。当我运行此请求时:

curl-L-xpost-b“cookie.txt”--user-agent.CHROMEhttps://site.net


它提供的不是桌面站点,而是移动站点。登录时是否可以更改用户代理?

网站似乎在登录时将用户代理保存为全局会话变量。在这种情况下,否,在以后的curl请求中更改用户代理不会更改会话变量中的用户代理。

服务器也基于cookie检测您的访问。因此,在函数
signIn()
的第一行,清空cookie文件或删除它

例如:

file_put_contents($cookie, "");
或者删除它,curl将创建一个新的:

unlink($cookie);