|
Posted by Charlie King on 08/16/06 07:47
I'm trying to send data to a remote script (a credit card processing
third party) from my site using POST.
Currently, I'm doing it using the ususal form dynamically built with
my values.
This is less than ideal because I want to perform local processing
(updating order status in the local database) and then to proceed to
the remote script, with resultant values, without relying on the user
pressing a [submit] button.
I'm part of the way there - I have a function that uses fsock to open
a socket, sends the POST data and retrieves the response from the
remote server into an array - but it falls short of emulating an
actual posted form, in that the user isn't transferred to the remote
site with the data, as they would be with a form.
I toyed idly with using a header("Location: $originalurl"), but that
just redirects the user independantly from the data, and so the target
script b0rks.
I don't want to use javascript to click-on-open the form, or to use
AJAX, because that relies on the users having javascript enabled.
I can't use cURL, because the client's environment doesn't have it
installed. I understand that cURL could do what I want to do, which
implies that it's do-able.
Any ideas?
Cheers
Charlie
function post_data($datastream, $url) {
$originalurl=$url;
if (substr($originalurl, 0, 5) == "https") {
$protocol = "https";
$url = preg_replace("@^https://@i", "", $url);
$port = 443;
} else {
$protocol = "http";
$url = preg_replace("@^http://@i", "", $url);
$port = 80;
}
$host = substr($url, 0, strpos($url, "/"));
$uri = strstr($url, "/");
$reqbody = "";
foreach($datastream as $key=>$val) {
if(is_array($val)){ //don't url encode if we're passing an array
if (!empty($reqbody)) $reqbody .= "&";
$reqbody .= $key."=".$val;
}else{
if (!empty($reqbody)) $reqbody .= "&";
$reqbody .= $key."=".urlencode($val);
}
}
$reqlength = strlen($reqbody);
$reqheader = "POST $uri HTTP/1.0\r\n".
"Host: $host\r\n" . "User-Agent: CK-Conception POST-o-matic\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: $reqlength\r\n\r\n".
"$reqbody\r\n";
# header("Location: $originalurl");
if (substr($originalurl, 0, 5) == "https") {
$socket = fsockopen("ssl://" . $host, $port, $errno, $errstr);
} else {
$socket = fsockopen($host, $port, $errno, $errstr);
}
if (!$socket) {
$result["errno"] = $errno;
$result["errstr"] = $errstr;
return $result;
}
fputs($socket, $reqheader);
while (!feof($socket)) {
$result[] = fgets($socket, 4096);
}
fclose($socket);
return $result;
}
--
Charlie
Navigation:
[Reply to this message]
|