使用curl检索Android Market mylibrary

时间:2012-01-22 02:33:18

标签: php curl

我正在尝试使用php中的curl检索this页面。此页面当然要求您登录,因为它为每个用户显示不同的应用程序。我一直在关注this page所做的工作,但是没有取得多大成功。

到目前为止,在他的示例中,我能够使用auth令牌成功填充auth变量。然而,在下一步(登录Android Market的评论下方)我遇到了麻烦。他说的输出变量应该有302代码会导致“文档已移动”页面将我链接回Google登录页面。

这是一个用于显示我正在尝试的内容的pastebin。 http://pastebin.com/9Fs9GWxk 此外,如果有人知道我需要做什么步骤,以实际获得我需要的页面,这将是惊人的。感谢

1 个答案:

答案 0 :(得分:1)

以下是我今天为this question提出的修改后适用于您的内容:

<?php

$USERNAME = 'you@gmail';
$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;
}