-2

Can curl return an object that has the headers and body as separate properties?

Here is what I'm doing now. This returns a String(?) with the header first and then body after:

$session = curl_init($url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HEADER, true);
$response = curl_exec($session);
echo $response; // string

What I want is:

...
$response = curl_exec($session);
$header = $response->headers;
$body = $response->body;

I've seen a similar question that suggests requesting both the headers and body and then parsing the results. I'm not asking about that.

I'm specifically asking about returning an object with the body and headers as separate properties.

If the answer is "No, that's not possible" then I'll accept that answer. I'll also accept "No curl doesn't do that but here is another called swurl (or whatever) that does return an object..."

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • 1
    pure curl: no; a tiny bit more work on your part, yes. –  May 03 '17 at 21:28
  • 1
    Several ways in the duplicate, make sure to look at the first 4. – AbraCadaver May 03 '17 at 21:28
  • @AbraCadaver I read through that first. And my question is different. It is not a duplicate. – 1.21 gigawatts May 03 '17 at 21:42
  • Hi please reopen. I already read that question my question is not a duplicate – 1.21 gigawatts May 04 '17 at 01:26
  • @abracadaver I was very specific about what I needed. You've marked this a duplicate. It is not. Please reopen. – 1.21 gigawatts May 04 '17 at 01:29
  • @AbraCadaver You said, "Several ways in the duplicate". I said specifically there was one way that I required. This happens every other question. Someone comes along and marks it a duplicate without reading question and requirements. I specifically said I read that other question and that it did not meet my needs. Why would I create another question if that one had the answer I needed? Why would I waste my time if that had solved it? – 1.21 gigawatts May 04 '17 at 02:04
  • So the answer is no for returning an object with the body and headers as separate properties. – AbraCadaver May 04 '17 at 13:39

1 Answers1

2

Headers and body are separated by a blank line, you can can just split on that:

[$headers, $body] = explode("\r\n\r\n", $response, 2);

Object-oriented Enterprise version:

$response = new stdClass();
[$response->headers, $response->body] = explode("\r\n\r\n", curl_exec($session),  2);
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • 1
    he says he does not want that (for what ever reason) –  May 03 '17 at 21:28
  • 1
    @nogad This answer shows how trivial it is to separate headers and body. It is equally simple to create a stdClass object and set two properties. Two lines of code actually. That gives _exactly_ what the OP asks. – arkascha May 03 '17 at 21:30
  • @arkascha I totally agree i think the question is silly, but thats what the OP wants –  May 03 '17 at 21:31
  • @nogad Fixed. :) – Alex Howansky May 03 '17 at 21:31
  • @arkascha Ha ha we were thinking the same thing... – Alex Howansky May 03 '17 at 21:32
  • 1
    Exactly the two lines I had in mind :-) – arkascha May 03 '17 at 21:32
  • I appreciate the answer but my requirement is that it does not do any parsing. I know it is hard to understand but I have my reasons. – 1.21 gigawatts May 03 '17 at 21:45
  • 1
    @1.21gigawatts i don't see that requirement, just a passing mention that other answers do it like that but you don't want to (without explaining why). if you can't execute any of the code in this answer then explain why not? is there something that's actively stopping you when you do try it? if you just don't like using `stdClass` well the `explode` is more than enough for you to make your own wrapper class around `curl_*` functions or even inherit it and overload `curl_exec` to also to the explode – Memor-X May 04 '17 at 04:10
  • I have to make this call maybe thousands of times a second. I was hoping for an existing method that would return an object. For one that would remove the risk posed by parsing for line breaks (line breaks are different between mac, unix and Windows). Old servers may pose issues or someone could set a header with two line breaks in it and the parser would break. It would also, theoretically be more performant because it's not parsing a string looking for a value. It's unclear why it doesn't have a method to return an object yet. – 1.21 gigawatts May 04 '17 at 15:10
  • Long story short: reduce potential errors and may be more performant. All I wanted to know was if it existed. Then I could test my hypothesis. – 1.21 gigawatts May 04 '17 at 15:16
  • _"line breaks are different between mac, unix and Windows"_ No. RFC 7230 explicitly defines the break as CRLF. _"theoretically be more performing"_ Theoretically means you're guessing and you haven't actually tested it. Regardless, whether the split happens in PHP or Curl, it has to be done, so you pay the cost in both cases. You're wasting time with micro optimizations -- the time to make the actual HTTP query is going to be orders of magnitude greater than the time to run an `explode()` call on the result. – Alex Howansky May 04 '17 at 15:22