问题
This code:
$headersSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$responseHeaders = substr($response, 0, $headersSize);
$responseBody = substr($response, $headersSize);
return incorrect size (strlen) of headers, e.g. here:
...
Pragma: no-cache
Set-Cookie: foo=bar; pat
---- curl want split here ----
h=/
Access-Control-Allow-Origin: *
<!DOCTYPE html ...
Response can have multiple headers (redirections, continue, etc.). And ofcourse like this isnt work:
list($header, $body) = explode("\r\n\r\n", $response, 2);
How to correct determine size of headers?
回答1:
Might be related to this: https://bugs.php.net/bug.php?id=63894&edit=1 or http://sourceforge.net/p/curl/bugs/1204
That fix was commit bc6037e which went into curl 7.30.0
Anyway, it's a bug. Any fix after this bug will be unreliable.
Knowing how it works, you can just manually add/subtract to the length of the header by a specific amount, just see how much it's missing.
Or if the proxy is used only for HTML, then you can manually parse the string until you get a line that starts with a HTML tag, which is where your header ends.
Sorry, but I don't see a reliable solution other than to fix this bug at the source. If you can, you can just update curl, libcurl or whatever. Or you can pull that version from git, fix this bug, recompile, if you're version-sensible.
来源:https://stackoverflow.com/questions/34743545/php-curl-incorrect-header-size-when-use-proxy