Re: Improvements to HTTP stream metadata
| From: | Sean Coates | Date: | Fri, 01 Jul 2011 00:55:17 +0000 |
| Subject: | Re: Improvements to HTTP stream metadata | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-53714@lists.php.net to get a copy of this message | ||
> * Add a 'response_code' key, with the server response code as an integer
> …
> Anyone got any thoughts on this? I appreciate it'd be a BC break, but I think it might be
> useful.
While I applaud the idea of making this easier, keep in mind that a single request can have multiple
status code responses:
<?php
$s = fopen('http://gimmebar.com/',
'r');
$m = stream_get_meta_data($s);
var_dump(array_filter($m['wrapper_data'], function ($l) {
if (substr($l, 0, 5) === 'HTTP/') {
return true;
}
}));
Outputs:
array(2) {
[0]=>
string(18) "HTTP/1.1 302 Found"
[8]=>
string(15) "HTTP/1.1 200 OK"
}
There could also be a large number of "HTTP/1.1 100 Continue" headers in there, or more
300 series redirects.
Putting the headers into an associative array also has similar problems (there can be many headers
with the same name).
This can, of course, be worked around, but it's not as simple as just expecting a single
response code from a HTTP stream request.
Aside, but related: most code I'd written prior to ~6 months ago, and most of the example code
I've seen does something like:
list($prot, $status, $msg) = explode($m['wrapper_data'][0], 3);
…which is a nasty bug to track down.
S