Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3314458
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:14:06+00:00 2026-05-17T22:14:06+00:00

I’m writing a specialized PHP proxy and got stumped by a feature of cURL.

  • 0

I’m writing a specialized PHP proxy and got stumped by a feature of cURL.

If the following values are set:

curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

cURL correctly handles redirects, but returns ALL page headers, not just the final (non-redirect) page, e.g.

HTTP/1.1 302 Found
Location: http://otherpage
Set-Cookie: someCookie=foo
Content-Length: 198

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 3241

<!DOCTYPE HTML>
...rest of content

Note that CURLOPT_HEADER is set because I need to read and copy parts of the original header into my proxy header.

I appreciate why it’s returning all these headers (for example, my proxy code must detect any cookies set in the 302 header and pass them along). HOWEVER, it also makes it impossible to detect when the headers end and the content begins. Normally, with one header we could just do a simple split:

$split = preg_split('/\r\n\r\n/', $fullPage, 2)

But that obviously won’t work here. Hm. We could try something that only splits if it looks like the next line is part of a header:

$split = preg_split('/\r\n\r\nHTML\/(1\.0|1\.1) \\d+ \\w+/', $fullPage)
// matches patterns such a "\r\n\r\nHTML/1.1 302 Found"

Which will work almost all the time, but chokes if someone has the following in their page:

...and for all you readers out there, here is an example HTTP header:
<PRE>

HTTP/1.1 200 OK

BALLS!

We really want the split to stop matching as soon as it encounters any pattern of \r\n\r\n that isn’t immediately followed by HTML/1.x – is there a way to do this with PHP RegExs? Even this solution can choke on the (quite rare) situation where someone puts an HTTP header right at the beginning of their content. Is there a way in cURL to get all of the returned pages as an array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:14:07+00:00Added an answer on May 17, 2026 at 10:14 pm

    You can get the information of the total header size, and split the string up like this:

    $buffer = curl_exec($ch);
    $curl_info = curl_getinfo($ch);
    curl_close($ch);
    $header_size = $curl_info["header_size"];
    $header = substr($buffer, 0, $header_size);
    $body = substr($buffer, $header_size)
    

    Information taken from the helpful post by “grandpa”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.