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.Martin Thurau– Martin Thurau2009-07-18 21:42:16 +00:00Commented 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 methodNaumaan– Naumaan2009-07-18 21:47:21 +00:00Commented 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?nategood– nategood2009-07-18 21:58:04 +00:00Commented 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 scriptNaumaan– Naumaan2009-07-18 22:09:52 +00:00Commented Jul 18, 2009 at 22:09
-
Arrgh. I wish I could edit this question.jason– jason2009-07-18 22:43:18 +00:00Commented Jul 18, 2009 at 22:43
|
Show 1 more comment
3 Answers
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'];
Comments
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
outis
-1. He's asking about getting headers when his script is an HTTP client.
How did you issue the POST? I use a socket and then read back the response where I get headers and body.
3 Comments
Naumaan
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?
grantwparks
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.
grantwparks
Sorry about the formatting. I looked under faq for code formatting