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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending data to a CGI via POST

Coding

Sending data to a CGI via POST

by  sleipnir214  Posted    (Edited  )
[tt]
Code:
<?php
/*
	There are two fundamental ways for PHP to send data
	to another CGI via the POST-method:  CURL and fopen.
	
	Where CURL is the easier of the two, fopen is more
	commonly available.
	
	Check the output of phpinfo() to see whether CURL is
	available on your system.


	DISCLAIMER
	This script is intended only to demonstrate a
	possible solution, not to provide a plug-and-play
	solution to a problem.  It will nearly certainly
	not work on your system until you have modified it.
	
	This source code comes without warranty of any kind.
	It might work as-is and solve your problem.
	It might also cause your system to fail 
	catastrophically and spectacularly.
	Use with circumspection and trepidation.
	END DISCLAIMER
	
*/



/*
	This method uses CURL to contact the server.
	
*/


//	Either 'http' or 'https'. 'https' is only an
//	option if OpenSSH is available on your system.
//	Check phpinfo() to see whether HTTPS is available.
$HTTP_method = 'http';

//	IP-resolvable FQDN of the server
$hostname = 'hostname.myrurl.com';

//	Path on that server to the CGI
$cgi = '/mypath/mycgi';

//	Array of data.  The foreach loop below is going to
//	construct a field/data string like the one you see
//	in the URL of a GET-method CGI.
$my_data = array (							
					'field1' => 'value1',	
					'field2' => 'value2',
					'field3' => 'value3',
					'field4' => 'value4'
				);

//	This section constructs the field/value pairs of
//	the form:
//	field1=value1&field2=value2&field3=value3
$data_string = ';
$add_ampersand = FALSE;
foreach ($my_data as $key => $value)
{
	if ($add_ampersand)
	{
		$data_string .= '&';
	}
	$data_string .= $key . '=' . $value;
	$add_ampersand = TRUE;
}

//	Get a CURL handle
$curl_handle = curl_init ();

//	Tell CURL the URL of the CGI
curl_setopt ($curl_handle, CURLOPT_URL, $HTTP_method . '://' . $hostname . $cgi);

//	This section sets various options.
//	See http://www.php.net/manual/en/function.curl-setopt.php
//	for more details
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);
	
//	Perform the POST and get the data returned by the
//	server.
$result = curl_exec ($curl_handle) or die ('There has been an error');

//	Close the CURL handle
curl_close ($curl_handle);

//	Process the return
print $result;






/*
	This method uses fopen to contact the server.
	
	Frankly, I haven't figured out how to do HTTPS
	POST-method inputs yet.
	
*/

//	IP-resolvable FQDN of the server
$hostname = 'hostname.myrurl.com';

//	Path on that server to the CGI
$cgi = '/mypath/mycgi';

//	Array of data.  The foreach loop below is going to
//	construct a field/data string like the one you see
//	in the URL of a GET-method CGI.
$my_data = array (							
					'field1' => 'value1',	
					'field2' => 'value2',
					'field3' => 'value3',
					'field4' => 'value4'
				);


//	This is the beginning of the HTTP request header
$send_string = 
'POST $cgi HTTP/1.1' . "\r\n" .
'Host: $hostname' . "\r\n" .
'Content-Type: application/x-www-form-urlencoded' . "\r\n" .
'Content-Length: ';

//	This section constructs the field/value pairs of
//	the form:
//	field1=value1&field2=value2&field3=value3
$data_string = "";
$add_ampersand = FALSE;
foreach ($_GET as $key => $value)
{
	if ($add_ampersand)
	{
		$data_string .= '&';
	}
	$data_string .= $key . '=' . $value;
	$add_ampersand = TRUE;
}

//	Complete the send string once we know how long our input string is.
$send_string .= strlen($data_string) ."\r\n\r\n" . $data_string . "\r\n\r\n";

//	Open the socket to the server
$fh = fsockopen ($hostname, 80);

//	Send the data
fputs ($fh, $send_string);

//	Get the return
$return = fread ($fh, 4096);

//	Close the socket
fclose ($fh);

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

?>
[/tt]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top