Я довольно новичок в PHP, но для практики я хотел написать скрипт, использующий curl для регистрации, и отправить запрос на пост на сайт, написанный на asp. Я использовал библиотеки libcurl и simple_html_dom.php.
Иногда я получаю сообщение об ошибке для недопустимого состояния представления, это трассировка стека:
[ViewStateException: Invalid viewstate.
Client IP: 111.11.11.111
Port: 4743
User-Agent: Mozilla/5.0
ViewState: /wEPDwUKLTk1OTAzODYwMA9kFgJmD2QWBAIBD2QWAgIBD2QWCmYPFgIeBFRleHQFRTxtZXRhIHByb3BlcnR5PSJvZzp0aXRsZSIgY29udGVudD0iQURJREFTIEVRVCBSVU5OSU5HIENVU0hJT04gOTEiIC8+IGQCAQ8WAh8ABWY8bWV0YSBwcm9wZXJ0eT0ib2c6aW1hZ2UiIGNvbnRlbnQ9Imh0dHA6Ly9zaG9wLnVyYmFuanVuZ2xlc3RvcmUuaXQvZm90b19hcnRpY29saS9kNjc1NjgtaW50cm8uanBnIiAvPiBkAgIPFgIfAAWVBDxtZXRhIHByb3BlcnR5PSJvZzpkZXNjcmlwdGlvbiIgY29udGVudD0iUmVjZW50ZW1lbnRlLCBvbHRyZSBhbGxlIG1pdGljaGUgU3RhbiBTbWl0aCwgc29ubyB0b3JuYXRlIGluIGF1Z2UgZGVpIHBlenppIGRlbGxhIHNlcmllIE9kZGl0eS4gTmVsIDIwMDUgYWRpZGFzIGhhIGxhbmNpYXRvIHVuYSBjb2xsZXppb25lIHNwZWNpYWxlIGRpIHJ1bm5lcnMgY29uIGFsY3VuaSBkZWkgY29sb3JpIHBpJnVncmF2ZTsgcGF6emkgY2hlIHNpIHBvc3Nhbm8gbWV0dGVyZSBzdSB1bmEgc25lYWtlci4gRSBtZW50cmUgcXVlc3RvIG1vZGVsbG8gZGkgYWRpZGFzIEVRVCBSdW5uaW5nIEN1c2hpb24gbm9uIGZhIHVmZmljaWFsbWVudGUgcGFydGUgZGVsIHJpdG9ybm8gZGVsbGEgY29sbGV6aW9uZSBPZGRpdHkgKG8gZm9yc2UgcyZpZ3JhdmU7PyksIGx...]
[HttpException (0x80004005): Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
http://go.microsoft.com/fwlink/?LinkID=314055]
System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +153
System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState) +14
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +237
System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
System.Web.UI.HiddenFieldPageStatePersister.Load() +207
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +199
System.Web.UI.Page.LoadAllState() +43
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
System.Web.UI.Page.ProcessRequest() +80
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.web_articolo_aspx.ProcessRequest(HttpContext context) +37
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
Вот мой код:
<?php
include('simple_html_dom.php');
//method for logging
function login($url,$data){
$headers = array(
'Host' => 'site',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' => 'en-US;q=0.5,en;q=0.3',
'Accept-Encoding' => 'gzip, deflate',
'Referer' => 'http://somesite.com',
'Connection' => 'keep-alive',
'Content-Type' => 'application/x-www-form-urlencoded'
);
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookie.txt");
curl_setopt($login, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookie.txt");
curl_setopt($login, CURLOPT_TIMEOUT, 4000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_HEADER, 1);
curl_setopt($login, CURLOPT_HTTPHEADER, $headers);
curl_setopt($login, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0');
curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($login, CURLOPT_POST, 1);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
//$verbose = curl_setopt($login, CURLOPT_VERBOSE, true);
$ris = curl_exec ($login);
curl_close($login);
unset($login);
return $ris;
}
//method for posting data
function post_data($site,$data){
$datapost = curl_init();
curl_setopt($datapost, CURLOPT_URL, $site);
curl_setopt($datapost, CURLOPT_TIMEOUT, 4000);
curl_setopt($datapost, CURLOPT_HEADER, 1);
//curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers);
curl_setopt($datapost, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0');
curl_setopt($datapost, CURLOPT_POST, 1);
curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
curl_setopt($datapost, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookie.txt");
$result = curl_exec ($datapost);
curl_close ($datapost);
return $result;
}
function getParamLogin($url,$pageType) {
//return the html page
$html = file_get_html($url);
$eventtarget = '';
$eventargument = '';
if(sizeof($html->find('[id=__EVENTTARGET]')) > 0)
$eventtarget = $html->find('[id=__EVENTTARGET]')[0]->value;
if(sizeof($html->find('[id=__EVENTARGUMENT]')) > 0)
$eventargument = $html->find('[id=__EVENTARGUMENT]')[0]->value;
//post params
$params = array('ctl00_ScriptManager1_HiddenField' => $html->find('[id=ctl00_ScriptManager1_HiddenField]')[0]->value,
'__EVENTTARGET' => $eventtarget,
'__EVENTARGUMENT' => $eventargument,
'__VIEWSTATE' => $html->find('[id=__VIEWSTATE]')[0]->value,
'__VIEWSTATEGENERATOR' => $html->find('[id=__VIEWSTATEGENERATOR]')[0]->value,
'__EVENTVALIDATION' => $html->find('[id=__EVENTVALIDATION]')[0]->value,
'ctl00$search' => '',
'ctl00$newsletter' => ''
);
switch ($pageType) {
case 'firstpage':
$login = array('ctl00$pagina$tlogin_area' => 'user',
'ctl00$pagina$tpassword_area' => 'passwd',
'ctl00$pagina$tinvia' => 'ACCEDI',);
$params = array_merge($params,$login);
break;
case 'secondpage':
$cart = array('__SCROLLPOSITIONX' => '0',
'__SCROLLPOSITIONY' => '0',
'ctl00$pagina$taglie' => '6M',
'ctl00$pagina$tag' => 'Aggiungi al carrello');
$params = array_merge($params,$cart);
break;
}
print_r($params);
return $params;
}
?>
<?php
login('http://www.loginpage.com', getParamLogin('http://www.loginpage.com','firstpage'));
post_data('http://www.anotherpage.com',getParamLogin('http://www.anotherpage.com','secondpage'));
?>
Интересно, есть ли cookie в ручке cookie, а затем в состоянии просмотра … Кто-нибудь может мне помочь? Спасибо!
Задача ещё не решена.
Других решений пока нет …