# Improvements to HTTP stream metadata

- Thread: https://externals.io/message/53708
- Original archive: https://news-web.php.net/php.internals/53708
- Started by Michael Maclean on 2011-06-30T20:44:05+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #53708 — Michael Maclean — 2011-06-30T20:44:05+00:00

URL: https://externals.io/message/53708#53708

Hi,
Currently, if you open an HTTP stream using `fopen()`, and then call 
 `stream_get_meta_data()` on it, you obtain an associative array within 
which as another with the key 'wrapper_data'. Inside that, there is a 
list of the HTTP headers from the server response. I'd like to make this 
a little more useful as currently I don't believe there's a way to get 
the response code from the remote end without parsing those headers 
yourself. While this isn't hard, I'd like to make the following changes:

* move the current contents of the wrapper_data array to a key called 
'headers' within wrapper_data;
* Add a 'response_code' key, with the server response code as an integer
* Optionally, add things like the mime type of the response.

Anyone got any thoughts on this? I appreciate it'd be a BC break, but I 
think it might be useful.

-- 
Cheers,
Michael

---

## #53709 — Stas Malyshev — 2011-06-30T21:29:41+00:00

URL: https://externals.io/message/53708#53709 · In reply to: #53708

Hi!


> Hi,
> Currently, if you open an HTTP stream using `fopen()`, and then call
> `stream_get_meta_data()` on it, you obtain an associative array within
> which as another with the key 'wrapper_data'. Inside that, there is a
> list of the HTTP headers from the server response. I'd like to make this
> a little more useful as currently I don't believe there's a way to get
> the response code from the remote end without parsing those headers
> yourself. While this isn't hard, I'd like to make the following changes:
>
> * move the current contents of the wrapper_data array to a key called
> 'headers' within wrapper_data;

This means breaking every script in existence that uses this data now. 
Probably not a good idea. Maybe we should think about a BC way to do the 
same.

> * Add a 'response_code' key, with the server response code as an integer
> * Optionally, add things like the mime type of the response.
>
> Anyone got any thoughts on this? I appreciate it'd be a BC break, but I
> think it might be useful.
>


-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #53710 — Michael Maclean — 2011-06-30T21:34:20+00:00

URL: https://externals.io/message/53708#53710 · In reply to: #53709

> Hi!
>
> 
> > […]
>
> This means breaking every script in existence that uses this data now.
> Probably not a good idea. Maybe we should think about a BC way to do the
> same.

Yeah; I'm open to suggestions on that front. I couldn't really think of 
another way to do it.

The same data also ends up in the bizarre $http_response_headers var 
that gets spontaneously created in local scope - I've wondered about how 
good that is to do.

-- 
Cheers,
Michael

---

## #53712 — Stas Malyshev — 2011-06-30T23:29:13+00:00

URL: https://externals.io/message/53708#53712 · In reply to: #53710

Hi!


> The same data also ends up in the bizarre $http_response_headers var
> that gets spontaneously created in local scope - I've wondered about how
> good that is to do.

This thing is indeed bizzare. I wonder if anybody uses it and why it was 
done this way...
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #53713 — Tjerk Meesters — 2011-07-01T00:02:47+00:00

URL: https://externals.io/message/53708#53713 · In reply to: #53712

>
> Hi!
>
>
> 
> > […]
>
>
> This thing is indeed bizzare. I wonder if anybody uses it and why it was
done this way...

I use this, mainly because I don't always have the file handle after calling
a function that does network requests. Who doesn't love a bit of 'magic' ;)

>
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227

---

## #53714 — Sean Coates — 2011-07-01T00:55:17+00:00

URL: https://externals.io/message/53708#53714 · In reply to: #53708

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

---

## #53716 — david.zuelke@bitextender.com — 2011-07-01T08:18:02+00:00

URL: https://externals.io/message/53708#53716 · In reply to: #53712

> Hi!
> 
> 
> > […]
> 
> This thing is indeed bizzare. I wonder if anybody uses it and why it was done this way...

I've used it before... IIRC, it was because that var gets set on older PHP versions even if the HTTP response is an error (in which case the stream is destroyed and you can't get the metadata anymore either; newer versions have the "ignore_errors" context option for that).

David

---

## #53717 — Hannes Magnusson — 2011-07-01T10:08:10+00:00

URL: https://externals.io/message/53708#53717 · In reply to: #53716

> 
>
> > […]
>
> I've used it before... IIRC, it was because that var gets set on older PHP versions even if the HTTP response is an error (in which case the stream is destroyed and you can't get the metadata anymore either; newer versions have the "ignore_errors" context option for that).
>

I use it extensively. Its nearly the only way to get the headers from
 `file_get_contents()` for example.

-Hannes

---

## #53718 — Stas Malyshev — 2011-07-01T16:51:24+00:00

URL: https://externals.io/message/53708#53718 · In reply to: #53717

Hi!


> I use it extensively. Its nearly the only way to get the headers from
> `file_get_contents()` for example.

We could have a function, like stream_get_last_headers() or something 
like that (like all DBs do, for example, which have exactly the same 
issue with no handle on error). Injecting random variables in scope is 
not usually the best way...
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
