1

I am writting a class which communicates with a server through sockets and sends HTTP request.

I am using http_parse_headers function from pecl_http, but I didn't install it, so I've written a workaround. There are many on the documentation page. They all returns the HTTP status code as well (as a status key, or a 0 key). What I would like to know: does the original function returns it somehow? I would like to clone the functionality, so if anyone has this extension installed, the original function can be used.

1 Answer 1

1

According to the PHP documentation, the only possible return values are:

  • An array (containing the parsed headers)
  • or FALSE (on failure).

Unless the HTTP status code is sent along with the headers, you won't be able to retrieve it by using the http_parse_headers() function.

If however the status code is sent along in the headers, it may look like this:

"HTTP/1.0 404 Not Found"

It is therefore just a matter of interpretation (in the function you actually use) whether the status will be properly parsed and listed in the array of results or not.

In any case you can refer to this function that was posted on php.net, which mimicks the behaviour of http_parse_headers() but which returns the HTTP status as the first key.

Sign up to request clarification or add additional context in comments.

7 Comments

I've read the documentation and the examples. Let's say, I pass the HTTP StatusLine as well. I would like to know whether it is an undocumented feature and I GET the status as well, or it is just the commenter's bad concept of cloning functionality?
http_response_code won't work, as the communication itself is done through socket
Thanks for your help. I already thought about your points. I would like to exactly clone the function in the extension, because if I don't, it will mess up with the extension installed. If the status line/status code is NOT returned in the original function than the authors of those comments made a really big mistake, as their code won't run the same way in other environments, and it is a bad concept.
It seems so. I honestly cannot say for sure, as i have never used http_parse_headers().
Well, I think this is enough for me to think, that the answer is no. I just needed someone else's thoughts about this. :) Thanks
|

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.