php-curl incorrect header size when use proxy

喜欢而已 提交于 2020-01-06 02:59:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!