2012-01-22 94 views
1

我試圖檢索this頁面使用curl在php中。這個頁面當然需要你登錄,因爲它爲每個用戶顯示不同的應用程序。我一直在關注this page所做的工作,但是我沒有取得太大的成功。以curl檢索Android電子市場mylibrary

到目前爲止,在他的例子中,我能夠使用auth令牌成功地填充auth變量。然而,在下一步(登錄Android Market的評論之下),我遇到了麻煩。他所說的輸出變量應該有一個302代碼導致「文檔已移動」頁面,該頁面將我鏈接回Google登錄頁面。

這裏是一個pastebin來顯示我正在嘗試的東西。 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