1

I've come across a dumbfounding issue with sending headers in PHP. I've spent about 45 minutes reading on SO and other sites and I can't come up with a legitimate reason for my problem.

I need to send a POST request to another server and I'm using the PHP header() function to set the values. I have sample code below.

    $server = 'http://fakedomain.com';
    $server_path = '/';
    $request = 'key=value&key2=value2';
    header("POST $server_path HTTP/1.1" );
    header("Host: $server\r\n" );
    header("Content-type: application/x-www-form-urlencoded\r\n" );
    header("Content-length: ".strlen($request)."\r\n" );
    header("Connection: close\r\n\r\n" );
    header($request);

I've tried a variety of options but each of them results in the same error in my log file

malformed header from script. Bad header=POST / HTTP/1.1: php5.cgi

I'm an experience PHP programmer who just hasn't had much need to manually send HTTP post requests.

I want the code to redirect the browser so that's why I decided to use this method.

Am I doing it right?

Is there some other way that is standard and I just don't know about?

5 Answers 5

11

header() sends a response header.

It sounds like you want to make a request, on the back end.

So you probably want to use curl to make the request.

If when you're done processing the response, you want to send some kind of header to the user agent (browser), then header() would be appropriate.

Sign up to request clarification or add additional context in comments.

1 Comment

It's nice when you can learn something new. Much appreciated.
1

The header functions relate to the headers returned to the client, I suggest you look into using cURL to do your post request: http://www.php.net/cURL

Comments

0

If you want to redirect user to another page, you should use

header("Location: http://domain.com/some.other.page?x=y");

If you want the user to send POST variables to the redirect page you will need to use JavaScript.

<html>
<head>
<title>Redirect</title>
</head>
<body onload="document.myform.submit()">
<form name="myform" action="http://domain.com/some.other.page" method="POST">
<input type="hidden" name="x" value="y">
</form>
</body>
</html>

Comments

0

Expanding on timdev's answer, here's the cURL you'd use:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.target.com/script.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2&postvar3=value3");
curl_exec ($ch);
curl_close ($ch); 
?>

Comments

0

I have tried simple html form posting it worked for me

    <?php
    if( $dev === 'sample1' || $dev === 'sample2' )  {

?>
    <form name="frmpostdata" action="mystatement.php" method="post">
        <input type="hidden" name="cmsReq" value="MobApp" />
        <input type="hidden" name="cardno" value="<?php echo $chkTkn['cardno'];?>" />
        <input type="hidden" name="cardPwd" value="<?php echo $chkTkn['pwd'];?>" />
        <input type="submit" style="display:none;" name="Submit" value="" />
    </form>
    <script>
        document.frmpostdata.submit();
    </script>
<?php   
        exit;
    }
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.