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'] );   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号