@@ -178,8 +178,38 @@ private function getCurlCommand(array $trace): ?string
178
178
return null ;
179
179
}
180
180
181
+ $ scheme = parse_url ($ trace ['url ' ], \PHP_URL_SCHEME ) ?? parse_url ($ trace ['options ' ]['base_uri ' ], \PHP_URL_SCHEME );
182
+ $ host = $ request = $ path = null ;
183
+ $ headers = [];
184
+
181
185
$ debug = explode ("\n" , $ trace ['info ' ]['debug ' ]);
182
- $ url = self ::mergeQueryString ($ trace ['url ' ], $ trace ['options ' ]['query ' ] ?? [], true );
186
+ foreach ($ debug as $ line ) {
187
+ $ line = substr ($ line , 0 , -1 );
188
+
189
+ if (str_starts_with ('< ' , $ line )) {
190
+ // End of the request, beginning of the response. Stop parsing.
191
+ break ;
192
+ }
193
+
194
+ if ('' === $ line || str_starts_with ($ line , '* ' )) {
195
+ continue ;
196
+ }
197
+
198
+ if (preg_match ('/Host: (.+)/ ' , $ line , $ match )) {
199
+ $ host = $ match [1 ];
200
+ continue ;
201
+ }
202
+
203
+ if (preg_match ('/^> ([A-Z]+) ([^ ]+)/ ' , $ line , $ match )) {
204
+ [, $ request , $ path ] = $ match ;
205
+ continue ;
206
+ }
207
+
208
+ $ headers [] = $ line ;
209
+ }
210
+
211
+ $ url = $ scheme .':// ' .$ host .$ path ;
212
+
183
213
$ command = ['curl ' , '--compressed ' ];
184
214
185
215
if (isset ($ trace ['options ' ]['resolve ' ])) {
@@ -191,6 +221,13 @@ private function getCurlCommand(array $trace): ?string
191
221
}
192
222
}
193
223
224
+ $ command [] = sprintf ('--request %s ' , $ request );
225
+ $ command [] = sprintf ('--url %s ' , escapeshellarg ($ url ));
226
+
227
+ foreach ($ headers as $ header ) {
228
+ $ command [] = '--header ' .escapeshellarg ($ header );
229
+ }
230
+
194
231
$ dataArg = [];
195
232
196
233
if ($ json = $ trace ['options ' ]['json ' ] ?? null ) {
@@ -199,7 +236,7 @@ private function getCurlCommand(array $trace): ?string
199
236
if (\is_string ($ body )) {
200
237
try {
201
238
$ dataArg [] = '--data ' .escapeshellarg ($ body );
202
- } catch (\ValueError $ e ) {
239
+ } catch (\ValueError ) {
203
240
return null ;
204
241
}
205
242
} elseif (\is_array ($ body )) {
@@ -214,27 +251,6 @@ private function getCurlCommand(array $trace): ?string
214
251
215
252
$ dataArg = empty ($ dataArg ) ? null : implode (' ' , $ dataArg );
216
253
217
- foreach ($ debug as $ line ) {
218
- $ line = substr ($ line , 0 , -1 );
219
-
220
- if (str_starts_with ('< ' , $ line )) {
221
- // End of the request, beginning of the response. Stop parsing.
222
- break ;
223
- }
224
-
225
- if ('' === $ line || preg_match ('/^[*<]|(Host: )/ ' , $ line )) {
226
- continue ;
227
- }
228
-
229
- if (preg_match ('/^> ([A-Z]+)/ ' , $ line , $ match )) {
230
- $ command [] = sprintf ('--request %s ' , $ match [1 ]);
231
- $ command [] = sprintf ('--url %s ' , escapeshellarg ($ url ));
232
- continue ;
233
- }
234
-
235
- $ command [] = '--header ' .escapeshellarg ($ line );
236
- }
237
-
238
254
if (null !== $ dataArg ) {
239
255
$ command [] = $ dataArg ;
240
256
}
0 commit comments