2012-01-22 12 views
1

私はthisページをPHPでカールを使用して取得しようとしています。このページには、ユーザーごとに異なるアプリが表示されるため、ログインする必要があります。私はthis pageで行われた作業に従っていますが、あまり成功していません。カール付きのAndroidマーケットmylibraryを取得

これまでの例では、auth変数にauthトークンを正常に設定できました。しかし、次のステップでは(Androidマーケットにログインするコメントの下に)私は問題にぶつかります。彼が言う302のコードを出力変数は、私はページにGoogleのログインに私をリンクする "文書が移動しました"ページになります。

私が試していることを正確に示すためのペーストビンです。 http://pastebin.com/9Fs9GWxk さらに、実際に私が必要とするページを手に入れるために、この後に何が必要なのかを誰かが知っていれば、それはすばらしくなるでしょう。おかげ

答えて

1

ここではあなたのために動作するように変更されました私はthis questionのために今日を思い付いたものです:

<?php 

$USERNAME = '[email protected]'; 
$PASSWORD = 'yourpasswd'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR); 
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120); 

curl_setopt($ch, CURLOPT_URL, 
    'https://accounts.google.com/ServiceLogin?hl=en&continue=https://market.android.com/mylibrary'); 
$data = curl_exec($ch); 

$formFields = getFormFields($data); 

$formFields['Email'] = $USERNAME; 
$formFields['Passwd'] = $PASSWORD; 
unset($formFields['PersistentCookie']); 

// var_dump($formFields); 

$post_string = ''; 
foreach($formFields as $key => $value) { 
    $post_string .= $key . '=' . urlencode($value) . '&'; 
} 

$post_string = substr($post_string, 0, -1); 

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 

$result = curl_exec($ch); 
//var_dump($result); 

if (preg_match('/^2\d{2}/', curl_getinfo($ch, CURLINFO_HTTP_CODE)) == false) { 
    die("Login failed"); 
    var_dump(curl_getinfo($ch), $result); 
} else { 
    curl_setopt($ch, CURLOPT_URL, 'https://market.android.com/mylibrary'); 
    curl_setopt($ch, CURLOPT_POST, 0); 
    curl_setopt($ch, CURLOPT_HTTPGET, true); 

    $result = curl_exec($ch); 
    echo $result; 
} 

function getFormFields($data) 
{ 
    if (preg_match('/(<form id=.?gaia_loginform.*?<\/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; 
} 
+0

うわー、完璧な、どうもありがとうございました。 – tgrosinger

関連する問題