Using header() to send POST data
Moderator: General Moderators
Using header() to send POST data
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
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
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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.
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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&I'm not sure I really understand what you mean by this:
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.
...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.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)
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.
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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?
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.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?
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
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.
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.
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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).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.
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.
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
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
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.
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.
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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.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.
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
Care to provide example code? I'm a bit confused on what you mean..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).