4

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 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);
----**

Srividya Sharma
  • 778
  • 1
  • 7
  • 16

2 Answers2

1

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?

Michael McTiernan
  • 5,153
  • 2
  • 25
  • 16
  • That did not help in my case. Here is the output: __VIEWSTATE=0k1ruWH%2BzSohS0G0kOy6JhOZxeadZDkpmU9syivmmwxj0FfA9qHwJSDDG%2Bf2URv%2FGqnsfcIbapMhbqUg%3D%3D&__EVENTVALIDATION=JV9xlRMggTR14sqkFT0zMJ0aO5LSOp41vy4kIKnCMwIoNnh1SiyeMUgcbb9J1jec4S8Yr3s%2FakWbjbgsLZLa081BYSObgoLU17lL1YEaCLj0dKhrduupil4RSqqURfGbj%2FIpfqtjsD%2BHxC2ZcfddlJFhMZdh&ctl00_ToolkitScriptManager1_HiddenField=&ctl00%24ContentPlaceHolder1%24name=&ctl00%24ContentPlaceHolder1%24type=&ctl00%24ContentPlaceHolder1%size=10&ctl00%24ContentPlaceHolder1%24btnSearch=Search&__EVENTTARGET=&__EVENTARGUMENT= . I get a Page Not Found. – Srividya Sharma Mar 22 '11 at 03:22
  • If a 404 Page Not Found was returned, then the URL you're using probably isn't an actual resource. I wouldn't be able to tell without more context, however. – Michael McTiernan Mar 22 '11 at 03:29
  • This query string works (I dont get page not found), but only first param is considered: __VIEWSTATE%252FgRlmFUjXphs2ES6bzvyvKWm62AcGQ7cyD%252F1aAQuioHYWQ%253D%253D%26__EVENTTARGET%3Dctl00%24ContentPlaceHolder1%24gvCrossRef%26__EVENTARGUMENT%3DPage%242%26ctl00%2524ContentPlaceHolder1%2524txtSearch%3DExar%26ctl00%2524ContentPlaceHolder1%2524name%3D%26ctl00%2524ContentPlaceHolder1%2524ddltype%3D%26ctl00%2524ContentPlaceHolder1%2524size%3D10%26ctl00%2524ContentPlaceHolder1%2524btnSearch%3DSearch%26ctl00_ToolkitScriptManager1_HiddenField%3D – Srividya Sharma Mar 22 '11 at 03:46
  • Are you `urlencode`ing after running `http_build_query`? Because you shouldn't be. – Michael McTiernan Mar 22 '11 at 04:26
  • Updated my answer to reflect what the whole thing should look like. – Michael McTiernan Mar 22 '11 at 04:34
  • It is supposed to be a text field name. – Srividya Sharma Mar 22 '11 at 05:42
  • Code from your updated post did not work either. $params3 = '__VIEWSTATE='.$viewState; $post_string = urlencode($params); This shows the page, but as stated in my post, the other parameters are not considered.Does it have anything to do with the length of the post parameters? – Srividya Sharma Mar 23 '11 at 04:24
  • If the page you're requesting itself is able to post the same exact parameters, then it couldn't be the length of the parameters that is affecting your request. My guess is that there's something else you might not be doing, but I can't tell because your original post doesn't really show the entire request. – Michael McTiernan Mar 23 '11 at 16:35
  • I have updated my post to include the complete code. Please see if you can modify it to make it work. Thanks in advance. – Srividya Sharma Mar 27 '11 at 02:10
  • I have accepted your answer. Looks like it works from Ubuntu but not windows. Thank you. – Srividya Sharma Apr 08 '11 at 10:49
0

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

Devin Ceartas
  • 4,743
  • 1
  • 20
  • 33
  • I tried this. The query string looks like this: __VIEWSTATE=%2FM2fyvIXCicvzPxQrhT%2BVHnkc1NnV609%2FMiR97D%2Ft7%2FCJN22ZLO2E3rieAbWd25A%2Fc%2Bgaahtefb4LMAs5cZm2Lj7KaSn7qmlk1XgeKTFodhzTnLzvd4aSRP5GqOEuWVKPODsh2SMQtJYfLKfvUFs8oYhr5LKJ53zXyEKcQ0PW1n5StLIA5Y1%2FgAwlAc&ctl00_ToolkitScriptManager1_HiddenField=&ctl00%24ContentPlaceHolder1%24name=&ctl00%24ContentPlaceHolder1%24type=&ctl00%24ContentPlaceHolder1%24size=10&ctl00%24ContentPlaceHolder1%24btnSearch=Search&__EVENTTARGET=&__EVENTARGUMENT= . I get page not found error. – Srividya Sharma Mar 22 '11 at 04:00