dongqiao3927 2014-03-30 03:48
浏览 40
已采纳

将cURL请求迁移到WGET请求以登录到无法正常工作的网站

I have some code to login to a website via cURL:

    $url = "https://www.site.net/post/login.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, "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53"); 
    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);

I need to be able to change the user agent on the fly, and I can't really do that to my knowledge. So I decided to migrate over to WGET:

shell_exec("wget -qO- --max-redirect=1 --save-cookies=\"cookie.txt\" --referer=\"https://www.site.net/Index.page\" --user-agent=\"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53\" --post-data=\"screenName=$username&kclq=$password&submitEvent=1&TCNK=authenticationEntryComponent&enterClicked=true&ajaxSupported=yes\" https://www.site.net/post/login.page");

However, this isn't even saving cookies to a file. What am I doing wrong/what should I modify?

  • 写回答

1条回答 默认 最新

  • dpp10181 2014-03-30 17:02
    关注

    It can happen that your wget version doesn't support --max-redirect=1 So remove that param from your command and try again. And it should work for you.

    Alternately, the curl commandline:

    curl -L -X POST --cookie-jar "cookie.txt" --referer "https://www.site.net/Index.page" --user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53" --data "screenName=$username&kclq=$password&submitEvent=1&TCNK=authenticationEntryComponent&enterClicked=true&ajaxSupported=yes" http://localhost/post.php
    

    Another option, change the agent from your php script in this way:

    Add the following line at the top of your php script:

    $agent = "Mozilla 6.0";
    if(isset($_GET['agent'])){
        $agent = $_GET['agent'];
    }
    

    And change your curl code for this line CURLOPT_USERAGENT with the following one:

    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    

    Now browse your php script from browser like this way:

    http://yourdomain/code.php?agent=Opera 9.0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集