1

我正在尝试使用 curl 将我的用户名和密码传递给网站 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;
    }
?>
4

2 回答 2

0

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

curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);

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

于 2012-06-27T03:00:10.620 回答
0
<?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;
    }  

?>
于 2015-11-04T11:36:11.043 回答