tags:

views:

241

answers:

1

Hi all

I'm trying to automatically log in to www.meetup.com without much success:-

This is my code:-

      <?
$username="[email protected]";
$password="123abc";
$url="http://meetup.com";
$cookie="cookie.txt";

$postdata = "email=". $username ."&password=". $password . "&submitButton=Login";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . "/login");

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, "http://www.meetup.com/");

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;

exit;
?>

No joy - any ideas?

Thanks

Jonathan

+3  A: 

they may be handing you a session cookie upon first pageload

try making a request with CURLOPT_COOKIEJAR and then make the login request using CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR

also for the hell of it, this is a function i make to make requests look cleaner

function curl_http_request ($url, $options)
{
    $handle = curl_init($url);
    curl_setopt_array($handle, $options);
    ob_start();
    $buffer = curl_exec($handle);
    ob_end_clean();
    curl_close($handle);
    return $buffer;
}

example of use

$options = array(
    CURLOPT_RETURNTRANSFER => TRUE
);

curl_http_request($url, $options);

this should work

// set global curl options
$curloptions = array(
     CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6',
     CURLOPT_TIMEOUT => 60,
     CURLOPT_COOKIEJAR => $cookie,
     CURLOPT_COOKIEFILE => $cookie,
     CURLOPT_REFERER => 'http://www.meetup.com/'
);

// set userinfo
$username = '[email protected]';
$password = '[email protected]';
$cookie = 'cookie.txt';

// clear cookie.txt (fresh session)
$handle = fopen($cookie, 'w');
fclose($handle);

// make a dummy request to generate a session
curl_http_request('http://www.meetup.com/login/', $curloptions);

// login
curl_http_request('http://www.meetup.com/login/', 
    array(
     CURLOPT_POSTFIELDS => 'email=' . urlencode($username) . '&password=' . urlencode($password) . '&rememberme=on&submitButton=Login&returnUri=http%3A%2F%2Fwww.meetup.com%2F&op=login',
     CURLOPT_POST => TRUE
    ), $curloptions
);

//example request
echo curl_http_request('http://www.meetup.com/account/', 
    array(
     CURLOPT_FOLLOWLOCATION => TRUE,
     CURLOPT_RETURNTRANSFER => TRUE
    ), $curloptions
);

function curl_http_request ($url, $moreoptions = array(), $options = array())
{
    foreach ($moreoptions as $k => $v) $options[$k] = $v;
  $handle = curl_init($url);
  curl_setopt_array($handle, $options);
  ob_start();
  $buffer = curl_exec($handle);
  ob_end_clean();
  curl_close($handle);
  return $buffer;
}

hope this one works :)

Chad Scira
Thanks Chad, I tried (at least I think I did it correctly) to add the line you suggested but no joy. I would be very grateful if you can perhaps try it out for yourself with www.meetup.com and let me know if you can get it to work. Thanks so much!
Jonathan Lyon
the above works for me, like i said they use a session. So to successfully login you will need to make a dummy page request with CURLOPT_COOKIEJAR file set to make the site give you session cookies. Then actually post a login. :)
Chad Scira
Hi Chad - awesome!That worked a treat with one smallish issue. when I try and navigate to another page it loses the login.Is there a way to retain the login status? For example I run the script from http/mydomain.com/testlogin.php which is successful and displays the meetup page but if I click on a link it goes to http://meetup.com/link-I-clicked but not with me logged in.Any ideas?I really appreciate the help!
Jonathan Lyon