Skip to content

Commit b6ad72f

Browse files
authored
Merge pull request php-curl-class#494 from zachborboa/master
Fix php-curl-class#493: Build POST data correctly when headers have been set
2 parents 44c1539 + 40f7026 commit b6ad72f

File tree

2 files changed

+90
-10
lines changed

2 files changed

+90
-10
lines changed

src/Curl/MultiCurl.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public function addDelete($url, $query_parameters = array(), $data = array())
5858
$url = $this->baseUrl;
5959
}
6060
$curl = new Curl();
61+
$this->queueHandle($curl);
6162
$curl->setUrl($url, $query_parameters);
6263
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
6364
$curl->setOpt(CURLOPT_POSTFIELDS, $curl->buildPostData($data));
64-
$this->queueHandle($curl);
6565
return $curl;
6666
}
6767

@@ -77,6 +77,7 @@ public function addDelete($url, $query_parameters = array(), $data = array())
7777
public function addDownload($url, $mixed_filename)
7878
{
7979
$curl = new Curl();
80+
$this->queueHandle($curl);
8081
$curl->setUrl($url);
8182

8283
// Use tmpfile() or php://temp to avoid "Too many open files" error.
@@ -95,7 +96,6 @@ public function addDownload($url, $mixed_filename)
9596
$curl->setOpt(CURLOPT_FILE, $curl->fileHandle);
9697
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
9798
$curl->setOpt(CURLOPT_HTTPGET, true);
98-
$this->queueHandle($curl);
9999
return $curl;
100100
}
101101

@@ -115,10 +115,10 @@ public function addGet($url, $data = array())
115115
$url = $this->baseUrl;
116116
}
117117
$curl = new Curl();
118+
$this->queueHandle($curl);
118119
$curl->setUrl($url, $data);
119120
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
120121
$curl->setOpt(CURLOPT_HTTPGET, true);
121-
$this->queueHandle($curl);
122122
return $curl;
123123
}
124124

@@ -138,10 +138,10 @@ public function addHead($url, $data = array())
138138
$url = $this->baseUrl;
139139
}
140140
$curl = new Curl();
141+
$this->queueHandle($curl);
141142
$curl->setUrl($url, $data);
142143
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'HEAD');
143144
$curl->setOpt(CURLOPT_NOBODY, true);
144-
$this->queueHandle($curl);
145145
return $curl;
146146
}
147147

@@ -161,10 +161,10 @@ public function addOptions($url, $data = array())
161161
$url = $this->baseUrl;
162162
}
163163
$curl = new Curl();
164+
$this->queueHandle($curl);
164165
$curl->setUrl($url, $data);
165166
$curl->removeHeader('Content-Length');
166167
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
167-
$this->queueHandle($curl);
168168
return $curl;
169169
}
170170

@@ -184,11 +184,11 @@ public function addPatch($url, $data = array())
184184
$url = $this->baseUrl;
185185
}
186186
$curl = new Curl();
187+
$this->queueHandle($curl);
187188
$curl->setUrl($url);
188189
$curl->removeHeader('Content-Length');
189190
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH');
190191
$curl->setOpt(CURLOPT_POSTFIELDS, $data);
191-
$this->queueHandle($curl);
192192
return $curl;
193193
}
194194

@@ -213,6 +213,7 @@ public function addPost($url, $data = array(), $follow_303_with_post = false)
213213
}
214214

215215
$curl = new Curl();
216+
$this->queueHandle($curl);
216217

217218
if (is_array($data) && empty($data)) {
218219
$curl->removeHeader('Content-Length');
@@ -230,7 +231,6 @@ public function addPost($url, $data = array(), $follow_303_with_post = false)
230231

231232
$curl->setOpt(CURLOPT_POST, true);
232233
$curl->setOpt(CURLOPT_POSTFIELDS, $curl->buildPostData($data));
233-
$this->queueHandle($curl);
234234
return $curl;
235235
}
236236

@@ -250,14 +250,14 @@ public function addPut($url, $data = array())
250250
$url = $this->baseUrl;
251251
}
252252
$curl = new Curl();
253+
$this->queueHandle($curl);
253254
$curl->setUrl($url);
254255
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'PUT');
255256
$put_data = $curl->buildPostData($data);
256257
if (is_string($put_data)) {
257258
$curl->setHeader('Content-Length', strlen($put_data));
258259
}
259260
$curl->setOpt(CURLOPT_POSTFIELDS, $put_data);
260-
$this->queueHandle($curl);
261261
return $curl;
262262
}
263263

@@ -277,14 +277,14 @@ public function addSearch($url, $data = array())
277277
$url = $this->baseUrl;
278278
}
279279
$curl = new Curl();
280+
$this->queueHandle($curl);
280281
$curl->setUrl($url);
281282
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'SEARCH');
282283
$put_data = $curl->buildPostData($data);
283284
if (is_string($put_data)) {
284285
$curl->setHeader('Content-Length', strlen($put_data));
285286
}
286287
$curl->setOpt(CURLOPT_POSTFIELDS, $put_data);
287-
$this->queueHandle($curl);
288288
return $curl;
289289
}
290290

@@ -495,6 +495,7 @@ public function setCookieJar($cookie_jar)
495495
public function setHeader($key, $value)
496496
{
497497
$this->headers[$key] = $value;
498+
$this->updateHeaders();
498499
}
499500

500501
/**
@@ -510,6 +511,7 @@ public function setHeaders($headers)
510511
foreach ($headers as $key => $value) {
511512
$this->headers[$key] = $value;
512513
}
514+
$this->updateHeaders();
513515
}
514516

515517
/**
@@ -783,6 +785,18 @@ public function __destruct()
783785
$this->close();
784786
}
785787

788+
/**
789+
* Update Headers
790+
*
791+
* @access private
792+
*/
793+
private function updateHeaders()
794+
{
795+
foreach ($this->curls as $curl) {
796+
$curl->setHeaders($this->headers);
797+
}
798+
}
799+
786800
/**
787801
* Queue Handle
788802
*
@@ -795,6 +809,8 @@ private function queueHandle($curl)
795809
$curl->id = $this->nextCurlId++;
796810
$curl->isChildOfMultiCurl = true;
797811
$this->curls[$curl->id] = $curl;
812+
813+
$curl->setHeaders($this->headers);
798814
}
799815

800816
/**
@@ -829,7 +845,6 @@ private function initHandle($curl)
829845
}
830846

831847
$curl->setOpts($this->options);
832-
$curl->setHeaders($this->headers);
833848
$curl->setRetry($this->retry);
834849
$curl->setCookies($this->cookies);
835850

tests/PHPCurlClass/PHPMultiCurlClassTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,71 @@ public function testSetCookies()
20702070
$this->assertEquals('yummy', $get_2->responseCookies['mycookie']);
20712071
}
20722072

2073+
public function testJsonRequest()
2074+
{
2075+
foreach (array(
2076+
array(
2077+
array(
2078+
'key' => 'value',
2079+
),
2080+
'{"key":"value"}',
2081+
),
2082+
array(
2083+
array(
2084+
'key' => 'value',
2085+
'strings' => array(
2086+
'a',
2087+
'b',
2088+
'c',
2089+
),
2090+
),
2091+
'{"key":"value","strings":["a","b","c"]}',
2092+
),
2093+
) as $test) {
2094+
list($data, $expected_response) = $test;
2095+
2096+
$multi_curl = new MultiCurl();
2097+
$multi_curl->setHeader('X-DEBUG-TEST', 'post_json');
2098+
$multi_curl->complete(function ($instance) use ($expected_response, $data) {
2099+
\PHPUnit\Framework\Assert::assertEquals($expected_response, $instance->response);
2100+
});
2101+
$multi_curl->addPost(Test::TEST_URL, json_encode($data));
2102+
$multi_curl->start();
2103+
2104+
foreach (array(
2105+
'Content-Type',
2106+
'content-type',
2107+
'CONTENT-TYPE') as $key) {
2108+
foreach (array(
2109+
'APPLICATION/JSON',
2110+
'APPLICATION/JSON; CHARSET=UTF-8',
2111+
'APPLICATION/JSON;CHARSET=UTF-8',
2112+
'application/json',
2113+
'application/json; charset=utf-8',
2114+
'application/json;charset=UTF-8',
2115+
) as $value) {
2116+
$multi_curl = new MultiCurl();
2117+
$multi_curl->setHeader('X-DEBUG-TEST', 'post_json');
2118+
$multi_curl->setHeader($key, $value);
2119+
$multi_curl->complete(function ($instance) use ($expected_response, $data) {
2120+
\PHPUnit\Framework\Assert::assertEquals($expected_response, $instance->response);
2121+
});
2122+
$multi_curl->addPost(Test::TEST_URL, json_encode($data));
2123+
$multi_curl->start();
2124+
2125+
$multi_curl = new MultiCurl();
2126+
$multi_curl->setHeader('X-DEBUG-TEST', 'post_json');
2127+
$multi_curl->setHeader($key, $value);
2128+
$multi_curl->complete(function ($instance) use ($expected_response, $data) {
2129+
\PHPUnit\Framework\Assert::assertEquals($expected_response, $instance->response);
2130+
});
2131+
$multi_curl->addPost(Test::TEST_URL, $data);
2132+
$multi_curl->start();
2133+
}
2134+
}
2135+
}
2136+
}
2137+
20732138
public function testJsonDecoder()
20742139
{
20752140
$data = array(

0 commit comments

Comments
 (0)