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 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同