Page 1 of 1

cURL won't post data :(

Posted: Fri Apr 09, 2010 3:34 am
by WIMP
Hi!
I'm building an application for my employer which is supposed to log into an online service, search for some vehicle info using license plate number and return this info to the script. The problem I'm having is that I am able to log in to the online service and return the "member's area", but when I try to search for the vehicle info using the form in said member's area, the data returned is just the same as before I posted the search form. The form's action seems to be the same as the uri I'm entering when logged in.

My file looks like this:

Code: Select all

<?php
//====================================================//
// Id:     index.php 								      //
// Date: 05.04.10 								      //
// Desc: File to connect to nbk.no using cURL		              //
// Auth: author								              //
//====================================================//

// Get functions.php //
require_once('functions.php');

// Get 1st redirect url to use for login url //
$url = 'http://nbk2.autodata.no';
$redir_1 = curl_extract($url);
$url_2 = $redir_1['url'];

// Attempt to login using $url_2 //
$data_2 = array('username' => urlencode('user'), 'password' => urlencode('pass'));
$redir_2 = curl_extract($url_2,true,$data_2);
$url_3 = $redir_2['url'];

// Attempt to search for vehicle when logged in //
$license = $_GET['plate'];
empty($license) == true ? $license = 'dummypl8' : $license = $license;
$data_3 = array(
			  'tp_articlesearch$search_vehicle_criterion' => '0', 
			  'tp_articlesearch$input_search_single' => urlencode($license),
			  'tp_articlesearch$btn_search_single' => ''
			  );
$action_raw = curl_extract($url_3,false,'',true);
$action = extract_unit($action_raw,'<form name="Main" method="post" action="','" id="Main">');
$action = 'http://nbk2.autodata.no/' . html_entity_decode($action);
$content = curl_extract($action,true,$data_3,true);
echo $content;
?>
This outputs exactly the same as

Code: Select all

<?php
$action_raw = curl_extract($url_3,false,'',true);
echo $action_raw;
?>
In other words: the search form doesn't seem to get the data, or the data doesn't get posted somehow. :?
Does anyone have any clue why this is, and how it can be fixed?

My functions.php file looks like this btw :idea:

Code: Select all

<?php
//====================================================//
// Id: 	functions.php 								  //
// Date: 05.04.10 									  //
// Desc: Misc cURL fuctions			 			  	  //
// Auth: Rune S. Lien								  //
//====================================================//

//===== Extract url or response from cURL-session ====//
function curl_extract( $url, $post = false, $postData = '', $returnContent = false ){
	$agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; nb-NO; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
	$options = array(
	   CURLOPT_RETURNTRANSFER => true,     // return web page 
           CURLOPT_HEADER         => true,     // return headers 
           CURLOPT_FOLLOWLOCATION => true,     // follow redirects 
           CURLOPT_ENCODING       => "",       // handle all encodings 
           CURLOPT_USERAGENT      => $agent,   // user agent
           CURLOPT_AUTOREFERER    => true,     // set referer on redirect 
           CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect 
           CURLOPT_TIMEOUT        => 120,      // timeout on response 
           CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
	);
	
	if($returnContent == true){
		$options[CURLOPT_HEADER] = false;
	}
	
	if($post == true){
		$options[CURLOPT_POST] = true;			  // Enable post headers
		$options[CURLOPT_POSTFIELDS] = $postData; // Set post data
	}
	
	$ch 		= 	curl_init( $url );	  // Initialize cURL session
	curl_setopt_array( $options );		  // Set cURL options
	$content 	=	curl_exec( $ch );	  // Get cURL response [Returned]
	$errno		=	curl_errno( $ch );	  // Get cURL error number
	$errmsg		=	curl_error( $ch );	  // Get cURL error message
	$header		= 	curl_getinfo( $ch );  // Get cURL-info [Returned]
	curl_close( $ch );
	
	return $returnContent == false ? $header : $content; // Return header or content
}
//================= EOF curl_extract() ===============//

//====== Extract a sub-string from given content =====//
function extract_unit($string, $start, $end){
	$pos = stripos($string, $start);
	$str = substr($string, $pos);
	$str_two = substr($str, strlen($start));
	$second_pos = stripos($str_two, $end);
	$str_three = substr($str_two, 0, $second_pos);
	$unit = trim($str_three); // remove whitespaces
	
	return $unit;
}
//================= EOF extract_unit() ===============//
?>
Any help would be greatly appretiated! :mrgreen:

Re: cURL won't post data :(

Posted: Fri Apr 09, 2010 3:19 pm
by requinix
It's kinda hard to say without being able to see the search form. Have you accounted for any JavaScript it might try to run?

Re: cURL won't post data :(

Posted: Mon Apr 12, 2010 6:21 am
by WIMP
To be quite honest with you; no. :P
I checked the source of the web service now and it seems to run a function through javascript when the "Search"-button is pressed.

The JS function looks like this:

Code: Select all

function WebForm_DoPostBackWithOptions(options) {
    var validationResult = true;
    if (options.validation) {
        if (typeof(Page_ClientValidate) == 'function') {
            validationResult = Page_ClientValidate(options.validationGroup);
        }
    }
    if (validationResult) {
        if ((typeof(options.actionUrl) != "undefined") && (options.actionUrl != null) && (options.actionUrl.length > 0)) {
            theForm.action = options.actionUrl;
        }
        if (options.trackFocus) {
            var lastFocus = theForm.elements["__LASTFOCUS"];
            if ((typeof(lastFocus) != "undefined") && (lastFocus != null)) {
                if (typeof(document.activeElement) == "undefined") {
                    lastFocus.value = options.eventTarget;
                }
                else {
                    var active = document.activeElement;
                    if ((typeof(active) != "undefined") && (active != null)) {
                        if ((typeof(active.id) != "undefined") && (active.id != null) && (active.id.length > 0)) {
                            lastFocus.value = active.id;
                        }
                        else if (typeof(active.name) != "undefined") {
                            lastFocus.value = active.name;
                        }
                    }
                }
            }
        }
    }
    if (options.clientSubmit) {
        __doPostBack(options.eventTarget, options.eventArgument);
    }
}
Is there any way of bypassing this or sending the data correctly to this function using cURL?