2012-04-25 11 views
6

私はbarnesandnoble.comモバイルサイトにカールでログインしようとしていました これまでの運がなかった。 のエラーが発生したページが返され、ログインページの電子メール入力フォームボックスに電子メールが再度出力されます(print $ resultから返された形式)。 //カールとSSLとクッキーでログインする方法

:// とeBayのログインのhttpた:

同じコードが実際に唯一の違いはbarnesandnoblesがhttpsではということで

eBayのログインを指すように私はLOGINURLを変更することで、正しく のeBayに手放すことができます

また、私はバーンズのウェブサイトは、私はそれは私が 過去のためにこれをデバッグしようとしてと任意の助けをいただければ幸いクッキーと_state異なっ

を処理する方法を を知らないASP/ASPXであると信じています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

答えて

15

作業であることは、私はあなたのコードから作成された実施例です。これは、Androidマーケットにログインする同様の質問(この記事の一番下にある最初の参照)で書いた関数getFormFieldsを使用しています。

あなたのスクリプトには、ログインが正常に動作しないことがありました。まず、ポストストリングの電子メールやパスワードなどのURLパラメータをurlencodeに設定する必要があります(cURLはこれを行いません)。次に、ログインURLの一部として使用されるxの値が必要になると思います。

ここでは、正常にログインするためのソリューションです。注意、元のcURLハンドルを再利用しました。これは必須ではありませんが、キープアライブを指定すると、実際には同じ接続が再利用され、同じオプションを何度も指定する必要がなくなります。

クッキーを取得したら、新しい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; 
} 

これは私のために働いてくれました。ここで

が、私はそれが学ぶのを助けることが持っているいくつかの他のcURLの答えは以下のとおりです。

+1

ありがとう、(多くの感謝)働いた。 –

+2

あなたが大歓迎です、うれしかったです。 – drew010

+0

ドリュー、この回答は、私自身の質問への回答を得るのに役立ちました。http://stackoverflow.com/questions/16611362/curl-login-into-ebay-co-uk/16614760#16614760私はあなたが気にしないことを願っています。私はあなたのコードのいくつかを使用しました。 :) –

関連する問題