Re: Simulate Submit in a function
| From: | Joe Conway | Date: | Sun, 12 Nov 2000 20:37:30 +0000 |
| Subject: | Re: Simulate Submit in a function | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-24873@lists.php.net to get a copy of this message | ||
> I am trying to write a function that posts required cc info to verisign /
> signio payflow link. The function has to submit the info and get the
> results. I want it to be transparent to the user.
>
> Is fopen / fwrite the right tree to bark up?
>
> Anyone done this or have a function that works?
>
> TIA
>
> Kevin
Here's a quick and dirty function to conditionally GET or POST depending on
desired method. $request is a string of the variables being passed, e.g.
$request = "var1=foo&var2=bar". $rsf is an array of values from a PostgreSQL
query which includes the URL of the target page ($rsf["target_url"]) and the
desired method ($rsf["target_method"]).
Hope this helps --
Joe
<?
function process_submission($request,$rsf) {
$request = substr($request,0,strlen($request)-1);
print("Request: " . $request . "<br>\n");
// Build the header
$tgt = substr($rsf["target_url"],strpos($rsf["target_url"],"/" ,7));
print("Target: " . $tgt . "<br>\n");
flush();
if (strtoupper($rsf["target_method"])=="POST") {
$header = "POST " . $tgt . " HTTP/1.0\r\n";
$header .= "Content-type: application/x-www-form-urlencoded\r\n";
$header .= "Content-length: " . strlen($request) . "\r\n\r\n";
} else {
$header = "GET " . $tgt . "?" . $request . " HTTP/1.0\r\n\r\n";
};
print("Header: " . $header . "<br>\n");
flush();
// Open the connection
$host = substr($rsf["target_url"],7,strpos($rsf["target_url"],"/" ,7)
- 7);
print("Host: " . $host . "<br><hr>\n");
flush();
$fp = fsockopen($host, 80, &$err_num, &$err_msg, 30);
if ($fp) {
// Send everything
if (strtoupper($rsf["target_method"])=="POST") {
fputs($fp, $header . $request);
} else {
fputs($fp, $header);
};
// Get the response
print("Response:<br><hr>\n");
flush();
while (!feof($fp)) {
$response = fgets($fp, 128);
print($response);
flush();
};
print("<br><hr>\n");
flush();
};
};