开发者

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.

I learnt that this can be done by making a post request to the asp.net page with the appropriate parameters.

So in curl,

  1. I make a get request / just use file_get_contents to retrieve the initial page.
  2. From this, I extract the values for __VIEWSTATE and __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. ( values for the fields present in the asp.net form )

I am unable to construct the CURLOPT_POSTFIELDS correctly.

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,

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.

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.

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.

Thanks.

--Edited--

Here is the complete code: $resultsPerPage='10'; $url = "www.example.com"; // url changed

$curl_connection = curl_init($url); 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 re开发者_如何学编程turn 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.

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?


Don't urlencode() the ampersands (&) linking the parameters together, just the keys & values (the stuff on either side of the ampersands).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜