Page 1 of 1

Using header() to send POST data

Posted: Wed Jun 02, 2004 10:16 am
by mwaw
I can use this:

header("Location = mypage.php?first=Mike&last=Wilkinson");

to send data using URL encoding (GET). Is there any way to do something similar using POST?

Thanks,

Mike Wilkinson

Posted: Wed Jun 02, 2004 11:37 am
by launchcode
Yes, but it is easier to use a class such as Snoopy (if you don't have cURL support compiled into PHP) - you can get it from snoopy.sourceforge.net I believe.

Posted: Thu Jun 03, 2004 6:12 am
by dave420
Technically, there isn't a similar way. The location header tells the browser to go to the new address, whereas using curl simply turns the web server into a web client, which is something completely different. Javascript is the best way to achieve what you want, as you can render a form on your HTML document, then have it automatically POSTed to the URL you want. That, like the location header, tells your browser to request a new page from somewhere else, and specifies the data it needs to pass to the new server, instead of making the request itself and passing the output back to the browser.

Posted: Thu Jun 03, 2004 6:57 am
by launchcode
Dave - the "Location" Header function does indeed simply redirect a client, but "Location:" is only one of many different parts of a Header you can send - every time you POST data you just send an extended Header call. This is what a POST looks like and you can send it using PHP's Header function easily. Messing with Javascript is not needed (or curl unless you need SSL support)

Code: Select all

POST /snoopy/read.php HTTP/1.0
User-Agent: Snoopy v1.01
Host: sandbox.dev
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Content-type: application/x-www-form-urlencoded
Content-length: 28

v=atari%21&submit=Search%21&

Posted: Thu Jun 03, 2004 10:13 am
by JAM
I'm not sure I really understand what you mean by this:
launchcode wrote:... - every time you POST data you just send an extended Header call. This is what a POST looks like and you can send it using PHP's Header function easily. Messing with Javascript is not needed (or curl unless you need SSL support)
...but please feel free to comment further. For what i know, header() (raw, and basicly) is sendt from server to browser. POST is sendt from browser to server.

I cannot agree that messing with cURL is such a bad thing. You mention SSL as if it was a mandatory thing.

If cURL indeed is available, I'd go for that.

Posted: Thu Jun 03, 2004 10:25 am
by launchcode
No, Header is sent from BROWSER to SERVER - whether you do a GET, POST, PUT, Location, etc - it's always from the browser to the server. The server sends a response code back.

cURL is not required for such a simple operation as a POST request, why waste the processing time loading up those libraries, etc for it?

Posted: Thu Jun 03, 2004 11:12 am
by hedge
launchcode wrote:No, Header is sent from BROWSER to SERVER - whether you do a GET, POST, PUT, Location, etc - it's always from the browser to the server. The server sends a response code back.

cURL is not required for such a simple operation as a POST request, why waste the processing time loading up those libraries, etc for it?
Headers go both directions.... for example the server sends the browser a 'Set-Cookie' header or 'Cache-control' headers.

I have done some work with this using sockets, probably would have been easier to use snoopy or CURL but it may be useful to see what's happening. Here's the link viewtopic.php?t=7063

Posted: Fri Jun 04, 2004 4:22 am
by dave420
launchcode - I'm well aware of the structure of POSTed data. What I'm saying is that a POST has to be initiated by the browser, just like a GET. There is a header available to instruct the browser to GET a page (location), but there isn't one to make it POST to a page. That's what I'm saying. There is no "POST" equivalent of the "location" header.

Headers do indeed go both ways. HTTP traffic is defined as a header block, an empty line and body data. In a GET request there is no body, yet in a POST there is. When the server responds, the data is in the body of the request, and information about it is in the header.

If you want the server to download a page it has POSTed data to, then use curl. It's quite trivial to write the whole code yourself, but it can be slightly unnerving if you've not played with sockets before, or if you are unsure of HTTP.

Posted: Fri Jun 04, 2004 7:38 am
by launchcode
What I'm saying is that a POST has to be initiated by the browser, just like a GET. There is a header available to instruct the browser to GET a page (location), but there isn't one to make it POST to a page. That's what I'm saying. There is no "POST" equivalent of the "location" header.
You can initiate a POST request from a PHP script easily, why do you assume it has to be started by a browser? It doesn't. Equally it doesn't need to be done via curl or any sockets operation either - that's complete overkill for such a simple task, you can perform any POST request via the Header() functions, no matter how that script was initiated (called up via a browser, cron job, etc).

Location isn't a means to "get" a page, it just redirects a browser to it. The fact you can pass querystring values on the URL doesn't alter the fact that you issue a Location header which when it arrives at the page requested in turn issues a GET header. It doesn't do it all in one. Location is not GET.

Posted: Fri Jun 04, 2004 11:43 am
by dave420
Of course you can initiate a POST from PHP - I do it all the time. You can't initiate the browser to POST from PHP directly, though, which is what mwaw was asking.

For your information, 'location:' does cause the browser to GET a page. That's what a normal HTTP request is (regardless of the query string) - a GET request (you GET a page, hence the name).

You can't perform a POST from headers. You've already posted a POST request which demonstrates it isn't performed entirely in headers. It can only be initiated by a client acting as a browser, as it's a direct request to that server for something. PHP can act as the browser, but it can't directly tell the browser viewing its output to make a POST request to another server. For that, you need some extra trickery such as HTML/javascript.

I'd be happy to get you the RFC page for the HTTP 1.1 protocol if you still don't believe me :)

Posted: Fri Jun 04, 2004 12:26 pm
by hedge
You cannot use header to do a post. Header only sends headers back to the client... you can't post to a browser.

In theory you could if header let you target to the server but it doesn't, that's why you need to do it over a socket.

when you do a header('location: ?') call it sends a header to the browser that tells it to do a GET request with the paramaters you specified.

Posted: Fri Jun 04, 2004 12:56 pm
by launchcode
PHP can act as the browser, but it can't directly tell the browser viewing its output to make a POST request to another server.
Agreed - I wasn't questioning that, I was questioning your statement that said "What I'm saying is that a POST has to be initiated by the browser" - which as you just confirmed for yourself isn't the case. Without your follow-up post that statement didn't make sense, but now I can see where you were coming from.

Posted: Fri Jun 04, 2004 10:39 pm
by d3ad1ysp0rk
launchcode wrote: You can initiate a POST request from a PHP script easily, why do you assume it has to be started by a browser? It doesn't. Equally it doesn't need to be done via curl or any sockets operation either - that's complete overkill for such a simple task, you can perform any POST request via the Header() functions, no matter how that script was initiated (called up via a browser, cron job, etc).
Care to provide example code? I'm a bit confused on what you mean..

Posted: Mon Jun 07, 2004 5:40 am
by dave420
You can initiate a POST request from a PHP script easily
The only way you can initiate a POST request in the browser via PHP is by outputting an HTML form and the required javascript to submit the form (which isn't really PHP, but HTML :-P)[/quote]