@@ -16,7 +16,7 @@ final class CurlDispatcher
16
16
private RequestInterface $ request ;
17
17
private $ curl ;
18
18
private array $ headers = [];
19
- private bool $ multi = false ;
19
+ private $ body ;
20
20
private ?int $ error = null ;
21
21
22
22
/**
@@ -28,7 +28,7 @@ public static function fetch(?ResponseFactoryInterface $responseFactory, Request
28
28
29
29
if (count ($ requests ) === 1 ) {
30
30
$ connection = new static ($ requests [0 ]);
31
- return [$ connection ($ responseFactory )];
31
+ return [$ connection-> exec ($ responseFactory )];
32
32
}
33
33
34
34
//Init connections
@@ -37,7 +37,6 @@ public static function fetch(?ResponseFactoryInterface $responseFactory, Request
37
37
38
38
foreach ($ requests as $ request ) {
39
39
$ connection = new static ($ request );
40
- $ connection ->multi = true ;
41
40
curl_multi_add_handle ($ multi , $ connection ->curl );
42
41
43
42
$ connections [] = $ connection ;
@@ -72,7 +71,7 @@ public static function fetch(?ResponseFactoryInterface $responseFactory, Request
72
71
curl_multi_close ($ multi );
73
72
74
73
return array_map (
75
- fn ($ connection ) => $ connection ($ responseFactory ),
74
+ fn ($ connection ) => $ connection-> exec ($ responseFactory ),
76
75
$ connections
77
76
);
78
77
}
@@ -101,33 +100,14 @@ private function __construct(RequestInterface $request)
101
100
CURLOPT_USERAGENT => $ request ->getHeaderLine ('User-Agent ' ),
102
101
CURLOPT_COOKIEJAR => $ cookies ,
103
102
CURLOPT_COOKIEFILE => $ cookies ,
103
+ CURLOPT_HEADERFUNCTION => [$ this , 'writeHeader ' ],
104
+ CURLOPT_WRITEFUNCTION => [$ this , 'writeBody ' ],
104
105
]);
105
-
106
- curl_setopt (
107
- $ this ->curl ,
108
- CURLOPT_HEADERFUNCTION ,
109
- function ($ resource , $ string ) {
110
- if (preg_match ('/^([\w-]+):(.*)$/ ' , $ string , $ matches )) {
111
- $ name = strtolower ($ matches [1 ]);
112
- $ value = trim ($ matches [2 ]);
113
- $ this ->headers [] = [$ name , $ value ];
114
- } elseif ($ this ->headers ) {
115
- $ key = array_key_last ($ this ->headers );
116
- $ this ->headers [$ key ][1 ] .= ' ' .trim ($ string );
117
- }
118
-
119
- return strlen ($ string );
120
- }
121
- );
122
106
}
123
107
124
- public function __invoke (ResponseFactoryInterface $ responseFactory ): ResponseInterface
108
+ private function exec (ResponseFactoryInterface $ responseFactory ): ResponseInterface
125
109
{
126
- if ($ this ->multi ) {
127
- $ body = curl_multi_getcontent ($ this ->curl );
128
- } else {
129
- $ body = curl_exec ($ this ->curl );
130
- }
110
+ curl_exec ($ this ->curl );
131
111
132
112
$ info = curl_getinfo ($ this ->curl );
133
113
@@ -148,11 +128,14 @@ public function __invoke(ResponseFactoryInterface $responseFactory): ResponseInt
148
128
$ response = $ response ->withAddedHeader ($ name , $ value );
149
129
}
150
130
151
- if (! $ response-> hasHeader ( ' Content-Location ' )) {
152
- $ response = $ response -> withHeader ('Content-Location ' , $ info ['url ' ]);
153
- }
131
+ $ response = $ response
132
+ -> withAddedHeader ('Content-Location ' , $ info ['url ' ])
133
+ -> withAddedHeader ( ' X-Request-Time ' , sprintf ( ' %.3f ms ' , $ info [ ' total_time ' ]));
154
134
155
- $ response ->getBody ()->write ($ body );
135
+ if ($ this ->body ) {
136
+ //5Mb max
137
+ $ response ->getBody ()->write (stream_get_contents ($ this ->body , 5000000 , 0 ));
138
+ }
156
139
157
140
return $ response ;
158
141
}
@@ -177,4 +160,27 @@ private function getRequestHeaders(): array
177
160
178
161
return $ headers ;
179
162
}
163
+
164
+ private function writeHeader ($ curl , $ string ): int
165
+ {
166
+ if (preg_match ('/^([\w-]+):(.*)$/ ' , $ string , $ matches )) {
167
+ $ name = strtolower ($ matches [1 ]);
168
+ $ value = trim ($ matches [2 ]);
169
+ $ this ->headers [] = [$ name , $ value ];
170
+ } elseif ($ this ->headers ) {
171
+ $ key = array_key_last ($ this ->headers );
172
+ $ this ->headers [$ key ][1 ] .= ' ' .trim ($ string );
173
+ }
174
+
175
+ return strlen ($ string );
176
+ }
177
+
178
+ private function writeBody ($ curl , $ string ): int
179
+ {
180
+ if (!$ this ->body ) {
181
+ $ this ->body = fopen ('php://temp ' , 'w+ ' );
182
+ }
183
+
184
+ return fwrite ($ this ->body , $ string );
185
+ }
180
186
}
0 commit comments