views:

240

answers:

3

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

+1  A: 

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 ...

back2dos
-1. He's asking about getting headers when his script is an HTTP client.
outis
A: 

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

grantwparks
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?
Naumaan
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 requestfputs($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 soHttpComm::$headers = explode("\r\n", $sResponse);If you continue reading from this point you then it's the body of the response.
grantwparks
Sorry about the formatting. I looked under faq for code formatting
grantwparks
A: 

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'];
mrclay