dronthpi05943 2017-06-17 14:09
浏览 32
已采纳

如何使用php在远程网站上获取表单的原始发布数据

I want to login to a remote website from mine. So, I used cURL to achieve this and I was successful using the code below:

function login($url,$data)
{
   $fp = fopen("cookie.txt", "w");
   fclose($fp);
   $login = curl_init();
   curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
   curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
   curl_setopt($login, CURLOPT_TIMEOUT, 40000);
   curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
   curl_setopt($login, CURLOPT_URL, $url);
   curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
   curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
   curl_setopt($login, CURLOPT_POST, TRUE);
   curl_setopt($login, CURLOPT_POSTFIELDS, $data);
   ob_start();
   return curl_exec ($login);
   ob_end_clean();
   curl_close ($login);
   unset($login);
}

So for posting data I used the raw data for the form in the website. Which I managed to get from developer tools in Edge.Here is the screenshot of raw POST data when I try to login directly in the remote website (http://vce.ac.in)

The problem is the values of the parameters viewstate, validation, viewstategenerator are changing for every 24 hours.

So if there is a method in PHP to get the raw POST data of the website, I can update those values of raw data parameters which are changing.

  • 写回答

1条回答 默认 最新

  • dongzhi4239 2017-06-17 16:52
    关注

    Parse them with a simple PHP DOM

    // get simple_html_dom.php from : https://netix.dl.sourceforge.net/project/simplehtmldom/simple_html_dom.php
    require_once('simple_html_dom.php');
    
    function login($url, $data){
    
       $html = file_get_html( $url );
    
       foreach ($html->find('input') as $input){
           # echo "INPUTDOM". print_r($input);
           if ($input->attr['name']=="__VIEWSTATE"){
               //__VIEWSTATE
               echo "__VIEWSTATE: {$input->attr['value']}
    ";
               $form_data['__VIEWSTATE'] = $input->attr['value'];
    
           } elseif ($input->attr['name']=="__VIEWSTATEGENERATOR"){
               //__VIEWSTATEGENERATOR
               echo "__VIEWSTATEGENERATOR: {$input->attr['value']}
    ";
               $form_data['__VIEWSTATEGENERATOR'] = $input->attr['value'];
    
           } elseif ($input->attr['name']=="__VIEWSTATE"){
               //__EVENTVALIDATION
               echo "__VIEWSTATE: {$input->attr['value']}
    ";
               $form_data['__EVENTVALIDATION'] = $input->attr['value'];
    
           }
       };
    
       return $form_data;
    
    }
    
    
    echo login('http://vce.ac.in/index.aspx',['txtLoginID'=> 'user', 'txtPWD'=> 'password'] );   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?