如何用Curl和SSL和Cookie登录 [英] How to login in with Curl and SSL and cookies

查看:213
本文介绍了如何用Curl和SSL和Cookie登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图登录到barnesandnoble.com移动网站curl
和迄今为止没有运气。我回到页面没有错误
,它默认我的电子邮件进入登录页面的电子邮件输入表单框(以从print $ result返回的形式)。



同样的代码实际上可以让我正确地进入ebay
通过改变LOGINURL指向ebay的登录



唯一的区别是barnesandnobles是https://
和ebay login是http://



此外,我相信barnes网站是asp / aspx,所以我不知道
如何处理cookie和_state不同



任何帮助将不胜感激,因为我一直试图调试这个
过去16小时



也是,我的cookie.txt是可写的和工作的

 <?php 

$ cookie_file_path =C:/test/cookie.txt;
$ LOGINURL =https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn;

$ agent =Nokia-Communicator-WWW-Browser / 2.0(Geos 3.0 Nokia-9000i);

$ ch = curl_init();

$ headers [] =Accept:* / *;
$ headers [] =连接:Keep-Alive;
$ headers [] =Content-type:application / x-www-form-urlencoded; charset = UTF-8;


curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
curl_setopt($ ch,CURLOPT_HEADER,0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($ ch,CURLOPT_URL,$ LOGINURL);
curl_setopt($ ch,CURLOPT_USERAGENT,$ agent);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ ch,CURLOPT_COOKIEFILE,$ cookie_file_path);
curl_setopt($ ch,CURLOPT_COOKIEJAR,$ cookie_file_path);

$ content = curl_exec($ ch);

curl_close($ ch);

unset($ ch);




// NAME =path_statevalue =6657403>

if(stristr($ content,path_state)){
$ array1 = explode('path_statevalue =',$ content);
$ content1 = $ array1 [1];
$ array2 = explode('>',$ content1);
$ content2 = $ array2 [0];
}


b



$ b $ LOGINURL =https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn;

$ POSTFIELDS =d_hidPageStamp = V_3_17& hidViewMode = opSignIn& stage = signIn& previousStage = mainStage& path_state =。$ content2。& emailAddress=YOUREMAILHERE@gmail.com& acctPassword = YOURPASSWORD;

$ reffer =https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn;

$ ch = curl_init();


$ headers [] =Accept:* / *;
$ headers [] =Connection:Keep-Alive;
$ headers [] =Content-type:application / x-www-form-urlencoded; charset = UTF-8;


curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
curl_setopt($ ch,CURLOPT_HEADER, 0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);



curl_setopt ch,CURLOPT_URL,$ LOGINURL);
curl_setopt($ ch,CURLOPT_USERAGENT,$ agent);

curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ POSTFIELDS);

curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ ch,CURLOPT_REFERER,$ reffer);
curl_setopt($ ch,CURLOPT_COOKIEFILE,$ cookie_file_path);
curl_setopt($ ch,CURLOPT_COOKIEJAR,$ cookie_file_path);

$ result = curl_exec($ ch);

print $ result;

?>


解决方案

这是一个从代码创建的工作示例。这使用了一个函数 getFormFields ,我写了一个类似的问题(这篇文章的底部的第一个参考),它登录到Android市场。



我认为你的脚本中可能有一些东西阻止了登录工作。首先,您应该在发布字符串中 urlencode 网址参数(如电子邮件和密码)(cURL不会为您执行此操作)。第二,我认为可能需要使用 x 值作为登录URL的一部分。



登录成功。注意,我重用了原来的cURL句柄。这不是必须的,但是如果你指定keep-alive,它实际上将重用同一个连接,并且也不必重复指定相同的选项。



一旦你有cookie,你可以创建一个新的cURL对象,并指定COOKIEFILE和COOKIEJAR,你将不需要执行第一步就登录。

 <?php 

// options
$ EMAIL ='you@yoursite.com';
$ PASSWORD ='yourpassword';
$ cookie_file_path =/tmp/cookies.txt;
$ LOGINURL =https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn;
$ agent =Nokia-Communicator-WWW-Browser / 2.0(Geos 3.0 Nokia-9000i);


//开始脚本
$ ch = curl_init();

//额外标头
$ headers [] =Accept:* / *;
$ headers [] =连接:Keep-Alive;

//所有请求的基本curl选项
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
curl_setopt($ ch,CURLOPT_HEADER,0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ ch,CURLOPT_USERAGENT,$ agent);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ ch,CURLOPT_COOKIEFILE,$ cookie_file_path);
curl_setopt($ ch,CURLOPT_COOKIEJAR,$ cookie_file_path);

//设置第一个URL
curl_setopt($ ch,CURLOPT_URL,$ LOGINURL);

//执行会话以获取Cookie和所需的表单输入
$ content = curl_exec($ ch);

//从登录所需的表单中获取隐藏的输入
$ fields = getFormFields($ content);
$ fields ['emailAddress'] = $ EMAIL;
$ fields ['acctPassword'] = $ PASSWORD;

//获取在登录url中使用的x值
$ x ='';
if(preg_match('/ op\.asp\?x =(\d +)/ i',$ content,$ match)){
$ x = $ match [1]
}

// $ LOGINURL =https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn;
$ LOGINURL =https://cart2.barnesandnoble.com/mobileacct/op.asp?x=$x;

//使用我们从表单中提取的postfields设置
$ POSTFIELDS = http_build_query($ fields);

//将URL改为登录URL
curl_setopt($ ch,CURLOPT_URL,$ LOGINURL);

//设置post选项
curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ POSTFIELDS);

//执行login
$ result = curl_exec($ ch);

print $ result;


function getFormFields($ data)
{
if(preg_match('/(< form action =op。*?< \ / form> ;)/ is',$ data,$ matches)){
$ inputs = getInputs($ matches [1]);

return $ inputs;
} else {
die('didnt find login form');
}
}

function getInputs($ form)
{
$ inputs = array ();

$ elements = preg_match_all('/(< input [^>] +>)/ is',$ form,$ matches);

if($ elements> 0){
for($ i = 0; $ i< $ elements; $ i ++){
$ el = preg_replace('/ \s {2,} / ','',$ matches [1] [$ i]);

if(preg_match('/ name =(?:[\' \s] *)/ i',$ el,$ name)){
$ name = $ name [1];
$ value ='';

if (preg_match('/ value =(?:[\'])?([^\'\s] *)/ i',$ el,$ value)){
$ value = $ value [1];
}

$ inputs [$ name] = $ value;
}
}
}

return $ inputs;
}

这对我有用,希望能帮助你。

这里有一些我可以帮助学习的其他cURL答案:




I been trying to log into barnesandnoble.com mobile site with curl and have had no luck so far. I get back the page with no errors and it defaults my email into the email input form box of the login page again (in the form returned from print $result).

The same code can actually let me go into ebay correctly by changing the LOGINURL to point to ebay's login

The only difference being that barnesandnobles is https:// and ebay login was http://

Also, I believe that barnes website is asp/aspx so I don't know how that would handle cookies and _state differently

Any help would be appreciated as I been trying to debug this for the past 16hrs

also, my cookie.txt is writable and working

    <?php

$cookie_file_path   = "C:/test/cookie.txt";
$LOGINURL       = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; 

$agent          = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";

$ch = curl_init(); 

$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8";


curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt($ch, CURLOPT_HEADER,  0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        

    curl_setopt($ch, CURLOPT_URL, $LOGINURL); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 

    $content = curl_exec($ch); 

    curl_close($ch); 

    unset($ch); 




//     NAME="path_state" value="6657403">

 if(stristr($content,"path_state")){
        $array1=explode('path_state" value="',$content);
        $content1=$array1[1];
        $array2=explode('">',$content1);
        $content2=$array2[0];
        }







$LOGINURL   = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; 

$POSTFIELDS     = "d_hidPageStamp=V_3_17&hidViewMode=opSignIn&stage=signIn&previousStage=mainStage&path_state=" .  $content2 . "&emailAddress=YOUREMAILHERE@gmail.com&acctPassword=YOURPASSWORD";

$reffer     = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; 

$ch = curl_init(); 


$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8";


curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt($ch, CURLOPT_HEADER,  0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   



    curl_setopt($ch, CURLOPT_URL, $LOGINURL); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 

    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_REFERER, $reffer); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 

    $result = curl_exec($ch);  

    print $result; 

?>

解决方案

Here is a working example I created from your code. This uses a function getFormFields that I wrote for a similar question (first reference at the bottom of this post) which logs into the Android market.

I think there may have been a couple of things in your script that were preventing the login from working. First, you should urlencode the URL parameters such as email and password in the post string (cURL will not do this for you). Second, I think the x value used as part of the login URL may be required.

Here is a solution that logs in successfully. Note, I re-used the original cURL handle. This is not necessary, but if you specify keep-alive, it will actually re-use the same connection, and it also saves you from having to specify the same options over and over.

Once you have the cookies, you can create a new cURL object and specify the COOKIEFILE and COOKIEJAR and you will be logged in without performing the first steps.

<?php

// options
$EMAIL            = 'you@yoursite.com';
$PASSWORD         = 'yourpassword';
$cookie_file_path = "/tmp/cookies.txt";
$LOGINURL         = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; 
$agent            = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";


// begin script
$ch = curl_init(); 

// extra headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";

// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt($ch, CURLOPT_HEADER,  0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);         
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 

// set first URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);

// execute session to get cookies and required form inputs
$content = curl_exec($ch); 

// grab the hidden inputs from the form required to login
$fields = getFormFields($content);
$fields['emailAddress'] = $EMAIL;
$fields['acctPassword'] = $PASSWORD;

// get x value that is used in the login url
$x = '';
if (preg_match('/op\.asp\?x=(\d+)/i', $content, $match)) {
    $x = $match[1];
}

//$LOGINURL   = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";
  $LOGINURL   = "https://cart2.barnesandnoble.com/mobileacct/op.asp?x=$x";

// set postfields using what we extracted from the form
$POSTFIELDS = http_build_query($fields); 

// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL); 

// set post options
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS); 

// perform login
$result = curl_exec($ch);  

print $result; 


function getFormFields($data)
{
    if (preg_match('/(<form action="op.*?<\/form>)/is', $data, $matches)) {
        $inputs = getInputs($matches[1]);

        return $inputs;
    } else {
        die('didnt find login form');
    }
}

function getInputs($form)
{
    $inputs = array();

    $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);

    if ($elements > 0) {
        for($i = 0; $i < $elements; $i++) {
            $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);

            if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
                $name  = $name[1];
                $value = '';

                if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
                    $value = $value[1];
                }

                $inputs[$name] = $value;
            }
        }
    }

    return $inputs;
}

This worked for me, hope that helps get you going.

Here are some other cURL answer I have that may help learn:

这篇关于如何用Curl和SSL和Cookie登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆