@@ -89,28 +89,57 @@ public function setBody($body)
89
89
$ this ->body = $ body ;
90
90
}
91
91
92
+
92
93
public function sendRequest ($ saveBody = true )
94
+ {
95
+ $ this ->prepareOptions ();
96
+
97
+ try {
98
+ $ this ->client = new Client ();
99
+ $ this ->response = $ this ->client ->request ($ this ->method , $ this ->url , $ this ->options );
100
+ return true ;
101
+ } catch (RequestException $ e ) {
102
+ $ this ->response = $ e ->getResponse ();
103
+ return false ;
104
+ }
105
+ }
106
+
107
+ private function prepareOptions ()
93
108
{
94
109
$ this ->options = array_merge ([
95
110
'headers ' => $ this ->headers ,
96
111
'query ' => $ this ->queryParams ,
97
112
], $ this ->options );
98
113
114
+ $ this ->prepareBody ();
115
+ $ this ->prepareAuth ();
116
+ }
117
+
118
+ private function prepareBody ()
119
+ {
99
120
if ($ this ->method === 'POST ' || $ this ->method === 'PUT ' ) {
121
+ $ this ->setDefaultContentType ();
122
+
100
123
if (!empty ($ this ->postData )) {
101
124
$ this ->options ['form_params ' ] = $ this ->postData ;
102
125
} elseif (isset ($ this ->body )) {
103
- if (!isset ($ this ->headers ['Content-Type ' ])) {
104
- $ this ->options ['headers ' ] = array_merge (
105
- // POSTでContent-Type未指定の場合はapplication/x-www-form-urlencodedにFallbackする
106
- ['Content-Type ' => 'application/x-www-form-urlencoded ' ],
107
- $ this ->options ['headers ' ]
108
- );
109
- }
110
126
$ this ->options ['body ' ] = $ this ->body ;
111
127
}
112
128
}
129
+ }
130
+
131
+ private function setDefaultContentType ()
132
+ {
133
+ if (!isset ($ this ->headers ['Content-Type ' ])) {
134
+ $ this ->options ['headers ' ] = array_merge (
135
+ ['Content-Type ' => 'application/x-www-form-urlencoded ' ],
136
+ $ this ->options ['headers ' ]
137
+ );
138
+ }
139
+ }
113
140
141
+ private function prepareAuth ()
142
+ {
114
143
if (isset ($ this ->basicAuthUsername )) {
115
144
$ this ->options = array_merge ([
116
145
'auth ' => [
@@ -120,15 +149,6 @@ public function sendRequest($saveBody = true)
120
149
]
121
150
], $ this ->options );
122
151
}
123
-
124
- try {
125
- $ this ->client = new Client ();
126
- $ this ->response = $ this ->client ->request ($ this ->method , $ this ->url , $ this ->options );
127
- return true ;
128
- } catch (RequestException $ e ) {
129
- $ this ->response = $ e ->getResponse ();
130
- return false ;
131
- }
132
152
}
133
153
134
154
public function getResponseCode ()
@@ -165,6 +185,30 @@ public function setBasicAuth($user, $pass)
165
185
$ this ->basicAuthPassword = $ pass ;
166
186
}
167
187
188
+ public function getResponseReason ()
189
+ {
190
+ $ response = $ this ->client ->getResponse ();
191
+ return $ response ->getReasonPhrase ();
192
+ }
193
+
194
+ public function getResponseCookies ()
195
+ {
196
+ $ response = $ this ->client ->getResponse ();
197
+ $ cookieJar = $ response ->getCookies ();
198
+ $ cookies = [];
199
+
200
+ foreach ($ cookieJar as $ cookie ) {
201
+ $ cookies [$ cookie ->getName ()] = $ cookie ->getValue ();
202
+ }
203
+
204
+ return $ cookies ;
205
+ }
206
+
207
+ public function clearPostData ()
208
+ {
209
+ $ this ->postData = [];
210
+ }
211
+
168
212
public function setHttpVer ($ http )
169
213
{
170
214
throw new LogicException ('Not implemented yet ' );
@@ -185,36 +229,27 @@ public function addCookie($name, $value)
185
229
throw new LogicException ('Not implemented yet ' );
186
230
}
187
231
188
- public function getResponseReason ()
232
+ public function disconnect ()
189
233
{
190
- $ response = $ this ->client ->getResponse ();
191
- return $ response ->getReasonPhrase ();
234
+ throw new LogicException ('Not implemented yet ' );
192
235
}
193
236
194
- public function getResponseCookies ( )
237
+ public function attach (& $ listener )
195
238
{
196
- $ response = $ this ->client ->getResponse ();
197
- $ cookieJar = $ response ->getCookies ();
198
- $ cookies = [];
199
-
200
- foreach ($ cookieJar as $ cookie ) {
201
- $ cookies [$ cookie ->getName ()] = $ cookie ->getValue ();
202
- }
203
-
204
- return $ cookies ;
239
+ throw new LogicException ('Not implemented yet ' );
205
240
}
206
241
207
- public function disconnect ( )
242
+ public function detach (& $ listener )
208
243
{
209
244
throw new LogicException ('Not implemented yet ' );
210
245
}
211
246
212
- public function attach (& $ listener )
247
+ public function reset ( $ url , $ params = array () )
213
248
{
214
249
throw new LogicException ('Not implemented yet ' );
215
250
}
216
251
217
- public function detach (& $ listener )
252
+ public function clearCookies ( )
218
253
{
219
254
throw new LogicException ('Not implemented yet ' );
220
255
}
0 commit comments