2012-04-25 16 views
6

मैं curl के साथ barnesandnoble.com मोबाइल साइट में लॉग इन करने का प्रयास कर रहा हूं और अब तक कोई भाग्य नहीं है। मैं पृष्ठ को वापस में बिना किसी त्रुटि के वापस प्राप्त करता हूं और यह मेरे ईमेल को लॉगिन पृष्ठ के ईमेल इनपुट फॉर्म बॉक्स में फिर से प्रिंट करता है (प्रिंट $ परिणाम से वापस फॉर्म में)। // और ईबे लॉगिन http था: //कर्ल और एसएसएल और कुकीज़ के साथ लॉगिन कैसे करें

एक ही कोड वास्तव में मुझे जा रहा है कि barnesandnobles ईबे के लॉगिन करने के लिए

फर्क सिर्फ इतना है बात करने के लिए LOGINURL बदलकर सही ढंग से ईबे में जाने दे सकते हैं https है

इसके अलावा, मुझे विश्वास है कि बार्न्स वेबसाइट एएसपी/aspx है तो मैं नहीं पता है कि कैसे कुकीज़ और _state अलग ढंग से

किसी भी मदद की सराहना की होगी संभाल के रूप में मैं अतीत के लिए इस डिबग करने के लिए कोशिश कर रहा 16hrs

भी, मेरे 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 एकाधिक समवर्ती अनुरोधों से एक ही कुकी फ़ाइल को लिखने के बारे में चिंतित। – Bruno

उत्तर

15

काम कर रहा है। यह एक फ़ंक्शन getFormFields का उपयोग करता है जिसे मैंने इसी प्रश्न के लिए लिखा था (इस पोस्ट के निचले हिस्से में पहला संदर्भ) जो एंड्रॉइड मार्केट में लॉग इन करता है।

मुझे लगता है कि आपकी स्क्रिप्ट में कुछ चीजें हो सकती हैं जो लॉगिन से काम करने से रोक रही थीं। सबसे पहले, आपको urlencode पोस्ट स्ट्रिंग में ईमेल और पासवर्ड जैसे यूआरएल पैरामीटर चाहिए (curl यह आपके लिए नहीं करेगा)। दूसरा, मुझे लगता है कि x लॉगिन यूआरएल के हिस्से के रूप में इस्तेमाल किया जाने वाला मूल्य आवश्यक हो सकता है।

यहां एक समाधान है जो सफलतापूर्वक लॉग इन करता है। नोट, मैंने मूल कर्ल हैंडल का फिर से उपयोग किया। यह जरूरी नहीं है, लेकिन यदि आप जीवित रहते हैं, तो यह वास्तव में एक ही कनेक्शन का पुन: उपयोग करेगा, और यह आपको वही विकल्प निर्दिष्ट करने से बचाता है।

एक बार आपके पास कुकीज़ हो जाने के बाद, आप एक नया कर्ल ऑब्जेक्ट बना सकते हैं और कुकीआई और कुकीजर निर्दिष्ट कर सकते हैं और आप पहले चरण किए बिना लॉग इन होंगे।

<?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 मुझे आशा है कि आपको कोई फर्क नहीं पड़ता, लेकिन मैंने आपके कुछ कोड का इस्तेमाल किया। :) –

संबंधित मुद्दे