Retrieving response headers AND body in a single request

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?

Errrm.

http://php.net/manual/en/function.fsockopen.php

(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.

Mittineague,
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: http://stackoverflow.com/questions/9183178/php-curl-retrieving-response-headers-and-body-in-a-single-request/17971689?noredirect=1#comment52197279_17971689

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:
socket:
0.0950679779053
0.52462220192
0.256870985031
0.264568090439
0.104283094406

file_get_contents:
0.240982055664
0.138034105301
0.151926040649
0.0816640853882
0.134870052338

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 https://github.com/phplicengine/phplicengine-api/blob/master/lib/PHPLicengine/Api/FileUpload.php
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?

no such tool or lib?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.