• Willkommen im Geoclub - dem größten deutschsprachigen Geocaching-Forum. Registriere dich kostenlos, um alle Inhalte zu sehen und neue Beiträge zu erstellen.

cURL Login

Status
Für weitere Antworten geschlossen.

Martin1983

Geonewbie
Hi,

hat schon jemand von euch einen cURL Login mit PHP zusammengebracht?

Bei mir scheitert es leider irgendwo... ich dachte ich bin mit diesem Codeteil schon eingeloggt und brauche bei jeder weiteren Abfrage nur das Cookie und den Viewstate mitgeben. Leider funktioniert das nicht so wie gedacht.

Kann mir jemand einen Tipp geben?

Code:
$GCName = "xxx";
$GCPasswort = "xxx";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.geocaching.com/login/default.aspx');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.24');
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
$indexseite = curl_exec($ch);
curl_close($ch);

preg_match('/id="__VIEWSTATE".*?value="(.*?)"/mis', $indexseite, $matches);
$viewstate = $matches[1];

preg_match('/id=\"__EVENTVALIDATION\".*?value=\"(.*?)"/mis', $indexseite, $matches);
$eventvalidation = $matches[1];

$params = array(
	//'__EVENTTARGET' => '',
	//'__EVENTARGUMENT' => '',
	'__EVENTVALIDATION' => $eventvalidation,
	//'__VIEWSTATFIELDCOUNT' => '2',
	'__VIEWSTATE' => $viewstate,
	'ctl00$ContentBody$tbUsername' => $GCName,
	'ctl00$ContentBody$tbPassword' => $GCPasswort,
	//'ctl00$ContentBody$cookie' => 'on',
	//'ctl00$ContentBody$Button1' => 'Login',
	'ctl00$ContentBody$cbRememberMe' => '1',
	'ctl00$ContentBody$btnSignIn' => 'Login'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.geocaching.com/login/default.aspx'); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.24');
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, '', '&')); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
$loginseite = curl_exec($ch); 
curl_close($ch);

echo $loginseite;

lG. Martin
 
OP
M

Martin1983

Geonewbie
Danke,

das habe ich aber leider schon versucht, es hat aber nichts geändert.

Gibt es sonst noch Ideen?
 
OP
M

Martin1983

Geonewbie
Problem gelöst!

"http_build_query" ist in diesem Fall nicht richtig, da Dollarzeichen in dem Array sind, und diese falsch konvertiert werden.

ich habe diese einfach so aneinandergereiht, dann geht es.
Code:
foreach ($params AS $key => $value) {
	$postfields .= $key . '=' . $value . '&';
}
$postfields = rtrim($postfields, '&');

Trotzdem Danke und
lG. Martin
 
Status
Für weitere Antworten geschlossen.
Oben