@@ -25,6 +25,8 @@ class Client implements ClientInterface
2525 private $ requestDataLength ;
2626 private $ requestResponse ;
2727 private $ requestMeta ;
28+ private array $ responseHeaders = [];
29+ private string $ responseProtocolLine = '' ;
2830
2931 public function __construct (array $ options = [])
3032 {
@@ -133,6 +135,31 @@ protected function prepareRequest(RequestInterface $request): void
133135 if (!$ this ->hasOption (CURLOPT_TIMEOUT )) {
134136 $ this ->setOption (CURLOPT_TIMEOUT , static ::DEFAULT_TIMEOUT );
135137 }
138+
139+ $ this ->setOption (CURLOPT_HEADERFUNCTION , function ($ ch , string $ headerLine ): int {
140+ $ len = strlen ($ headerLine );
141+ $ line = trim ($ headerLine );
142+
143+ if ($ line === '' ) {
144+ return $ len ;
145+ }
146+
147+ // New header block (e.g., redirects): reset headers when we see a status line
148+ if (str_starts_with ($ line , 'HTTP/ ' )) {
149+ $ this ->responseHeaders = [];
150+ $ this ->responseProtocolLine = $ line ;
151+ return $ len ;
152+ }
153+
154+ $ parts = explode (': ' , $ line , 2 );
155+ if (count ($ parts ) === 2 ) {
156+ $ name = trim ($ parts [0 ]);
157+ $ value = trim ($ parts [1 ]);
158+ $ this ->responseHeaders [$ name ][] = $ value ; // preserve multi-value headers
159+ }
160+
161+ return $ len ;
162+ });
136163 }
137164
138165 protected function createResponse (): ResponseInterface
@@ -144,7 +171,24 @@ protected function createResponse(): ResponseInterface
144171 }
145172 $ stream ->seek (0 );
146173
147- return new Response ($ stream );
174+ $ status = (int ) ($ this ->requestMeta ['http_code ' ] ?? 0 );
175+
176+ $ response = new Response ($ stream );
177+
178+ $ response = $ response ->withStatus ($ status );
179+
180+ foreach ($ this ->responseHeaders as $ name => $ values ) {
181+ foreach ($ values as $ v ) {
182+ $ response = $ response ->withAddedHeader ($ name , $ v );
183+ }
184+ }
185+
186+ // Should I set protocol version?
187+ // cURL gives you HTTP version as an int; you can map it:
188+ // CURL_HTTP_VERSION_1_0, 1_1, 2_0, 3 etc.
189+ // $response = $response->withProtocolVersion('1.1');
190+
191+ return $ response ;
148192 }
149193
150194 /**
0 commit comments