Retrieve Android Market mylibrary with curl

后端 未结 1 1134
北恋
北恋 2020-12-21 19:34

I am trying to retrieve this page using curl in php. This page of course requires you to log in because it displays different apps for each user. I have been following the w

相关标签:
1条回答
  • 2020-12-21 20:12

    Here is something I came up with today for this question that has been modified to work for you:

    <?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;
    }
    
    0 讨论(0)
提交回复
热议问题