POST Initiation with PHP header()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
number8
Forum Newbie
Posts: 7
Joined: Tue Nov 30, 2004 8:51 am
Location: VirtualCity, Neural Networks str. 11

POST Initiation with PHP header()

Post by number8 »

Hello,

Here is a problem:

To redirect a link we could use this:

Code: Select all

header("Location: http:/www.algirdas.com/index.html?merchant_account_id=223");


How to do same thing with POST method, like form posting ?

I tried this but it does not work:

Code: Select all

header("POST /index.html HTTP/1.1");
header("Host: www.algirdas.com");
header("User-Agent: Mozilla/4.0");
header("Content-Length: 23");
header("Content-Type: application/x-www-form-urlencoded");
print("merchant_account_id=223");
Have you any ideas how to handle this ? Thank you :)
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

One method is to use the [php_man]curl[/php_man]() functions. It works very well and is simple to do.
number8
Forum Newbie
Posts: 7
Joined: Tue Nov 30, 2004 8:51 am
Location: VirtualCity, Neural Networks str. 11

Post by number8 »

Thank you protokol for fast reply :)

Hmmm... but that's need's additional Library to be installed. Any ideas how to code such thing with standard PHP functionality ?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

You could probably use sockets or something, but curl is the easiest way
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

I found this also. It's a PHP class that simulates a web browser. http://sourceforge.net/projects/snoopy/
number8
Forum Newbie
Posts: 7
Joined: Tue Nov 30, 2004 8:51 am
Location: VirtualCity, Neural Networks str. 11

Post by number8 »

I found this one:

Code: Select all

<?php
/* Send POST request to https://secure.example.com/form_action.php
 * Include form elements named "foo" and "bar" with dummy values
 */

$sock = fsockopen("ssl://secure.example.com", 443, $errno, $errstr, 30);
if (!$sock) die("$errstr ($errno)\n");

$data = "foo=" . urlencode("Value for Foo") . "&bar=" . urlencode("Value for Bar");

fwrite($sock, "POST /form_action.php HTTP/1.0\r\n");
fwrite($sock, "Host: secure.example.com\r\n");
fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
fwrite($sock, "Accept: */*\r\n");
fwrite($sock, "\r\n");
fwrite($sock, "$data\r\n");
fwrite($sock, "\r\n");

$headers = "";
while ($str = trim(fgets($sock, 4096)))
  $headers .= "$str\n";

echo "\n";

$body = "";
while (!feof($sock))
  $body .= fgets($sock, 4096);

fclose($sock);
?>
I hope this will help. Thanks protocol. I enjoyed your help ! :)
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

I am doing a similiar thing on my site..

When you use the fsockopen, or even that snoopy class, does this send your browser (the user) directly to that URL?

thanks!
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Hibba wrote:I am doing a similiar thing on my site..

When you use the fsockopen, or even that snoopy class, does this send your browser (the user) directly to that URL?

thanks!
im not familiar w/ snoopy, but fsockopn() doesnt. you can set the user agent string using ini_set though if you want it to. and by doing that, you could pass your user_agent into ini_set so php will use whatever browsers user agent that is running the script
number8
Forum Newbie
Posts: 7
Joined: Tue Nov 30, 2004 8:51 am
Location: VirtualCity, Neural Networks str. 11

Post by number8 »

rehfeld wrote:im not familiar w/ snoopy, but fsockopn() doesnt. you can set the user agent string using ini_set though if you want it to. and by doing that, you could pass your user_agent into ini_set so php will use whatever browsers user agent that is running the script
Yes, I still looking for solution how to send users browser to that URL with these POST parameters. Do you mean I must set user_agent setting in php.ini to value retrieved throught get_browser() function or $_SERVER['HTTP_USER_AGENT'] variable etc. ? :?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

my mistake. if using fopen() or file_get_contents() you can set it it using ini_set


but if your using fsockopen(), its even easier

just add this to your code above

Code: Select all

fwrite($sock, "User-Agent: ".$_SERVER['HTTP_USER_AGENT']."\r\n");
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

How about a nice short and sweet example. That might help...
number8
Forum Newbie
Posts: 7
Joined: Tue Nov 30, 2004 8:51 am
Location: VirtualCity, Neural Networks str. 11

Post by number8 »

That would be greate :wink:
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

I'm almost positive the user-agent isn't useful to redirect them. I believe that header is just sent to the web server receiving the post to identify the client, you can send any header you want.

How to do the redirect is dependent on what the site returns back from the post. It could be a auto-generated confirm page, a header-redirect, lots of different options. If it's a login it would probably also contain set-cookie headers that would have to be sent back to the browser.

With the fsock option (I'd guess that snoopy is the same) the script is acting like the browser, the only thing the real browser knows about is what your script sends it.

I have done a bit of work with this stuff as well, you may find this link useful viewtopic.php?t=7063

PS. it's much easer to use http 1.0, http 1.1 allows for a chunked-response that is more difficult to retrieve.
number8
Forum Newbie
Posts: 7
Joined: Tue Nov 30, 2004 8:51 am
Location: VirtualCity, Neural Networks str. 11

Post by number8 »

Your topic seems to be very usefull for me. Big big thanks hedge :D
Post Reply