2012-04-25 3 views
6

나는 barnesandnoble.com 모바일 사이트에 curl 으로 로그인하려고 시도했지만 지금까지 행운이 없었다. 오류없이 페이지를 다시 가져오고, 로그인 페이지의 전자 메일 입력 양식 상자 (print $ result에서 반환 된 양식)로 전자 메일을 다시 채 웁니다. // 및 이베이 로그인이 HTTP했다 : //컬 (Curl)과 SSL 및 쿠키로 로그인하는 방법

같은 코드는 실제로 내가 그 barnesandnobles 인 이베이의 로그인에

유일한 차이점을 가리 키도록 LOGINURL을 변경하여 제대로 이베이에 가자 수있는 것은 HTTPS입니다 또한

, 나는 반즈 웹 사이트 그래서 나는 과거이를 디버깅하는 노력으로 그 다르게

어떤 도움을 주시면 감사하겠습니다 쿠키와 _state을 처리 할 방법 을 모르는 ASP/영문이라고 생각 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 여러 동시 요청으로부터 동일한 쿠키 파일에 작성하는 것에 대해 관심이 있습니다. – Bruno

답변

15

작업이다. 이 기능은 안드로이드 마켓에 로그인하는 유사한 질문 (이 포스트의 맨 아래에있는 첫 번째 참조)을 위해 쓴 기능 getFormFields을 사용합니다.

스크립트에서 로그인이 작동하지 않는 몇 가지 사항이있을 수 있습니다. 첫째, 게시물 문자열에 이메일 및 비밀번호와 같은 URL 매개 변수가 있어야합니다 (cURL은이 작업을 수행하지 않습니다). urlencode 둘째, 로그인 URL의 일부로 사용 된 x 값이 필요할 수 있습니다.

다음은 성공적으로 로그인하는 솔루션입니다. 참고, 원래 cURL 핸들을 다시 사용했습니다. 이것은 필수는 아니지만 keep-alive를 지정하면 실제로 동일한 연결이 다시 사용되며 동일한 옵션을 반복해서 지정하지 않아도됩니다.

쿠키가 있으면 새 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

감사합니다. (많이 감사드립니다). –

+2

도와 주니 기쁘다. – drew010

+0

드류 (Drew),이 대답은 내 자신의 질문에 대한 답변을 얻는 데 도움이되었습니다. http://stackoverflow.com/questions/16611362/curl-login-into-ebay-co-uk/16614760#16614760 하지만 당신 중 일부는 코드를 사용했습니다. :) –

관련 문제