Page 1 of 1
POST Initiation with PHP header()
Posted: Tue Nov 30, 2004 9:01 am
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

Posted: Tue Nov 30, 2004 9:08 am
by protokol
One method is to use the [php_man]curl[/php_man]() functions. It works very well and is simple to do.
Posted: Tue Nov 30, 2004 9:34 am
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 ?
Posted: Tue Nov 30, 2004 9:45 am
by protokol
You could probably use sockets or something, but curl is the easiest way
Posted: Tue Nov 30, 2004 9:47 am
by protokol
I found this also. It's a PHP class that simulates a web browser.
http://sourceforge.net/projects/snoopy/
Posted: Tue Nov 30, 2004 10:18 am
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 !

Posted: Tue Nov 30, 2004 9:19 pm
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!
Posted: Tue Nov 30, 2004 11:24 pm
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
Posted: Wed Dec 01, 2004 12:18 am
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. ?

Posted: Wed Dec 01, 2004 1:01 am
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");
Posted: Wed Dec 01, 2004 5:08 am
by Hibba
How about a nice short and sweet example. That might help...
Posted: Wed Dec 01, 2004 5:49 am
by number8
That would be greate

Posted: Wed Dec 01, 2004 8:42 am
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.
Posted: Wed Dec 01, 2004 9:33 am
by number8
Your topic seems to be very usefull for me. Big big thanks hedge
