[英]cURL post data to asp.net page
I am trying to call the __doPostback
javascript function in a asp.net page from php using curl. 我试图使用curl从php中调用asp.net页面中的__doPostback
javascript函数。
I learnt that this can be done by making a post request to the asp.net page with the appropriate parameters. 我了解到这可以通过使用适当的参数向asp.net页面发布请求来完成。
So in curl, 所以在卷曲中,
file_get_contents
to retrieve the initial page. 我发出get请求/只需使用file_get_contents
来检索初始页面。 __VIEWSTATE
and __EVENTVALIDATION
. 从这里,我提取__VIEWSTATE
和__EVENTVALIDATION
的值。 So far everything seems ok. 一切似乎都好。
Now, I understand that we need to make a post request using cURL with __VIEWSTATE
and other parameters required. 现在,我知道我们需要使用带有__VIEWSTATE
cURL和其他所需参数来发布请求。 ( values for the fields present in the asp.net form ) (asp.net表单中的字段值)
I am unable to construct the CURLOPT_POSTFIELDS
correctly. 我无法正确构造CURLOPT_POSTFIELDS
。
For instance, I am trying this out, 例如,我正在尝试这个,
$postoptions1='__EVENTTARGET='.('ctl00$ContentPlaceHolder1$gRef').'&__EVENTARGUMENT='.('$2');
$postoptions2 = '&__VIEWSTATE='.urlencode($viewState) ;
$otherparams = '&ctl00$ContentPlaceHolder1$ddlName=Abc';
And before using setopt for CURLOPT_POSTFIELDS
, I am doing, 在使用setopt进行CURLOPT_POSTFIELDS
之前,我正在做,
urlencode ($postoptions1.$postoptions2.$otherparams)
This does not work. 这不起作用。 The submit results are not shown, which means, the required parameter __VIEWSTATE
was not found in my post request. 未显示提交结果,这意味着在我的帖子请求中找不到所需的参数__VIEWSTATE
。
If I change the order of the parameters and place __VIEWSTATE
as the first parameter, the results page is shown but the other parameter values are not honoured. 如果我更改参数的顺序并将__VIEWSTATE
作为第一个参数,则会显示结果页面,但不会遵循其他参数值。
I think there is some problem with the way I am encoding the parameters. 我认为我对参数进行编码的方式存在一些问题。
Please tell me how to construct the parameters for the post request to a asp.net page. 请告诉我如何构建对asp.net页面的post请求的参数。
Thanks. 谢谢。
--Edited-- --Edited--
Here is the complete code: $resultsPerPage='10'; 这是完整的代码:$ resultsPerPage = '10'; $url = "www.example.com"; $ url =“www.example.com”; // url changed //网址已更改
$curl_connection = curl_init($url); $ curl_connection = curl_init($ url); function sendCurl($curl_connection,$url,$params,$isPost=false) { function sendCurl($ curl_connection,$ url,$ params,$ isPost = false){
//$post_string = $params;
$post_string = http_build_query($params);
//$post_string = build_query_string($params);
//$post_string = urlencode($params);
echo 'After Encode'.$post_string;
$cookie="/cookie.txt";
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_HEADER, 0); // don't return headers
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection,CURLOPT_REFERER, $url);
if($isPost) {
curl_setopt ($curl_connection, CURLOPT_POST, true);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($curl_connection,CURLOPT_COOKIEJAR,$cookie);
}
else {
curl_setopt($curl_connection,CURLOPT_COOKIEFILE,$cookie);
}
$response1 = curl_exec($curl_connection);
if($response1 === false)
{
echo 'Curl error: ' . curl_error($curl_connection);
}
else
{
echo 'Operation completed without any errors';
}
return $response1;
} **// First time, get request to asp.net page
$response1 = sendCurl($curl_connection,$url,'',false);
----** ---- **
$viewState=getVStateContent($response1);
$eventValidation =getEventValidationContent($response1);
$simpleParams = '&__VIEWSTATE='.$viewState.'&ctl00$ContentPlaceHolder1$ddlManuf=&ctl00$ContentPlaceHolder1$ddlCrossType=&ctl00$ContentPlaceHolder1$ddlPageSize='.$resultsPerPage.'&ctl00$ContentPlaceHolder1$btnSearch=Search&ctl00_ToolkitScriptManager1_HiddenField=&__EVENTTARGET=&__EVENTARGUMENT=';
// Second post - for submitting the search form
$response2= sendCurl($curl_connection,$url,$simpleParams,true);
What you want is http_build_query , which will format an array as proper HTTP parameters. 你想要的是http_build_query ,它将数组格式化为正确的HTTP参数。
Edit : To clarify what this should probably look like: 编辑 :澄清这应该是什么样子:
$params = array(
'__EVENTTARGET' => 'ctl00$ContentPlaceHolder1$gRef',
'__EVENTARGUMENT' => '$2',
'__VIEWSTATE' => $viewState,
'ctl00$ContentPlaceHolder1$ddlName' => 'Abc'
);
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, http_build_query($params));
Also, what's ctl00$ContentPlaceHolder1$ddlName
supposed to be? 另外,什么是ctl00$ContentPlaceHolder1$ddlName
应该是什么?
不要urlencode()将参数链接在一起的&符号(&),只是键和值(&符号两侧的东西)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.