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 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'] );   
    
    点赞 评论 复制链接分享

相关推荐