编程语言
首页 > 编程语言> > php – cURL将数据发布到asp.net页面

php – cURL将数据发布到asp.net页面

作者:互联网

我试图使用curl从php中调用asp.net页面中的__doPostback javascript函数.

我了解到这可以通过使用适当的参数向asp.net页面发布请求来完成.

所以在卷曲中,

>我发出get请求/只需使用file_get_contents来检索初始页面.
>从中,我提取__VIEWSTATE和__EVENTVALIDATION的值.

一切似乎都好.

现在,我知道我们需要使用带有__VIEWSTATE的cURL和其他所需参数来发布请求. (asp.net表单中的字段值)

我无法正确构造CURLOPT_POSTFIELDS.

例如,我正在尝试这个,

$postoptions1='__EVENTTARGET='.('ctl00$ContentPlaceHolder1$gRef').'&__EVENTARGUMENT='.('$2');
$postoptions2 =  '&__VIEWSTATE='.urlencode($viewState) ;
$otherparams = '&ctl00$ContentPlaceHolder1$ddlName=Abc';

在使用setopt进行CURLOPT_POSTFIELDS之前,我正在做,

urlencode ($postoptions1.$postoptions2.$otherparams)

这不起作用.未显示提交结果,这意味着在我的帖子请求中找不到所需的参数__VIEWSTATE.

如果我更改参数的顺序并将__VIEWSTATE作为第一个参数,则会显示结果页面,但不会遵循其他参数值.

我认为我对参数进行编码的方式存在一些问题.

请告诉我如何构建对asp.net页面的post请求的参数.

谢谢.

–Edited–

这是完整的代码:
$resultsPerPage = ’10’ ;
$url =“www.example.com”; //网址已更改

$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($响应1);
$eventValidation = getEventValidationContent($response1);
$simpleParams =’& __ VIEWSTATE =’.$viewState.’& ctl00 $ContentPlaceHolder1 $ddlManuf =& ctl00 $ContentPlaceHolder1 $ddlCrossType =& ctl00 $ContentPlaceHolder1 $ddlPageSize =’.$resultsPerPage.’& ctl00 $ContentPlaceHolder1 $btnSearch =搜索&安培; ctl00_ToolkitScriptManager1_HiddenField =安培; __ EVENTTARGET =安培; __ EVENTARGUMENT =’;
//第二篇文章 – 用于提交搜索表单
$response2 = sendCurl($curl_connection,$url,$simpleParams,true);
—- **

解决方法:

你想要的是http_build_query,它将数组格式化为正确的HTTP参数.

编辑:澄清这应该是什么样子:

$params = array(
    '__EVENTTARGET' => 'ctl00$ContentPlaceHolder1$gRef',
    '__EVENTARGUMENT' => '$2',
    '__VIEWSTATE' => $viewState,
    'ctl00$ContentPlaceHolder1$ddlName' => 'Abc'
);

curl_setopt($curlHandler, CURLOPT_POSTFIELDS, http_build_query($params));

另外,什么是ctl00 $ContentPlaceHolder1 $ddlName应该是什么?

标签:php,asp-net,curl,viewstate
来源: https://codeday.me/bug/20190710/1420262.html