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 ios可以实现ymodem-1k协议 1024字节传输吗?
  • ¥300 寻抓云闪付tn组成网页付款链接
  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)