By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

POSTing data to a page without using a form submit.

P: n/a
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
Aug 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Charlie King wrote:
I'm trying to send data to a remote script (a credit card processing
third party) from my site using POST.
>From the defunct clp FAQ:
Q. How do I post a form to another site?
A. Use stream_context_create() to create a HTTP POST context, then open
a connection to the site in question with fopen(), passing the context
as the fourth parameter. Use fread() to read the result from the form
submission.

Example:

$post_vars = array(
'post_var1' ="hello",
'post_var2' ="world"
);

// build the request body
foreach($post_vars as $name =$val) {
$pairs[] = $name . '=' . rawurlencode($val);
}

$body = implode("&", $pairs);

// HTTP options
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-length: " . strlen($body) . "\r\n" .
"Cookie: foo=bar\r\n",
'content'=>$body
)
);

$context = stream_context_create($opts);

$f = fopen('http://localhost/test.php', 'r', false, $context);

In PHP 5, a context can also be passed to file_get_contents() and
file().

Although stream_context_create() is available since PHP 4.3.0, support
for HTTP POST has only become available in 4.3.5. If you are using an
older version, you would need the cURL functions or use fsockopen() to
open the connection and send the request with fputs().

Aug 16 '06 #2

P: n/a
Charlie King wrote:
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.
Did you tried to print the result of fsock function? if you get html as
a result you can simply print it and user would get the desired page
content and all the links there should work (if they are absolute ones,
if not, you can tweak them a bit ... :))

Thing is, if you need to redirect to some outside page that displays
something and let's user do something then I wouldn't update anything
before user comes back from that particular page (what happens with the
order if he changes his mind while he's away on that particular page). I
usually log that user xy has arrived on summary page (one before CC
processing) and then when user successfully returns from third party,
then I do the DB stuff.

Hope it helps a bit... :)

--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination
Aug 16 '06 #3

P: n/a
On 16 Aug 2006 08:07:26 -0700, in
<11**********************@b28g2000cwb.googlegroups .com>
(comp.lang.php) "Chung Leong" <ch***********@hotmail.comwrote:
Charlie King wrote:
I'm trying to send data to a remote script (a credit card processing
third party) from my site using POST.
From the defunct clp FAQ:
Thanks for this info... it's a shame the FAQ is defunct, it sounds
like a potentially useful source of info!
Q. How do I post a form to another site?
A. Use stream_context_create() to create a HTTP POST context, then open
a connection to the site in question with fopen(), passing the context
as the fourth parameter. Use fread() to read the result from the form
submission.

Example:
[...snip]

Thanks for that, although it seems to have a similar effect to the
code that I posted, in that it returns the contents of the target
site, in this case to a file stream, in my case to an array.

What I want to do is to have the browser go to the target page, with
my variables, by POST. I.e. exactly as if the user had clicked on a
[submit] button in a form whose action was POST and url was the target
site.
$f = fopen('http://localhost/test.php', 'r', false, $context);
Now that I have a filestream with the remote site's contents in it,
what do I do with it?
In PHP 5, a context can also be passed to file_get_contents() and
file().
Unfortunately, I have to work in PHP 4.4.0 without cURL.
Although stream_context_create() is available since PHP 4.3.0, support
for HTTP POST has only become available in 4.3.5. If you are using an
older version, you would need the cURL functions or use fsockopen() to
open the connection and send the request with fputs().
So I have everything I need to create a stream context with POST
support. Good, now what do I do with it?
--
Charlie
Aug 16 '06 #4

P: n/a
On Wed, 16 Aug 2006 17:14:21 +0200, in <eb**********@ss408.t-com.hr>
(comp.lang.php) "B.r.K.o.N.j.A" <th********@inet.hrwrote:
Charlie King wrote:
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.
Did you tried to print the result of fsock function? if you get html as
a result you can simply print it and user would get the desired page
content and all the links there should work (if they are absolute ones,
if not, you can tweak them a bit ... :))
I did, and I could print it, but, as you point out, all the relative
links are broken.

What I want to achieve is to actually send the user to the remote
site, exactly as though they had hit [submit] on a POST form.

The only problem with using a form is that I want to do some local
processing that is coincidental with their being sent off to the
payment site. I'd rather not have the possibility of loose ends
caused by their deciding not to press the button, and go play football
instead.
Thing is, if you need to redirect to some outside page that displays
something and let's user do something then I wouldn't update anything
before user comes back from that particular page (what happens with the
order if he changes his mind while he's away on that particular page).
In the case of this particular page, I get the user returned with a
'cancelled' or 'timed out' flag that I can process.
>I
usually log that user xy has arrived on summary page (one before CC
processing) and then when user successfully returns from third party,
then I do the DB stuff.
If what I want to do turns out to be as difficult as it is looking,
then I may well have to do that :)
Hope it helps a bit... :)
It does, thank you.
--
Charlie
Aug 16 '06 #5

P: n/a
Charlie King wrote:
What I want to do is to have the browser go to the target page, with
my variables, by POST. I.e. exactly as if the user had clicked on a
[submit] button in a form whose action was POST and url was the target
site.
Oops. Shouldn't have responded having read only half the post.

Try calling stream_get_meta_data() on the resource returned by fopen
and see what it contains. Often times an application would immediately
redirect to a GET page after a POST. If you redirect the user to that
link, then it'd work.

You can redirect a POST using the HTTP status code 307. Non-IE browsers
would show an alert box however.

Aug 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.