我正在尝试使用 curl 登录我的一个网站以从页面中提取信息。它似乎不起作用。这是我正在尝试的代码。如果它有帮助,我可以为这种情况创建一个用户/通行证。
<?php
$username = 'xxx';
$password = 'xxx';
$loginUrl = 'http://gwintersdev.com/user';
$finalUrl = 'http://gwintersdev.com/admin';
$userinput = 'name';
$passwordinput = 'pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_URL,$loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$userinput=$username&$passwordinput=$password");
curl_setopt($ch, CURLOPT_USERAGENT, 'user-agent');
ob_start(); // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, $finalUrl);
$buf2 = curl_exec ($ch);
curl_close ($ch);
print $buf2;
?>
更新:我能够让上述工作,但我在不同的 ASP 站点上尝试这个并且它不工作。我抓住了所有隐藏的字段并将它们添加到帖子字符串中,但它仍然无法登录。
<?php
$username = 'xxx';
$password = 'xxx';
$loginUrl = 'http://vitalstim.com/health_professionals/certified_provider_resources/forum.aspx';
$finalUrl = 'http://vitalstim.com/health_professionals/certified_provider_resources/forum.aspx';
$userinput = 'ctl00$ContentPlaceHolder1$uc_login$txtUser';
$passwordinput = 'ctl00$ContentPlaceHolder1$uc_login$txtPass';
$login = 'ctl00$ContentPlaceHolder1$uc_login$butLogin';
$validation_input = '__EVENTVALIDATION';
$validation_input_value = '/wEWAgKf+PTrBQKItpn5BDXHCHsANbEpwkEBmMyNv+32L2Ec';
$view_state = '/wEPDwUJLTQyMjg0NzI0D2QWAmYPZBYGAgEPZBYEAgYPFgIeB1Zpc2libGVoZAIHDxYCHwBoZAIDD2QWBAIBD2QWCAIBD2QWBAIBDw8WAh4EVGV4dGVkZAIFDw8WAh8AaGRkAgcPZBYCAgEPZBYCAgMPZBYCAgEPFgIfAGhkAgkPDxYCHwBoZGQCCw8PFgIfAGhkZAIDDxYCHwBoZAIFDw8WAh8BBXY8c2NyaXB0IGxhbmd1YWdlPSJqYXZhc2NyaXB0IiB0eXBlPSJ0ZXh0L2phdmFzY3JpcHQiPgokKGRvY3VtZW50KS5yZWFkeShmdW5jdGlvbigpIHsKVml0YWxTdGltLkluaXQoNCk7Cn0pOwo8L3NjcmlwdD4KZGRkdz/7+FcQ1E1sbC0Gua3jJsCGSnM=';
$event_valid = '/wEWBwKeiM4xAoi2mfkEAurz/r4MAvTX0jYC+4GopQkCo6iimggC2pO41g77y84VwyhP6Ek+7PGZYDNgOawRZw==';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$userinput=$username&$passwordinput=$password&$validation_input=$validation_input_value&$login=login&__EVENTVALIDATION=$event_valid&_VIEWSTATE=$view_state");
curl_setopt($ch, CURLOPT_USERAGENT, 'user-agent');
curl_exec ($ch); // execute the curl command
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, $finalUrl);
$buf2 = curl_exec ($ch);
curl_close ($ch);
print $buf2;
?>