@@ -30,7 +30,7 @@ public function __construct(array $options = [])
30
30
{
31
31
$ this ->options = $ options ;
32
32
}
33
-
33
+
34
34
/**
35
35
* Set option
36
36
* https://www.php.net/manual/en/function.curl-setopt.php
@@ -53,7 +53,7 @@ public function hasOption(int $key): bool
53
53
{
54
54
return (isset ($ this ->options [$ key ]));
55
55
}
56
-
56
+
57
57
/**
58
58
* Sends a PSR-7 request and returns a PSR-7 response.
59
59
* @param RequestInterface $request
@@ -64,24 +64,19 @@ public function sendRequest(RequestInterface $request): ResponseInterface
64
64
$ this ->requestData = (string )$ request ->getBody ();
65
65
$ this ->requestDataLength = strlen ($ this ->requestData );
66
66
$ this ->prepareRequest ($ request );
67
- $ this ->buildHeaders ($ request );
68
-
69
67
try {
68
+ $ this ->curl = curl_init ();
69
+ $ this ->buildHeaders ($ request );
70
70
if (!extension_loaded ('curl ' )) {
71
71
throw new InvalidArgumentException ('You need to enable CURL on your server. ' );
72
72
}
73
-
74
73
// Init curl request
75
- $ this ->curl = curl_init ();
76
74
$ this ->buildOptions ();
77
75
$ this ->buildFromMethods ($ request );
78
-
79
76
// Execute request
80
77
$ this ->createRequest ();
81
-
82
78
// Close curl request
83
79
curl_close ($ this ->curl );
84
-
85
80
// Retrive the body
86
81
return $ this ->createResponse ();
87
82
} catch (InvalidArgumentException $ e ) {
@@ -101,22 +96,22 @@ protected function buildFromMethods(RequestInterface $request): void
101
96
switch ($ request ->getMethod ()) {
102
97
case 'GET ' :
103
98
$ this ->get ();
104
- // no break
99
+ break ;
105
100
case 'POST ' :
106
101
$ this ->post ();
107
- // no break
102
+ break ;
108
103
case 'PUT ' :
109
104
$ this ->put ();
110
- // no break
105
+ break ;
111
106
case 'PATCH ' :
112
107
$ request = $ this ->patch ($ request );
113
- // no break
108
+ break ;
114
109
case 'DELETE ' :
115
110
$ this ->delete ();
116
- // no break
111
+ break ;
117
112
default :
118
- throw new InvalidArgumentException ('The requesr method ( ' . $ request ->getMethod () . ') ' .
119
- ' is not supported. ' ) ;
113
+ throw new InvalidArgumentException ('The request method ( ' . $ request ->getMethod () . ') is not supported. ' );
114
+ break ;
120
115
}
121
116
}
122
117
@@ -241,7 +236,9 @@ private function buildHeaders(RequestInterface $request): void
241
236
foreach ($ request ->getHeaders () as $ name => $ _unUsedVal ) {
242
237
$ data [] = "{$ name }: " . $ request ->getHeaderLine ($ name );
243
238
}
244
- curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , $ data );
239
+ if (count ($ data ) > 0 ) {
240
+ curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , $ data );
241
+ }
245
242
}
246
243
247
244
/**
0 commit comments