Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response-Header the right way #38

Open
gnanet opened this issue Feb 27, 2020 · 0 comments
Open

Response-Header the right way #38

gnanet opened this issue Feb 27, 2020 · 0 comments

Comments

@gnanet
Copy link

gnanet commented Feb 27, 2020

The method of splitting the response data by a double \r\n starting at line 175 to get the response-headers and data in one request is by far not ideal.

Since then, there was a bit evolution on this topic, so i want to link you to the
proper solution for cUrl to retrieve response headers AND body in a single request and based on it, i changed the code sample below to your variable namings:

$response_headers = [];
// this function is called by curl for each header received
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
  function($curl, $header) use (&$response_headers)
  {
    $len = strlen($header);
    $header = explode(':', $header, 2);
    if (count($header) < 2) // ignore invalid headers
      return $len;

    $headers[strtolower(trim($header[0]))][] = trim($header[1]);

    return $len;
  }
);

...

$response_content = curl_exec($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant