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条)

报告相同问题?

悬赏问题

  • ¥15 关于模型训练的一个问题
  • ¥15 装了几千台服务器从来没遇到这种问题,哎看下哪位帮我解决吧
  • ¥15 单片机程序上的困难问题
  • ¥15 请教某软件缓存Ts文件破解合并mp4的方法
  • ¥15 求小游戏炸弹人中关于敌人的C++代码
  • ¥15 拿到这个服务器最高权限有偿
  • ¥50 来个抓app跳转支付宝转链接的
  • ¥15 remotes安装提示没有description文件
  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 以下代码,运行结果报错