Logging into blogger account using curl - php help needed

msm365

New member
Oct 27, 2008
11
0
0
Hi

Has anybody got a snippet of php code that works for logging into a Google blogger account via Curl.

I cant seem to get mine right.

I can login to the general Google account fine (then it doesnt seem to keep the cookie value when following the blogger link). My best bet would be to login directly, but it does not work via the url: www.blogger.com/start. I suspect it has to do with some hidden input fields in the form on this url.

Any code snippets appreciated

Thanks
 


And heres my code for the blogger direct login
Really think its got to do with the preg match... but any suggestions welcome

Code:
$cookie = tempnam('tmp','COO');
    
    // init CURL
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_VERBOSE => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_COOKIEJAR => $cookie,
        CURLOPT_COOKIEFILE => $cookie,
        CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)',
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_HEADER => false,
        CURLOPT_RETURNTRANSFER => true
    ));

    // go to login page and grab data
    curl_setopt_array($ch, array(
        CURLOPT_URL => "https://www.blogger.com/start",
        CURLOPT_HTTPGET => true,
    ));
    $content = curl_exec($ch);
    //echo $content;
    // get hidden fields on login page
    preg_match_all('|<input type="hidden" name="([^"]*)" value="([^"]*)">|msi',$content,$inputs,PREG_SET_ORDER);
    foreach($inputs as $input) {
        $form_data[$input[1]] = $input[2];
    }
    
        
    $form_data['Email'] = $data['email'];
    $form_data['Passwd'] = $data['pass'];
        
    $login_query = '';
    foreach($form_data as $key => $val) {
        $login_query .= $key.'='.rawurlencode($val).'&';
    }
    echo $login_query;
    
    // CURL: do login
    curl_setopt_array($ch, array(
        CURLOPT_URL => "https://www.blogger.com/start",
        CURLOPT_HTTPGET => false,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $login_query
    ));
    $content = curl_exec($ch);    
    echo $content;

And thats the bugger - doesnt seem to login though!!!
 
Cool start, but PLEASE, I BEG YOU, DO NOT USE REGEX ON HTML, you need to use a parser, and PHP5 comes with one by default. I spent about 2 years on a regex freenode channel and regex on html never really works out.

So the reason your code doesn't work is because sometimes they put the name before the type in input elements. In this example you were missing the CSRF token.


So change this paradigm

Code:
    preg_match_all('|<input type="hidden" name="([^"]*)" value="([^"]*)">|msi',$content,$inputs,PREG_SET_ORDER);
    foreach($inputs as $input) {
        $form_data[$input[1]] = $input[2];

    }

to using php's DOM.

Code:
$doc = new DOMDocument();
$doc->loadHTML($content);
$the_form=$doc->getElementById("start-login-form");

$inputs=$the_form->getElementsByTagName('input');

foreach($inputs as $input){
  $input_name=$input->getAttribute("name");
  $input_value=$input->getAttribute("value");

  if(strlen($input_name) == 0)
    continue;
  $form_data[$input_name]=$input_value;
}

$form_data['Email'] = 'me@mysite.faux';
$form_data['Passwd'] = 'supersecret';


Thanks for posting the code an i hope everyone will stop using regex on html.

If you don't like the DOM* stuff in PHP, but instead like how jQuery does selects $("div.yeah a").html(), check out
PHP Simple HTML DOM Parser it is free and open source and you will feel right at home.