Skip to content

Commit 78bff9b

Browse files
authored
Merge pull request php-curl-class#396 from zachborboa/master
Implement removeHeader() (curl -H "Host:" ...)
2 parents d8b46b9 + bfc868f commit 78bff9b

File tree

6 files changed

+145
-16
lines changed

6 files changed

+145
-16
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ $curl->post('https://www.example.com/login/', array(
6363
$curl = new Curl();
6464
$curl->setBasicAuthentication('username', 'password');
6565
$curl->setUserAgent('MyUserAgent/0.0.1 (+https://www.example.com/bot.html)');
66-
$curl->setReferrer('');
66+
$curl->setReferrer('https://www.example.com/url?url=https%3A%2F%2Fwww.example.com%2F');
6767
$curl->setHeader('X-Requested-With', 'XMLHttpRequest');
6868
$curl->setCookie('key', 'value');
6969
$curl->get('https://www.example.com/');
7070

7171
if ($curl->error) {
7272
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage;
7373
} else {
74-
echo $curl->response;
74+
echo 'Response:' . "\n";
75+
var_dump($curl->response);
7576
}
7677

7778
var_dump($curl->requestHeaders);
@@ -201,6 +202,7 @@ Curl::patch($url, $data = array())
201202
Curl::post($url, $data = array(), $follow_303_with_post = false)
202203
Curl::progress($callback)
203204
Curl::put($url, $data = array())
205+
Curl::removeHeader($key)
204206
Curl::search($url, $data = array())
205207
Curl::setBasicAuthentication($username, $password = '')
206208
Curl::setConnectTimeout($seconds)
@@ -215,6 +217,7 @@ Curl::setDefaultUserAgent()
215217
Curl::setDefaultXmlDecoder()
216218
Curl::setDigestAuthentication($username, $password = '')
217219
Curl::setHeader($key, $value)
220+
Curl::setHeaders($headers)
218221
Curl::setJsonDecoder($function)
219222
Curl::setMaxFilesize($bytes)
220223
Curl::setOpt($option, $value)
@@ -248,6 +251,7 @@ MultiCurl::close()
248251
MultiCurl::complete($callback)
249252
MultiCurl::error($callback)
250253
MultiCurl::getOpt($option)
254+
MultiCurl::removeHeader($key)
251255
MultiCurl::setBasicAuthentication($username, $password = '')
252256
MultiCurl::setConcurrency($concurrency)
253257
MultiCurl::setConnectTimeout($seconds)
@@ -257,6 +261,7 @@ MultiCurl::setCookieJar($cookie_jar)
257261
MultiCurl::setCookieString($string)
258262
MultiCurl::setDigestAuthentication($username, $password = '')
259263
MultiCurl::setHeader($key, $value)
264+
MultiCurl::setHeaders($headers)
260265
MultiCurl::setJsonDecoder($function)
261266
MultiCurl::setOpt($option, $value)
262267
MultiCurl::setOpts($options)

src/Curl/Curl.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public function options($url, $data = array())
495495
$url = $this->baseUrl;
496496
}
497497
$this->setURL($url, $data);
498-
$this->unsetHeader('Content-Length');
498+
$this->removeHeader('Content-Length');
499499
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
500500
return $this->exec();
501501
}
@@ -517,7 +517,7 @@ public function patch($url, $data = array())
517517
}
518518

519519
if (is_array($data) && empty($data)) {
520-
$this->unsetHeader('Content-Length');
520+
$this->removeHeader('Content-Length');
521521
}
522522

523523
$this->setURL($url);
@@ -900,6 +900,8 @@ public function setDefaultUserAgent()
900900
/**
901901
* Set Header
902902
*
903+
* Add extra header to include in the request.
904+
*
903905
* @access public
904906
* @param $key
905907
* @param $value
@@ -914,6 +916,27 @@ public function setHeader($key, $value)
914916
$this->setOpt(CURLOPT_HTTPHEADER, $headers);
915917
}
916918

919+
/**
920+
* Set Headers
921+
*
922+
* Add extra headers to include in the request.
923+
*
924+
* @access public
925+
* @param $headers
926+
*/
927+
public function setHeaders($headers)
928+
{
929+
foreach ($headers as $key => $value) {
930+
$this->headers[$key] = $value;
931+
}
932+
933+
$headers = array();
934+
foreach ($this->headers as $key => $value) {
935+
$headers[] = $key . ': ' . $value;
936+
}
937+
$this->setOpt(CURLOPT_HTTPHEADER, $headers);
938+
}
939+
917940
/**
918941
* Set JSON Decoder
919942
*
@@ -1058,13 +1081,33 @@ public function success($callback)
10581081
/**
10591082
* Unset Header
10601083
*
1084+
* Remove extra header previously set using Curl::setHeader().
1085+
*
10611086
* @access public
10621087
* @param $key
10631088
*/
10641089
public function unsetHeader($key)
10651090
{
1066-
$this->setHeader($key, '');
10671091
unset($this->headers[$key]);
1092+
$headers = array();
1093+
foreach ($this->headers as $key => $value) {
1094+
$headers[] = $key . ': ' . $value;
1095+
}
1096+
$this->setOpt(CURLOPT_HTTPHEADER, $headers);
1097+
}
1098+
1099+
/**
1100+
* Remove Header
1101+
*
1102+
* Remove an internal header from the request.
1103+
* Using `curl -H "Host:" ...' is equivalent to $curl->removeHeader('Host');.
1104+
*
1105+
* @access public
1106+
* @param $key
1107+
*/
1108+
public function removeHeader($key)
1109+
{
1110+
$this->setHeader($key, '');
10681111
}
10691112

10701113
/**

src/Curl/MultiCurl.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function addOptions($url, $data = array())
159159
}
160160
$curl = new Curl();
161161
$curl->setURL($url, $data);
162-
$curl->unsetHeader('Content-Length');
162+
$curl->removeHeader('Content-Length');
163163
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
164164
$this->queueHandle($curl);
165165
return $curl;
@@ -182,7 +182,7 @@ public function addPatch($url, $data = array())
182182
}
183183
$curl = new Curl();
184184
$curl->setURL($url);
185-
$curl->unsetHeader('Content-Length');
185+
$curl->removeHeader('Content-Length');
186186
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH');
187187
$curl->setOpt(CURLOPT_POSTFIELDS, $data);
188188
$this->queueHandle($curl);
@@ -212,7 +212,7 @@ public function addPost($url, $data = array(), $follow_303_with_post = false)
212212
$curl = new Curl();
213213

214214
if (is_array($data) && empty($data)) {
215-
$curl->unsetHeader('Content-Length');
215+
$curl->removeHeader('Content-Length');
216216
}
217217

218218
$curl->setURL($url);
@@ -456,6 +456,8 @@ public function setCookieJar($cookie_jar)
456456
/**
457457
* Set Header
458458
*
459+
* Add extra header to include in the request.
460+
*
459461
* @access public
460462
* @param $key
461463
* @param $value
@@ -465,6 +467,21 @@ public function setHeader($key, $value)
465467
$this->headers[$key] = $value;
466468
}
467469

470+
/**
471+
* Set Headers
472+
*
473+
* Add extra headers to include in the request.
474+
*
475+
* @access public
476+
* @param $headers
477+
*/
478+
public function setHeaders($headers)
479+
{
480+
foreach ($headers as $key => $value) {
481+
$this->headers[$key] = $value;
482+
}
483+
}
484+
468485
/**
469486
* Set JSON Decoder
470487
*
@@ -643,15 +660,30 @@ public function success($callback)
643660
/**
644661
* Unset Header
645662
*
663+
* Remove extra header previously set using Curl::setHeader().
664+
*
646665
* @access public
647666
* @param $key
648667
*/
649668
public function unsetHeader($key)
650669
{
651-
$this->setHeader($key, '');
652670
unset($this->headers[$key]);
653671
}
654672

673+
/**
674+
* Remove Header
675+
*
676+
* Remove an internal header from the request.
677+
* Using `curl -H "Host:" ...' is equivalent to $curl->removeHeader('Host');.
678+
*
679+
* @access public
680+
* @param $key
681+
*/
682+
public function removeHeader($key)
683+
{
684+
$this->setHeader($key, '');
685+
}
686+
655687
/**
656688
* Verbose
657689
*
@@ -714,15 +746,13 @@ private function initHandle($curl)
714746
$curl->complete($this->completeFunction);
715747
}
716748

717-
foreach ($this->options as $option => $value) {
718-
$curl->setOpt($option, $value);
719-
}
720-
foreach ($this->headers as $key => $value) {
721-
$curl->setHeader($key, $value);
722-
}
749+
$curl->setOpts($this->options);
750+
$curl->setHeaders($this->headers);
751+
723752
foreach ($this->cookies as $key => $value) {
724753
$curl->setCookie($key, $value);
725754
}
755+
726756
$curl->setJsonDecoder($this->jsonDecoder);
727757
$curl->setXmlDecoder($this->xmlDecoder);
728758

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,4 +2979,25 @@ public function testBuildUrlArgSeparator()
29792979
$this->assertEquals($expected_url, $actual_url);
29802980
}
29812981
}
2982+
2983+
public function testUnsetHeader()
2984+
{
2985+
$request_key = 'X-Request-Id';
2986+
$request_value = '1';
2987+
$data = array(
2988+
'test' => 'server',
2989+
'key' => 'HTTP_X_REQUEST_ID',
2990+
);
2991+
2992+
$curl = new Curl();
2993+
$curl->setHeader($request_key, $request_value);
2994+
$curl->get(Test::TEST_URL, $data);
2995+
$this->assertEquals($request_value, $curl->response);
2996+
2997+
$curl = new Curl();
2998+
$curl->setHeader($request_key, $request_value);
2999+
$curl->unsetHeader($request_key);
3000+
$curl->get(Test::TEST_URL, $data);
3001+
$this->assertEquals('', $curl->response);
3002+
}
29823003
}

tests/PHPCurlClass/PHPMultiCurlClassTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,4 +2383,29 @@ public function testAlternativeStandardErrorOutput()
23832383

23842384
$this->assertNotEmpty($stderr);
23852385
}
2386+
2387+
public function testUnsetHeader()
2388+
{
2389+
$request_key = 'X-Request-Id';
2390+
$request_value = '1';
2391+
$data = array(
2392+
'test' => 'server',
2393+
'key' => 'HTTP_X_REQUEST_ID',
2394+
);
2395+
2396+
$multi_curl = new MultiCurl();
2397+
$multi_curl->setHeader($request_key, $request_value);
2398+
$multi_curl->addGet(Test::TEST_URL, $data)->complete(function ($instance) use ($request_value) {
2399+
PHPUnit_Framework_Assert::assertEquals($request_value, $instance->response);
2400+
});
2401+
$multi_curl->start();
2402+
2403+
$multi_curl = new MultiCurl();
2404+
$multi_curl->setHeader($request_key, $request_value);
2405+
$multi_curl->unsetHeader($request_key);
2406+
$multi_curl->addGet(Test::TEST_URL, $data)->complete(function ($instance) {
2407+
PHPUnit_Framework_Assert::assertEquals('', $instance->response);
2408+
});
2409+
$multi_curl->start();
2410+
}
23862411
}

tests/PHPCurlClass/server.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333
}
3434
}
3535

36-
$test = isset($_SERVER['HTTP_X_DEBUG_TEST']) ? $_SERVER['HTTP_X_DEBUG_TEST'] : '';
36+
$test = '';
37+
if (isset($_SERVER['HTTP_X_DEBUG_TEST'])) {
38+
$test = $_SERVER['HTTP_X_DEBUG_TEST'];
39+
} elseif (isset($_GET['test'])) {
40+
$test = $_GET['test'];
41+
}
3742
$key = isset($data_values['key']) ? $data_values['key'] : '';
3843

3944
if ($test === 'http_basic_auth') {

0 commit comments

Comments
 (0)