Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic POSTing of Data

Status
Not open for further replies.

jerijeri

Programmer
Sep 11, 2002
33
CA
Hi,

I have a program called questions.php. It displays 5 text fields. The user then fills in the fields and the information is posted back to questions.php.

At this time the information is stored in a MySQL database and the information entered is sent by means of a querystring to a different server. (The user knows this)
example it is redirected to
This has worked fine for us. The problem is that the site that the data is being submitted to no longer wants to accept data by the GET method. They want it POSTed.

For us to have a copy of the information, we'd have to have a confirmation screen and then have the user hit Submit again. We've done that in the past and some people don't hit Submit the second time.

The other solution is using javascript to automatically POST the data. The reason we don't want to do that is due to the number of people who have disabled javascript.

So, the question is - is it possible to do a POST automatically in PHP. All help greatly appreciated.

Thanks,

Jer
 
I have two solutions, one using fsockopen() and one using CURL. I'm going to post my code -- in this case, it's a lot easier than trying to explain what I did.

Doing it with CURL is probably the easiest of the two, but not all PHP installations have CURL built in.

Neither script has as much error-checking as it needs for a production environment.

Here's the CURL solution:

Code:
<?php
$cgi = 'mycgi.php';
$hostname = 'myhost';
$HTTP_method = '[URL unfurl="true"]http://';[/URL]

$data_string = &quot;&quot;;
$add_ampersand = FALSE;
foreach ($_GET as $key => $value)
{
	if ($add_ampersand)
	{
		$data_string .= '&';
	}
	$data_string .= $key . '=' . $value;
	$add_ampersand = TRUE;
}

$curl_handle = curl_init ();

curl_setopt ($curl_handle, CURLOPT_URL, $HTTP_method . $hostname . $cgi);
curl_setopt ($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_POST, 1);
curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $data_string);
	
$result = curl_exec ($curl_handle) or die (&quot;There has been an error&quot;);
	
curl_close ($curl_handle);
	
print $result;

?>


Here's the fsockopen() solution:

Code:
<?php
$cgi = 'myscript.php';
$hostname = 'myhost';

$send_string = 
&quot;POST $cgi HTTP/1.1&quot; . &quot;\r\n&quot; .
&quot;Host: $hostname&quot; . &quot;\r\n&quot; .
'Content-Type: application/x-[URL unfurl="true"]www-form-urlencoded'[/URL] . &quot;\r\n&quot; .
'Content-Length: ';

$data_string = &quot;&quot;;
$add_ampersand = FALSE;
foreach ($_GET as $key => $value)
{
	if ($add_ampersand)
	{
		$data_string .= '&';
	}
	$data_string .= $key . '=' . $value;
	$add_ampersand = TRUE;
}

$send_string .= strlen($data_string) .&quot;\r\n\r\n&quot; . $data_string . &quot;\r\n\r\n&quot;;


$fh = fsockopen ($hostname, 80);

fputs ($fh, $send_string);

$return = fread ($fh, 4096);

fclose ($fh);

$return_array = preg_split ('/\r\n/', $return);

print '<pre>';
print (preg_match ('/ 200 /', $return_array[0]))? 'Success' : 'Error';
print &quot;\n&quot;;
print_r ($return_array);
print '</pre>';

?>
______________________________________________________________________
TANSTAAFL!
 
Thank you, I will check out the fsockopen approach.

I forgot to mention that Curl isnt' installed on the site and since it's not their server they can't install it.

Jer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top