2012-04-25 51 views
6

我一直在尝试登录barnesandnoble.com移动网站,使用curl 并且迄今为止还没有运气。我返回页面时没有错误 ,它将我的电子邮件默认再次登录到登录页面的电子邮件输入表单框(从print $ result返回的表单中)。如何使用Curl和SSL以及cookies登录

相同的代码其实可以让我改变LOGINURL指向易趣的登录

,唯一的区别是,barnesandnobles进入易趣正确 为https:// 和易趣的登录为HTTP://

而且,我相信巴恩斯网站是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[] = "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 . "&[email protected]&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; 

?> 
+0

我会CAU对从多个并发请求写入同一个cookie文件非常感兴趣。 – Bruno 2012-04-25 09:31:46

回答

15

下面是我从你的代码中创建一个工作示例。这使用了我为类似问题编写的功能getFormFields,该功能登录Android市场。

我想你的脚本中可能有一些阻止登录工作的事情。首先,您应该在邮件字符串中输入urlencode的URL参数(如电子邮件和密码)(cURL不会为您执行此操作)。其次,我认为可能需要使用作为登录URL一部分的x值。

以下是成功登录的解决方案。请注意,我重新使用了原始的cURL句柄。这不是必需的,但是如果指定保持活动状态,它实际上会重新使用相同的连接,并且还可以避免必须反复指定相同的选项。

一旦你有了cookies,你可以创建一个新的cURL对象并指定COOKIEFILE和COOKIEJAR,并且你将会在没有执行第一步的情况下登录。

<?php 

// options 
$EMAIL   = '[email protected]'; 
$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; 
} 

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

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

+1

谢谢,(非常感谢),工作。 – 2012-04-25 02:23:06

+2

不客气,很高兴帮助。 – drew010 2012-04-25 02:44:43

+0

德鲁,这个答案帮助我得到了我自己的问题的答案,http://stackoverflow.com/questions/16611362/curl-login-into-ebay-co-uk/16614760#16614760我希望你不介意,但我使用了一些代码。 :) – 2013-05-17 17:25:55