Jump to content

[SOLVED] HELP!!! cURL POST problem


msinternet

Recommended Posts

Hi brainy ones,

 

I am having a real problem with a cURL script. I based it on something that worked in another site but this is failing. The idea is a registration form is filled in on one page and the data is sent via cURL to register you in another system. If I run the script I get an output of the form as if I had not sent any post data but I am sure I have done this correctly. I set the content type etc. but it just will not work. If I complete the form that is returned it works fine.

 

The site I am posting to tries to set a cookie but I think I am handling this correctly and I have echoed my NVP string and that looks fine.

 

My code is:

 

$post = "ddlLearnerGroups=" . urlencode($_POST['group']) . "&auth=" . urlencode($_POST['auth']) . "&txtLogin=" . urlencode($_POST['username']) . "&txtPassword=" . urlencode($_POST['password']) . "&txtCPassword=" . urlencode($_POST['password2']) . "&txtFName=" . urlencode($_POST['fname']) . "&txtLName=" . urlencode($_POST['sname']) . "&txtEmail=" . urlencode($_POST['email']) . "&txtPhone=" . urlencode($_POST['phone']) . "&txtQuestion=" . urlencode($_POST['secretquestion']) . "&txtAnswer=" . urlencode($_POST['secretanswer']) . "&btnSubmit.x=46&btnSubmit.y=13";
$url = "http://www.example.com/directory/Registration.aspx";
		$header = array("POST /directory/Registration.aspx HTTP/1.1",
						"Host: www.example.com",
						"Connection: close",
						"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
						"Accept-Encoding: gzip",
						"Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7",
						"Cache-Control: no",
						"Accept-Language: de,en;q=0.7,en-us;q=0.3",
						"Referer: http://www.example.co.uk/",
						"Content-type: application/x-www-form-urlencoded",
						"Content-length: 0"
						);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)');
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_VERBOSE,1);
		curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
		curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
		curl_setopt($ch, CURLOPT_REFERER, 'http://www.example.com/directory/');
		//curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$curlres = curl_exec($ch);

 

The return headers are:

 

HTTP/1.1 200 OK Connection: close Date: Wed, 19 Aug 2009 17:10:46 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET X-AspNet-Version: 1.1.4322 Cache-Control: private Content-Type: text/html; charset=utf-8 Content-Length: 18329

 

I really need to get this working ASAP but I am stumped.

 

Thanks in advance.

 

Martin

Link to comment
https://forums.phpfreaks.com/topic/171022-solved-help-curl-post-problem/
Share on other sites

Hi all,

 

Looks like I beat you all to it!

 

The problem was that the ASP.NET form uses a __VIEWSTATE in the form which is like a rubbishly implemented session identifier. So you have to initiate a session, get the viewstate and then add that to the post data. I did this by doing a cURL request, trimming down to the value and then performing a second cURL data submission.

 

Works like a treat!

 

I hope someone finds this helpful anyway,

 

Martin

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.