3

How do I obtain the HTTP headers in response to a POST request I made with PHP?

6
  • Well...I think you should give a little more details. Code i.e. Commented Jul 18, 2009 at 21:42
  • I have a POST request to send to a second page and I wish to send the request and obtain the response headers of the second page in variables form somehow. This is how I have learnt it is done with CURL it.toolbox.com/wiki/index.php/… I was looking for a simpler method Commented Jul 18, 2009 at 21:47
  • it seems like that class already abstracts the curl stuff away pretty nicely. what's the problem with using it? Commented Jul 18, 2009 at 21:58
  • the problem is that I am behind a proxy which has http authentication. it does not support that directly and I do not know CURL to change the script Commented Jul 18, 2009 at 22:09
  • Arrgh. I wish I could edit this question. Commented Jul 18, 2009 at 22:43

3 Answers 3

2

Create an HTTP stream context and pass it into file_get_contents(). Afterwards, you can:

$metaData = stream_get_meta_data($context);
$headers = $metaData['wrapper_data'];
Sign up to request clarification or add additional context in comments.

Comments

1

you will find them in the superglobal $_SERVER ... anything that starts with HTTP_ should be a header ... it depends on the server, how well this will work ...

greetz

back2dos

1 Comment

-1. He's asking about getting headers when his script is an HTTP client.
0

How did you issue the POST? I use a socket and then read back the response where I get headers and body.

3 Comments

i want to use a socket but I don't know how exactly to code it. From my C++ knowledge, I know I can do this job easily if I create a socket but how do I go about it in PHP?
I plucked this out of an HTTP class I wrote, so all the niceties - error handling, etc - are missing, but I think it shows the basic steps. $fp = @fsockopen(HttpComm::$host, HttpComm::$port, $iErrno, $sErrStr, (int)self::$iTimeoutConnectionSecs); // send request fputs($fp, $sHttpString, strlen($sHttpString)); // this will get the string of headers $sResponse = stream_get_line($fp, 1024, "\r\n\r\n"); // I break the HTTP response headers apart like so HttpComm::$headers = explode("\r\n", $sResponse); If you continue reading from this point you then it's the body of the response.
Sorry about the formatting. I looked under faq for code formatting

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.