Retrieving response headers AND body in a single request

  1. I want to separate curl response header from body, I did google and found this

I want it to work with HTTP/1.1 100 Continue before other headers, so is below correct?

$parts = explode("\r\n\r\nHTTP/", $response, 2);
$parts = (count($parts) > 1 ? 'HTTP/' : '').array_pop($parts);
list($headers, $body) = explode("\r\n\r\n", $parts, 2);

or should I remove 3rd parameter of explode from from first line and it should be line below?

$parts = explode("\r\n\r\nHTTP/", $response);

I guess cURL sends Expect: 100 Continue when sending the request with post method, and this header is not in response header? am I right?

Follow the answer to that thread, to establish what $headers should be.

preg_match($headers,"#HTTP/[\d\.]+ (\d+) (.*)#",$matches)

$matches[1] then contains the status code number that was returned, and $matches[2] contains the string that accompanied it.

I want to just separate header with body, I don’t care with status code. please advice which of the codes I gave in first post is correct? both of them are the answers to that thread, which one is correct?

The one with the check next to it?

Actually I am not using cURL, I am using socket! if socket is the only option to go then how to split header from body?

why would you use a socket to retrieve HTML?

sorry, I meant fsockopen, not socket, fsockopen has nothing to do with sockets extension. why would I use? just to learn php.
I see fsockopen is completely independent from sockets extension and allow_url_fopen, so how to disable it (not using disable_funstions directive) or what is its limitation? will it work always?


(PHP 4, PHP 5)
fsockopen — Open Internet or Unix domain socket connection

nimasdj it is great that you want to learn/ :thumbsup:

Two requests
Please become more familiar with the PHP documentation if you’re not already.
Please don’t “change course mis-stream” as it were. At least not uannouced.

If you have given up on curl for this. Posting why it can’t be used would be helpful to others.
And a separate discussion for the different approach would be less confusing to others.

allow_url_fopen defaults to 1 (on). So… file_get_contents() and then check $http_response_header.

or cURL.

Or… any of the tools actually written to handle HTTP requests.

Trying to force your way through sockets is adding 10 layers of complexity to a one-function process, hence why we discourage its use.

I see the word ‘socket’ in the phrase

Open Internet or Unix domain socket connection

confused you and this is why you thought I did not read the php documentation. If you use print_r(get_extension_funcs('standard')); you’ll see fsockopen is a standard function and has nothing to do with sockets extension, for sure you can disable the extension and you’ll see this works still.

Tough the discussion became off-topic, I asked if cURL had no feature to separate header from body like discussed in that stackoverflow post, then which one of the two codes I gave in first post is correct?

And you were told; neither.

As I said in post #4. The one with the checkmark next to it:

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

And you were told if you had not curl and you were forced to use fsockopen anyway, which one of these 2 codes you would use? The code you gave is for curl only.
Anyway, he answered this:

You’re… using a CURL thread in stackoverflow to answer yourself about NOT using curl.

Good luck on that, then.

If i was forced to use fsockopen, then i would frankly have told whoever was giving me the assignment that their server needs reconfiguring to allow file_get_contents or curl. Because fsockopen is a ridiculous way of single-loading a HTML page.

Essentially, what your question is asking is: “If you had a car, but the breaks were cut, what’s the correct way to jam the break pedal through the floor to stop the vehicle.”

Which one of the codes work with cURL? Neither reliably, as you have read.

file_get_contents still depends on allow_url_fopen, what will you do if this is off? fsockopen depends on nothing! so noway to deny it. or am I wrong about limiting fsockopen?

Consider this:
You first check for curl if available do your job, if curl is not available then you switch to fsockopen, if fsockopen is not available then switch to file_get_contents if allow_url_fopen is on, otherwise you throw exception telling your user that their server is garbage!

  1. Is this a good priority to switch to what is available? or do you replace the priority between file_get_contents and fsockopen?
  2. Why fsockopen is ridiculous? otherwise what is its purpose at all?

I first check curl; a PHP extension in wide use around the world.
I second check allow_url_fopen, a DEFAULT ON configuration setting in the core of PHP.
If both of these are disabled, then the server owner is intentionally blocking attempts to access external HTML requests, as he has changed PHP’s default configuration. So the next stop is to find out why. If he’s gone to the steps of blocking url_fopen and disabling the curl extension, opening fsocks in his face isnt likely to make him happy.

Also, if the server owner has gone to the lengths of changing the INI to prevent external URL’s being opened, he’s probably disabled fsockopen as a function (yes, you can disable functions in the INI. Any function.)

You have reason, priority of file_get_contents must be higher than fsockopen as it is faster:


But I don’t think we should ignore fsockopen as 3rd option, this is just an option why not using it? As about disabling functions in php.ini I did not mean disable_functions directive but something else, something like allow_url_fopen or so, is it possible to block fsockopen in any other way than disable_functions?

Well if you ignore the ability to disable it, then no, there’s no other way to disable it.

Like if you ignore the ability to set allow_url_fopen to 0 that you wouldnt be able to disable file_get_contents.
Or if you ignore the ability to install PHP without curl you wouldnt be able to disable curl.

If you really, absolutely, feel the need to manually fsock read a HTML response, go for it. I just dont see a need to push your car to the shop instead of driving it.

I was googling how to send files, for cURL I found this
For file_get_contents found no ready to use lib but I think I should create a multipart boundary with the stream, right?

Is there any ready to use tool or lib to pass messages with files and it returns them as a multipart boundary?