Page 1 of 1

HTTP Header Post

Posted: Sat May 16, 2009 5:50 pm
by david64
Does anyone know how to redirect a page with POST variables using the header function?

I have found a few references to methods like this:

Code: Select all

<?php
$host = "www.example.com";
$path = "/path/to/script.php";
$data = "data1=value1&data2=value2";
$data = urlencode($data);
 
header("POST $path HTTP/1.1\r\n" );
header("Host: $host\r\n" );
header("Content-type: application/x-www-form-urlencoded\r\n" );
header("Content-length: " . strlen($data) . "\r\n" );
header("Connection: close\r\n\r\n" );
header($data);
?>
but all I am getting is my browser telling me to download a file.

If anyone could try running that on their system I would be interested to know what the response is. There may be some system issues.

Re: HTTP Header Post

Posted: Sat May 16, 2009 6:03 pm
by ldougherty
I copied your code to my server, got the same result with a popup asking me to save the PHP file.

This may be helpful..

Using PHP, how do I send a http POST without using a form?
http://www.faqts.com/knowledge_base/vie ... 039/fid/51

Re: HTTP Header Post

Posted: Sat May 16, 2009 6:25 pm
by Benjamin
Use cURL. Here's an example I commented for you.

Code: Select all

 
<?php
 
# set the post fields and values
$data = array(
    'foo' => 'bar',
    'bar' => 'foo',
);
 
# create the post string
$post_fields = '';
foreach ($data as $k => $v) $post_fields .= "$k=" . urlencode($v) . '&';
 
# initialize curl
$ch = curl_init('http://example.com/page.php');
 
# don't return the header
curl_setopt($ch, CURLOPT_HEADER, 0);
 
# return the resulting data rather than displaying it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
# tell curl we are posting data
curl_setopt($ch, CURLOPT_POST, 1);
 
# set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
 
# don't validate ssl certs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 
# execute and get the response
$response = curl_exec($ch);
 
# close curl
curl_close($ch);
 
# echo response
echo $response;
 
See also: http://us3.php.net/manual/en/book.curl.php

Re: HTTP Header Post

Posted: Sun May 17, 2009 3:48 pm
by david64
Thanks ldougherty and astions. However, I am very familiar with cURL. I don't want to download the actual page and save it as a variable. I want to forward the browser to the actual page, just like I was doing:

Code: Select all

<?php
header( 'Location: http://php.net/' );
?>
However, I also need to get a few POST variables in the mix.

Re: HTTP Header Post

Posted: Tue May 19, 2009 4:00 pm
by Darhazer
The headers are what you send to the browser, not to the page you are going to redirect to.
I don't know other way to redirect with POST then to display a form and to immediately submit it