cURL aspx发布问题

时间:2014-04-13 09:10:07

标签: php asp.net curl

我一直在为 www.pool.com

开发php curl脚本

我可以处理视图状态和事件问题,但我看到有关发布的其他问题。

Invalid postback or callback argument.  Event validation is enabled using 
<pages enableEventValidation="true"/> in configuration or <%@ Page
EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies
that arguments to postback or callback events originate from the server control that 
originally rendered them.  If the data is valid and expected, use the 
ClientScriptManager.RegisterForEventValidation method in order to register the postback or
callback data for validation.

在第一部分中,我正在获取eventtarget,viewstate和eventvalidation,这很好。然后我通过发布eventtarget点击高级搜索(你可以在代码中看到)。这也很好。但是,我发布了其他名为CtlBucket_Search1的数据:txtKeyWordCharacters它给我错误无效的回发..

我不明白为什么会有错误。

代码:

<?php

$url = "http://www.pool.com/viewlist.aspx";
$ckfile = tempnam("/tmp", "CURLCOOKIE");
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2';

$f = fopen('log.txt', 'w'); // file to write request header for debug purpose

/**
Get __VIEWSTATE & __EVENTVALIDATION
 */
$event = 'CtlBucket_Search1$lbtSearchToggleAdv';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

$html = curl_exec($ch);

curl_close($ch);

preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation);

$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];



/**
 * Start Posting process
 */
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

// Collecting all POST fields
$postfields = array();
$postfields['__EVENTTARGET'] = $event;
$postfields['__EVENTARGUMENT'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
// Problem is below
$postfields['CtlBucket_Search1:txtKeyWordCharacters'] = 4;

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch); // Get result after login page.

1 个答案:

答案 0 :(得分:0)

在数组http_build_query上使用$postfields函数。它会做两件事。 1)它将使用正常发布(目前您正在发布multipart/form-data发布.2)它将对数组中的值进行urlencode

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));