dongyanjing5975 2014-04-03 13:05
浏览 76
已采纳

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

So I'm using cURL to log into a website here, and spoofing the user agent. I have two constants: one called IOS that is an iOS user agent, and one called CHROME that is a Chrome user agent. Here is the code to log in:

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

In subsequent requests, I would like to change the user agent from IOS to CHROME, and it doesn't seem to work (I change to CHROME and the website still serves up a mobile page). When I run this request:

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

It does not serve up a desktop site, but a mobile one. Is it possible to change the user agent while logged in?

  • 写回答

2条回答 默认 最新

  • dongyi1429 2014-04-03 17:37
    关注

    The server is detecting your visit based on cookie also. So at the first line inside the function signIn() either empty the cookie file, or delete it.

    For example:

    file_put_contents($cookie, "");
    

    or delete it, curl will create a new one:

    unlink($cookie);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 eclipse连接sap后代码跑出来空白
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi