From 45a7b1b671b927fbe88937b8d0d7a6573e3ea166 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 23 Mar 2025 05:00:39 -0400 Subject: [PATCH] Add methods like Curl::setGet() for each HTTP request method (#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 --- examples/multi_curl_add_curl.php | 8 +--- examples/multi_curl_add_curl_low_level.php | 33 +++++++++++++ src/Curl/Curl.php | 56 ++++++++++++++++++---- tests/run_phpunit.sh | 6 +-- 4 files changed, 86 insertions(+), 17 deletions(-) create mode 100644 examples/multi_curl_add_curl_low_level.php diff --git a/examples/multi_curl_add_curl.php b/examples/multi_curl_add_curl.php index f932205..be5b809 100644 --- a/examples/multi_curl_add_curl.php +++ b/examples/multi_curl_add_curl.php @@ -11,23 +11,19 @@ $multi_curl->complete(function ($instance) { }); $curl_1 = new Curl(); -$curl_1->setOpt(CURLOPT_POST, true); -$curl_1->setOpt(CURLOPT_POSTFIELDS, [ +$curl_1->setPost('https://httpbin.org/post', [ 'to' => 'alice', 'subject' => 'hi', 'body' => 'hi Alice', ]); -$curl_1->setUrl('https://httpbin.org/post'); $multi_curl->addCurl($curl_1); $curl_2 = new Curl(); -$curl_2->setOpt(CURLOPT_POST, true); -$curl_2->setOpt(CURLOPT_POSTFIELDS, [ +$curl_2->setPost('https://httpbin.org/post', [ 'to' => 'bob', 'subject' => 'hi', 'body' => 'hi Bob', ]); -$curl_2->setUrl('https://httpbin.org/post'); $multi_curl->addCurl($curl_2); $multi_curl->start(); diff --git a/examples/multi_curl_add_curl_low_level.php b/examples/multi_curl_add_curl_low_level.php new file mode 100644 index 0000000..f932205 --- /dev/null +++ b/examples/multi_curl_add_curl_low_level.php @@ -0,0 +1,33 @@ +complete(function ($instance) { + echo 'call to "' . $instance->url . '" completed.' . "\n"; +}); + +$curl_1 = new Curl(); +$curl_1->setOpt(CURLOPT_POST, true); +$curl_1->setOpt(CURLOPT_POSTFIELDS, [ + 'to' => 'alice', + 'subject' => 'hi', + 'body' => 'hi Alice', +]); +$curl_1->setUrl('https://httpbin.org/post'); +$multi_curl->addCurl($curl_1); + +$curl_2 = new Curl(); +$curl_2->setOpt(CURLOPT_POST, true); +$curl_2->setOpt(CURLOPT_POSTFIELDS, [ + 'to' => 'bob', + 'subject' => 'hi', + 'body' => 'hi Bob', +]); +$curl_2->setUrl('https://httpbin.org/post'); +$multi_curl->addCurl($curl_2); + +$multi_curl->start(); diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 6027cd6..a9a95e2 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -275,6 +275,12 @@ class Curl extends BaseCurl * @return mixed Returns the value provided by exec. */ public function delete($url, $query_parameters = [], $data = []) + { + $this->setDelete($url, $query_parameters, $data); + return $this->exec(); + } + + public function setDelete($url, $query_parameters = [], $data = []) { if (is_array($url)) { $data = $query_parameters; @@ -295,7 +301,6 @@ class Curl extends BaseCurl if (!empty($data)) { $this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data)); } - return $this->exec(); } /** @@ -605,6 +610,12 @@ class Curl extends BaseCurl * @return mixed Returns the value provided by exec. */ public function get($url, $data = []) + { + $this->setGet($url, $data); + return $this->exec(); + } + + public function setGet($url, $data = []) { if (is_array($url)) { $data = $url; @@ -613,7 +624,6 @@ class Curl extends BaseCurl $this->setUrl($url, $data); $this->setOptInternal(CURLOPT_CUSTOMREQUEST, 'GET'); $this->setOptInternal(CURLOPT_HTTPGET, true); - return $this->exec(); } /** @@ -642,6 +652,12 @@ class Curl extends BaseCurl * @return mixed Returns the value provided by exec. */ public function head($url, $data = []) + { + $this->setHead($url, $data); + return $this->exec(); + } + + public function setHead($url, $data = []) { if (is_array($url)) { $data = $url; @@ -650,7 +666,6 @@ class Curl extends BaseCurl $this->setUrl($url, $data); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'HEAD'); $this->setOpt(CURLOPT_NOBODY, true); - return $this->exec(); } /** @@ -661,6 +676,12 @@ class Curl extends BaseCurl * @return mixed Returns the value provided by exec. */ public function options($url, $data = []) + { + $this->setOptions($url, $data); + return $this->exec(); + } + + public function setOptions($url, $data = []) { if (is_array($url)) { $data = $url; @@ -668,7 +689,6 @@ class Curl extends BaseCurl } $this->setUrl($url, $data); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS'); - return $this->exec(); } /** @@ -679,6 +699,12 @@ class Curl extends BaseCurl * @return mixed Returns the value provided by exec. */ public function patch($url, $data = []) + { + $this->setPatch($url, $data); + return $this->exec(); + } + + public function setPatch($url, $data = []) { if (is_array($url)) { $data = $url; @@ -692,7 +718,6 @@ class Curl extends BaseCurl $this->setUrl($url); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH'); $this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data)); - return $this->exec(); } /** @@ -722,6 +747,12 @@ class Curl extends BaseCurl * [3] http://php.net/ChangeLog-5.php#5.5.11 */ public function post($url, $data = '', $follow_303_with_post = false) + { + $this->setPost($url, $data, $follow_303_with_post); + return $this->exec(); + } + + public function setPost($url, $data = '', $follow_303_with_post = false) { if (is_array($url)) { $follow_303_with_post = (bool)$data; @@ -746,7 +777,6 @@ class Curl extends BaseCurl $this->setOpt(CURLOPT_POST, true); $this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data)); - return $this->exec(); } /** @@ -757,6 +787,12 @@ class Curl extends BaseCurl * @return mixed Returns the value provided by exec. */ public function put($url, $data = []) + { + $this->setPut($url, $data); + return $this->exec(); + } + + public function setPut($url, $data = []) { if (is_array($url)) { $data = $url; @@ -773,7 +809,6 @@ class Curl extends BaseCurl if (!empty($put_data)) { $this->setOpt(CURLOPT_POSTFIELDS, $put_data); } - return $this->exec(); } /** @@ -784,6 +819,12 @@ class Curl extends BaseCurl * @return mixed Returns the value provided by exec. */ public function search($url, $data = []) + { + $this->setSearch($url, $data); + return $this->exec(); + } + + public function setSearch($url, $data = []) { if (is_array($url)) { $data = $url; @@ -800,7 +841,6 @@ class Curl extends BaseCurl if (!empty($put_data)) { $this->setOpt(CURLOPT_POSTFIELDS, $put_data); } - return $this->exec(); } /** diff --git a/tests/run_phpunit.sh b/tests/run_phpunit.sh index 40060d7..46d1e1c 100755 --- a/tests/run_phpunit.sh +++ b/tests/run_phpunit.sh @@ -74,13 +74,13 @@ export PHP_CURL_CLASS_TEST_MODE_ENABLED="yes" # Start test servers. Run servers on different ports to allow simultaneous # requests without blocking. server_count=7 -declare -A pids -for i in $(seq 0 $(("${server_count}" - 1))); do +pids=() +for i in $(seq 0 "$(echo "${server_count} - 1" | bc)"); do port=8000 (( port += $i )) php -S "127.0.0.1:${port}" server.php &> /dev/null & - pids["${i}"]="${!}" + pids+=("${!}") done # Determine which phpunit to use.