I am trying to call the javascript __doPostback function on an asp.net page with php using curl.
I found out that this can be done by sending a request to an asp.net page with the appropriate parameters.
So in curl,
- I am making a get request / just using
file_get_contents to retrieve the start page. - From this I extract the values โโfor
__VIEWSTATE and __EVENTVALIDATION .
So far, everything looks fine.
Now I understand that we need to make a post request using cURL with __VIEWSTATE and other necessary parameters. (values โโfor fields present on asp.net form)
I cannot build CURLOPT_POSTFIELDS .
For example, I'm trying to do this,
$postoptions1='__EVENTTARGET='.('ctl00$ContentPlaceHolder1$gRef').'&__EVENTARGUMENT='.('$2'); $postoptions2 = '&__VIEWSTATE='.urlencode($viewState) ; $otherparams = '&ctl00$ContentPlaceHolder1$ddlName=Abc';
And before using setopt for CURLOPT_POSTFIELDS , I do,
urlencode ($postoptions1.$postoptions2.$otherparams)
This does not work. The presented results are not displayed, which means that the required __VIEWSTATE parameter __VIEWSTATE not found in my mail request.
If I change the order of the parameters and put __VIEWSTATE as the first parameter, the results page will appear, but the other parameter values โโwill not be respected.
I think there is some problem with the way I code the parameters.
Please tell me how to create parameters for the mail request on the asp.net page.
Thanks.
- ed -
Here is the complete code: $ ResultsPerPage = '10'; $ url = "www.example.com"; // URL changed
$ curl_connection = curl_init ($ url); sendCurl function ($ 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); ---- **