Skip to content

Commit 45a7b1b

Browse files
authored
Add methods like Curl::setGet() for each HTTP request method (php-curl-class#936)
* Adding methods like Curl::setGet() for each HTTP request method Adding these methods to separate the request initialization from the execution. Methods added: Curl::setDelete() Curl::setGet() Curl::setHead() Curl::setOptions() Curl::setPatch() Curl::setPost() Curl::setPut() Curl::setSearch() * Fix loading local test servers Error message: +++ declare -A pids run_phpunit.sh: line 77: declare: -A: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...] Error message: +++ server_count=7 +++ pids=() run_phpunit.sh: line 85: "7" - 1: syntax error: operand expected (error token is ""7" - 1") * Clean up
1 parent e2960d9 commit 45a7b1b

File tree

4 files changed

+86
-17
lines changed

4 files changed

+86
-17
lines changed

examples/multi_curl_add_curl.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,19 @@
1111
});
1212

1313
$curl_1 = new Curl();
14-
$curl_1->setOpt(CURLOPT_POST, true);
15-
$curl_1->setOpt(CURLOPT_POSTFIELDS, [
14+
$curl_1->setPost('https://httpbin.org/post', [
1615
'to' => 'alice',
1716
'subject' => 'hi',
1817
'body' => 'hi Alice',
1918
]);
20-
$curl_1->setUrl('https://httpbin.org/post');
2119
$multi_curl->addCurl($curl_1);
2220

2321
$curl_2 = new Curl();
24-
$curl_2->setOpt(CURLOPT_POST, true);
25-
$curl_2->setOpt(CURLOPT_POSTFIELDS, [
22+
$curl_2->setPost('https://httpbin.org/post', [
2623
'to' => 'bob',
2724
'subject' => 'hi',
2825
'body' => 'hi Bob',
2926
]);
30-
$curl_2->setUrl('https://httpbin.org/post');
3127
$multi_curl->addCurl($curl_2);
3228

3329
$multi_curl->start();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
use Curl\Curl;
6+
use Curl\MultiCurl;
7+
8+
$multi_curl = new MultiCurl();
9+
$multi_curl->complete(function ($instance) {
10+
echo 'call to "' . $instance->url . '" completed.' . "\n";
11+
});
12+
13+
$curl_1 = new Curl();
14+
$curl_1->setOpt(CURLOPT_POST, true);
15+
$curl_1->setOpt(CURLOPT_POSTFIELDS, [
16+
'to' => 'alice',
17+
'subject' => 'hi',
18+
'body' => 'hi Alice',
19+
]);
20+
$curl_1->setUrl('https://httpbin.org/post');
21+
$multi_curl->addCurl($curl_1);
22+
23+
$curl_2 = new Curl();
24+
$curl_2->setOpt(CURLOPT_POST, true);
25+
$curl_2->setOpt(CURLOPT_POSTFIELDS, [
26+
'to' => 'bob',
27+
'subject' => 'hi',
28+
'body' => 'hi Bob',
29+
]);
30+
$curl_2->setUrl('https://httpbin.org/post');
31+
$multi_curl->addCurl($curl_2);
32+
33+
$multi_curl->start();

src/Curl/Curl.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ private function progressInternal($callback)
275275
* @return mixed Returns the value provided by exec.
276276
*/
277277
public function delete($url, $query_parameters = [], $data = [])
278+
{
279+
$this->setDelete($url, $query_parameters, $data);
280+
return $this->exec();
281+
}
282+
283+
public function setDelete($url, $query_parameters = [], $data = [])
278284
{
279285
if (is_array($url)) {
280286
$data = $query_parameters;
@@ -295,7 +301,6 @@ public function delete($url, $query_parameters = [], $data = [])
295301
if (!empty($data)) {
296302
$this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data));
297303
}
298-
return $this->exec();
299304
}
300305

301306
/**
@@ -605,6 +610,12 @@ public function execDone()
605610
* @return mixed Returns the value provided by exec.
606611
*/
607612
public function get($url, $data = [])
613+
{
614+
$this->setGet($url, $data);
615+
return $this->exec();
616+
}
617+
618+
public function setGet($url, $data = [])
608619
{
609620
if (is_array($url)) {
610621
$data = $url;
@@ -613,7 +624,6 @@ public function get($url, $data = [])
613624
$this->setUrl($url, $data);
614625
$this->setOptInternal(CURLOPT_CUSTOMREQUEST, 'GET');
615626
$this->setOptInternal(CURLOPT_HTTPGET, true);
616-
return $this->exec();
617627
}
618628

619629
/**
@@ -642,6 +652,12 @@ public function getInfo($opt = null)
642652
* @return mixed Returns the value provided by exec.
643653
*/
644654
public function head($url, $data = [])
655+
{
656+
$this->setHead($url, $data);
657+
return $this->exec();
658+
}
659+
660+
public function setHead($url, $data = [])
645661
{
646662
if (is_array($url)) {
647663
$data = $url;
@@ -650,7 +666,6 @@ public function head($url, $data = [])
650666
$this->setUrl($url, $data);
651667
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'HEAD');
652668
$this->setOpt(CURLOPT_NOBODY, true);
653-
return $this->exec();
654669
}
655670

656671
/**
@@ -661,14 +676,19 @@ public function head($url, $data = [])
661676
* @return mixed Returns the value provided by exec.
662677
*/
663678
public function options($url, $data = [])
679+
{
680+
$this->setOptions($url, $data);
681+
return $this->exec();
682+
}
683+
684+
public function setOptions($url, $data = [])
664685
{
665686
if (is_array($url)) {
666687
$data = $url;
667688
$url = (string)$this->url;
668689
}
669690
$this->setUrl($url, $data);
670691
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
671-
return $this->exec();
672692
}
673693

674694
/**
@@ -679,6 +699,12 @@ public function options($url, $data = [])
679699
* @return mixed Returns the value provided by exec.
680700
*/
681701
public function patch($url, $data = [])
702+
{
703+
$this->setPatch($url, $data);
704+
return $this->exec();
705+
}
706+
707+
public function setPatch($url, $data = [])
682708
{
683709
if (is_array($url)) {
684710
$data = $url;
@@ -692,7 +718,6 @@ public function patch($url, $data = [])
692718
$this->setUrl($url);
693719
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH');
694720
$this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data));
695-
return $this->exec();
696721
}
697722

698723
/**
@@ -722,6 +747,12 @@ public function patch($url, $data = [])
722747
* [3] http://php.net/ChangeLog-5.php#5.5.11
723748
*/
724749
public function post($url, $data = '', $follow_303_with_post = false)
750+
{
751+
$this->setPost($url, $data, $follow_303_with_post);
752+
return $this->exec();
753+
}
754+
755+
public function setPost($url, $data = '', $follow_303_with_post = false)
725756
{
726757
if (is_array($url)) {
727758
$follow_303_with_post = (bool)$data;
@@ -746,7 +777,6 @@ public function post($url, $data = '', $follow_303_with_post = false)
746777

747778
$this->setOpt(CURLOPT_POST, true);
748779
$this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data));
749-
return $this->exec();
750780
}
751781

752782
/**
@@ -757,6 +787,12 @@ public function post($url, $data = '', $follow_303_with_post = false)
757787
* @return mixed Returns the value provided by exec.
758788
*/
759789
public function put($url, $data = [])
790+
{
791+
$this->setPut($url, $data);
792+
return $this->exec();
793+
}
794+
795+
public function setPut($url, $data = [])
760796
{
761797
if (is_array($url)) {
762798
$data = $url;
@@ -773,7 +809,6 @@ public function put($url, $data = [])
773809
if (!empty($put_data)) {
774810
$this->setOpt(CURLOPT_POSTFIELDS, $put_data);
775811
}
776-
return $this->exec();
777812
}
778813

779814
/**
@@ -784,6 +819,12 @@ public function put($url, $data = [])
784819
* @return mixed Returns the value provided by exec.
785820
*/
786821
public function search($url, $data = [])
822+
{
823+
$this->setSearch($url, $data);
824+
return $this->exec();
825+
}
826+
827+
public function setSearch($url, $data = [])
787828
{
788829
if (is_array($url)) {
789830
$data = $url;
@@ -800,7 +841,6 @@ public function search($url, $data = [])
800841
if (!empty($put_data)) {
801842
$this->setOpt(CURLOPT_POSTFIELDS, $put_data);
802843
}
803-
return $this->exec();
804844
}
805845

806846
/**

tests/run_phpunit.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ export PHP_CURL_CLASS_TEST_MODE_ENABLED="yes"
7474
# Start test servers. Run servers on different ports to allow simultaneous
7575
# requests without blocking.
7676
server_count=7
77-
declare -A pids
78-
for i in $(seq 0 $(("${server_count}" - 1))); do
77+
pids=()
78+
for i in $(seq 0 "$(echo "${server_count} - 1" | bc)"); do
7979
port=8000
8080
(( port += $i ))
8181

8282
php -S "127.0.0.1:${port}" server.php &> /dev/null &
83-
pids["${i}"]="${!}"
83+
pids+=("${!}")
8484
done
8585

8686
# Determine which phpunit to use.

0 commit comments

Comments
 (0)