NYCPHP Meetup

NYPHP.org

[nycphp-talk] Form Action Help

Michael Biamonte mbiamonte at affinitysolutions.com
Wed Feb 5 20:50:21 EST 2003



Here is generically-usable PostToHost function
that can do both ssl and non ssl posts...

It's up to you to parse the result - there's
an echo statement below that you can uncomment
to make sense of the response.

To so an ssl post, you need php 4.3 compiled with openssl...

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


//FUNCTION USE

$data_to_send="x=1&y=2&z=what ever";
$responseFromHost=PostToHost('10.10.10.10', '/path/to/receivepost.pl',
$data_to_send, 1)


//FUNCTION DEFINTION
function PostToHost($host, $path, $data_to_send, $ssl=0)
{
	if ($ssl)
	{
		$host='ssl://'.$host;
		$port='443';
	}
	else
	{
		$port='80';
	}

	$fp=fsockopen($host,$port,$errno,$errstr);

	if (!$fp)
	{
		echo "PostToHost couldn't connect to $host $path with ssl=$ssl";
		return false;
	}
		else
	{
		$count=0;
		$returnvalue='';

		$data_to_send="POST $path HTTP/1.1\
Host: $host\
Content-Type:
application/x-www-form-urlencoded\
Content-Length:
".strlen($data_to_send)."\
Connection: close\
\
".$data_to_send;

		//echo "<HR><PRE>";
		//echo $data_to_send;
		//echo "</PRE><HR>";

		fputs($fp, $data_to_send);
		while(!feof($fp))
		{
			$temp=@fgetss($fp,5000);
			// echo "Line ".$count++." (".strlen($temp)."): ".$temp."<BR>";
			$returnvalue.=$temp;
		}
		$fp=fclose($fp);
		return $returnvalue;
	}




More information about the talk mailing list