Jump to content

Posting to a form with curl not working..


kutchbhi

Recommended Posts

Trying to post to this page here:

 

How it works: The form has several hidden fields, one inputs value is generated randomly each time. So had to parse the page(using simple html dom) to get that. Then using firebug, found other input fields that were 'generated'(fetched?) with javascript. Used cookiejar and cookiefile to keep cookies between sessions.

But the end result is that it isn't posting. It just echoes the form page.

The code:

<?php
    include("includes/simple_html_dom.php") ;
    define("WEBBOT_NAME", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
    define("CURL_TIMEOUT", 25);

function get_initial_page($target)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $target) ;       // Target site
              
        curl_setopt($ch, CURLOPT_COOKIEJAR, "C:/cookie-lawyer.txt"); //CHANGE THIS 
        curl_setopt($ch, CURLOPT_REFERER, "www.google.com");  
        curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT);    // Timeout
        curl_setopt($ch, CURLOPT_USERAGENT, WEBBOT_NAME); 
       // curl_setopt ($ch, CURLOPT_POST, 1);
       //  curl_setopt ($ch, CURLOPT_POSTFIELDS, "username=kutchbhi&password=rem0te&autologin=1&hideonline=1&redirect=&login=Log in" );
        
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // Follow redirects
        curl_setopt($ch, CURLOPT_MAXREDIRS, 4);             // Limit redirections to four
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     // Return in string  
        $curled_page = curl_exec($ch);
        curl_close($ch);
        return $curled_page ;
    }
    
    function post_with_curl($target,$ref, $name ,$viewStateValue )
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $target) ;       // Target site
              
        curl_setopt($ch, CURLOPT_COOKIEFILE, "C:/cookie-lawyer.txt"); //CHANGE THIS 
        curl_setopt($ch, CURLOPT_REFERER, $ref);  
        curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT);    // Timeout
        curl_setopt($ch, CURLOPT_USERAGENT, WEBBOT_NAME); 
        curl_setopt ($ch, CURLOPT_POST, 1);
        curl_setopt ($ch, CURLOPT_HEADER, 1);
        switch ($target)
        {
            case "http://198.173.15.31/V2/COUNTY/Default.aspx":
            // echo "targe found" ;
            $postfields = array("__EVENTTARGET" => '' , "__EVENTARGUMENT" => '' , "__VIEWSTATE" => $viewStateValue , "__VIEWSTATEENCRYPTED" => '' , 'ctl00$ContentPlaceHolder1$NameSearch1$CompanyNameTextBox1' => $name , 'ctl00$ContentPlaceHolder1$SearchButton' => 'Search Now'  ) ;
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields );
            break ;
        }
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // Follow redirects
        curl_setopt($ch, CURLOPT_MAXREDIRS, 4);             // Limit redirections to four
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     // Return in string  
        $curled_page = curl_exec($ch);
        curl_close($ch);
        return $curled_page ;
    }

               $viewStateValue = "" ;
               // $url = "default.htm" ; $formPage = file_get_html($url) ; 
                $url = "http://198.173.15.31/V2/COUNTY/" ;  
                $curled_page = get_initial_page($url) ;              
                $formPage = str_get_html($curled_page) ; 
              
                $viewState = $formPage->find('input[id=__VIEWSTATE]') ;
                foreach($viewState as $theState)
                {
                 //   echo "found viewstate"  ."<br>";;
                    $viewStateValue = $theState->value   ;
                  //  echo $viewStateValue  ."<br>";;
                }
               // echo $viewStateValue ;
               $resultPage = post_with_curl("http://198.173.15.31/V2/COUNTY/Default.aspx","http://198.173.15.31/V2/COUNTY/", "jenna" ,$viewStateValue ) ;
               echo $resultPage ;
?>

 

the contents of the cookie file:

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_198.173.15.31	FALSE	/	FALSE	0	ASP.NET_SessionId	fxr0bu45fvme0wbv4t5u0k55

can someone kind have a look ? is it because of the javascript generated input fields? My guess is that the cookies are not being maintained within sessions.

 

Thanks

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.