Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP POST requests in PHP

Tags:

http

post

php

How do you go about redirecting a browser and sending a HTTP POST request in PHP? A header("Location: file.php?foo=bar") of HTTP POST requests, if you will.

like image 695
Johannes Gorset Avatar asked Dec 10 '22 17:12

Johannes Gorset


2 Answers

You can't - HTTP does not allow this - if you want to pass arguments via a redirect they have to be embedded into the URL as GET vars.

C.

like image 147
symcbean Avatar answered Dec 13 '22 21:12

symcbean


This does not redirect the browser but it can perform a POST request.

Curl Manual

Curl POST Example

PHP POST Without Curl

To redirect the browser i'd suggest using Javascript.

An example form that does POST and redirect

 <FORM action="http://somesite.com/prog/adduser" method="post">
    <P>
    <LABEL for="firstname">First name: </LABEL>
              <INPUT type="text" id="firstname"><BR>
    <LABEL for="lastname">Last name: </LABEL>
              <INPUT type="text" id="lastname"><BR>
    <LABEL for="email">email: </LABEL>
              <INPUT type="text" id="email"><BR>
    <INPUT type="radio" name="sex" value="Male"> Male<BR>
    <INPUT type="radio" name="sex" value="Female"> Female<BR>
    <INPUT type="submit" value="Send"> <INPUT type="reset">
    </P>
 </FORM>
like image 25
Peter Lindqvist Avatar answered Dec 13 '22 23:12

Peter Lindqvist