【问题标题】:No response with Curl卷曲没有反应
【发布时间】:2012-06-27 02:55:57
【问题描述】:

我正在尝试使用 curl 将我的用户 ID 和密码传递给站点 www.licindia.in,但我可能遇到了 cookie 问题,我无法继续我的会话并且站点响应 302 错误,文档已暂时搬家了,现在我没有收到这段代码的回应:-

<?php
    $username="myusername";
    $password="password";
    $url="http://onlinelic.in/LICEPS/Login/webLogin.do";
    //echo "praveenpuglai";
    $postdata = "portlet_5_6{actionForm.userName}=".$username."&portlet_5_6{actionForm.password}=".$password;
    $cookie = "JSESSIONID";
    ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0");
    curl_setopt($ch,CURLOPT_COOKIESESSION,true);
    curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_REFERER, $url);
    //var_dump($ch);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt ($ch, CURLOPT_POST, 1);
  //  $result = curl_exec ($ch);
    //echo $result;
    /*if($result != null)
    {
    header('Location: http://onlinelic.in/LICEPS/appmanager/Customer/CustomerHome');
    } */
//    curl_close($ch);
    $response = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    if($info['http_code'] == 301 || $info['http_code'] == 302) {
        preg_match_all('|Location: (.*)\n|U', $response, $results);
        $location = implode(';', $results[1]);
        header("Location: $location");
        exit;
    } else {
        $response = substr_replace($response, '', 0, strpos($response, '<', 0));
        echo $response;
    }
?>

【问题讨论】:

    标签: php curl


    【解决方案1】:

    您已通过此声明指定您不愿意遵循任何重定向:

    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
    

    将值设置得更高或不指定会使 cURL 跟随重定向。

    【讨论】:

    • 感谢回复,现在可以登录了,但是还是不能使用账号导致未授权访问,感觉还是cookies没有存储,我用CURLOPT_COOKIEJAR存储了curl生成的cookies cookie.txt 但我不知道为什么它不起作用。
    • 在同一个 cURL 会话中,cookie 已经自动保存;使用 curl_init() 创建新实例时,您只需要一个 cookie jar 文件
    • 嗯...我正在使用名为 cookie.txt 的 cookie jar 文件来存储 cookie 信息,并使用 COOKIEFILE 将该信息发送回服务器,但这不起作用.. ` $cookie = "cookie .txt" curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); `
    • 你为什么有CURLOPT_COOKIESESSION?这将禁止会话 cookie,这可能是您需要的。
    • 您可能正在 cURL 中收集 cookie,但是当您收到 301/302 时,您会将浏览器重定向到该位置...但是当然,该浏览器不会有您收集的 cookie在 cURL 中......所以这永远不会起作用。
    【解决方案2】:
    <?php
        $username="XXXXXX";
        $password="YYYYYY";
        $url="url"; 
        $capt="ZZZZZ"; 
    
    
    
    
        $postdata = "?{actionForm_userName}=".$username."&{actionForm_password}=".$password."&{actionForm_qreply}=".$capt;
        $cookie = "JSESSIONID";
    
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
    
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0");
        curl_setopt($ch,CURLOPT_COOKIESESSION,true);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
    
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt ($ch, CURLOPT_HEADER, 0);   
        curl_setopt ($ch, CURLOPT_REFERER, $url);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt ($ch, CURLOPT_POST, 1); 
    
          //echo $cookie;
          //echo $postdata;
    echo curl_error($ch);
        $result = curl_exec ($ch);
        print($result);
    
    
    curl_close($ch);
    
    
        $response = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if($info['http_code'] == 301 || $info['http_code'] == 302) {
            preg_match_all('|Location: (.*)\n|U', $response, $results);
            $location = implode(';', $results[1]);
            header("Location: $location");
            exit;
        } else {
            $response = substr_replace($response, '', 0, strpos($response, '<', 0));
            echo $response;
        }  
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 1970-01-01
      • 2020-12-01
      • 2013-10-12
      • 2017-07-19
      • 2022-01-26
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多