Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 4e2a744

Browse files
committed
Merge pull request #386 from SammyK/4.0-multi-array
[4.0] Fix for multidimensional params support
2 parents b583b4a + 2156afc commit 4e2a744

File tree

2 files changed

+34
-54
lines changed

2 files changed

+34
-54
lines changed

src/Facebook/HttpClients/FacebookCurlHttpClient.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function openConnection($url, $method = 'GET', array $parameters = array(
181181
);
182182

183183
if ($method !== 'GET') {
184-
$options[CURLOPT_POSTFIELDS] = $parameters;
184+
$options[CURLOPT_POSTFIELDS] = !$this->paramsHaveFile($parameters) ? http_build_query($parameters, null, '&') : $parameters;
185185
}
186186
if ($method === 'DELETE' || $method === 'PUT') {
187187
$options[CURLOPT_CUSTOMREQUEST] = $method;
@@ -326,4 +326,22 @@ private function needsCurlProxyFix()
326326
return $version < self::CURL_PROXY_QUIRK_VER;
327327
}
328328

329+
/**
330+
* Detect if the params have a file to upload.
331+
*
332+
* @param array $params
333+
*
334+
* @return boolean
335+
*/
336+
private function paramsHaveFile(array $params)
337+
{
338+
foreach ($params as $value) {
339+
if ($value instanceof \CURLFile) {
340+
return true;
341+
}
342+
}
343+
344+
return false;
345+
}
346+
329347
}

tests/HttpClients/FacebookCurlHttpClientTest.php

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function testCanOpenGetCurlConnection()
3737
->with(m::on(function($arg) {
3838
$caInfo = array_diff($arg, [
3939
CURLOPT_CUSTOMREQUEST => 'GET',
40-
CURLOPT_HTTPHEADER => [],
4140
CURLOPT_URL => 'http://foo.com',
4241
CURLOPT_CONNECTTIMEOUT => 10,
4342
CURLOPT_TIMEOUT => 60,
@@ -72,11 +71,15 @@ public function testCanOpenGetCurlConnectionWithHeaders()
7271
$this->curlMock
7372
->shouldReceive('setopt_array')
7473
->with(m::on(function($arg) {
74+
75+
// array_diff() will sometimes trigger error on multidimensional arrays
76+
if (['X-foo: bar'] !== $arg[CURLOPT_HTTPHEADER]) {
77+
return false;
78+
}
79+
unset($arg[CURLOPT_HTTPHEADER]);
80+
7581
$caInfo = array_diff($arg, [
7682
CURLOPT_CUSTOMREQUEST => 'GET',
77-
CURLOPT_HTTPHEADER => array(
78-
'X-foo: bar',
79-
),
8083
CURLOPT_URL => 'http://foo.com',
8184
CURLOPT_CONNECTTIMEOUT => 10,
8285
CURLOPT_TIMEOUT => 60,
@@ -114,17 +117,14 @@ public function testCanOpenPostCurlConnection()
114117
->with(m::on(function($arg) {
115118
$caInfo = array_diff($arg, [
116119
CURLOPT_CUSTOMREQUEST => 'POST',
117-
CURLOPT_HTTPHEADER => [],
118120
CURLOPT_URL => 'http://bar.com',
119121
CURLOPT_CONNECTTIMEOUT => 10,
120122
CURLOPT_TIMEOUT => 60,
121123
CURLOPT_RETURNTRANSFER => true,
122124
CURLOPT_HEADER => true,
123125
CURLOPT_SSL_VERIFYHOST => 2,
124126
CURLOPT_SSL_VERIFYPEER => true,
125-
CURLOPT_POSTFIELDS => array(
126-
'baz' => 'bar',
127-
),
127+
CURLOPT_POSTFIELDS => 'baz=bar&foo%5B0%5D=1&foo%5B1%5D=2&foo%5B2%5D=3',
128128
]);
129129

130130
if (count($caInfo) !== 1) {
@@ -140,47 +140,12 @@ public function testCanOpenPostCurlConnection()
140140
->once()
141141
->andReturn(null);
142142

143-
$this->curlClient->openConnection('http://bar.com', 'POST', array('baz' => 'bar'));
144-
}
145-
146-
public function testCanOpenPutCurlConnection()
147-
{
148-
$this->curlMock
149-
->shouldReceive('init')
150-
->once()
151-
->andReturn(null);
152-
$this->curlMock
153-
->shouldReceive('setopt_array')
154-
->with(m::on(function($arg) {
155-
$caInfo = array_diff($arg, [
156-
CURLOPT_CUSTOMREQUEST => 'PUT',
157-
CURLOPT_HTTPHEADER => [],
158-
CURLOPT_URL => 'http://baz.com',
159-
CURLOPT_CONNECTTIMEOUT => 10,
160-
CURLOPT_TIMEOUT => 60,
161-
CURLOPT_RETURNTRANSFER => true,
162-
CURLOPT_HEADER => true,
163-
CURLOPT_SSL_VERIFYHOST => 2,
164-
CURLOPT_SSL_VERIFYPEER => true,
165-
CURLOPT_POSTFIELDS => array(
166-
'baz' => 'bar',
167-
),
168-
]);
169-
170-
if (count($caInfo) !== 1) {
171-
return false;
172-
}
173-
174-
if (1 !== preg_match('/.+\/certs\/DigiCertHighAssuranceEVRootCA\.pem$/', $caInfo[CURLOPT_CAINFO])) {
175-
return false;
176-
}
177-
178-
return true;
179-
}))
180-
->once()
181-
->andReturn(null);
182-
183-
$this->curlClient->openConnection('http://baz.com', 'PUT', array('baz' => 'bar'));
143+
// Prove can support multidimensional params
144+
$params = array(
145+
'baz' => 'bar',
146+
'foo' => array(1, 2, 3),
147+
);
148+
$this->curlClient->openConnection('http://bar.com', 'POST', $params);
184149
}
185150

186151
public function testCanOpenDeleteCurlConnection()
@@ -194,17 +159,14 @@ public function testCanOpenDeleteCurlConnection()
194159
->with(m::on(function($arg) {
195160
$caInfo = array_diff($arg, [
196161
CURLOPT_CUSTOMREQUEST => 'DELETE',
197-
CURLOPT_HTTPHEADER => [],
198162
CURLOPT_URL => 'http://faz.com',
199163
CURLOPT_CONNECTTIMEOUT => 10,
200164
CURLOPT_TIMEOUT => 60,
201165
CURLOPT_RETURNTRANSFER => true,
202166
CURLOPT_HEADER => true,
203167
CURLOPT_SSL_VERIFYHOST => 2,
204168
CURLOPT_SSL_VERIFYPEER => true,
205-
CURLOPT_POSTFIELDS => array(
206-
'baz' => 'bar',
207-
),
169+
CURLOPT_POSTFIELDS => 'baz=bar',
208170
]);
209171

210172
if (count($caInfo) !== 1) {

0 commit comments

Comments
 (0)