From 7eb6938742072025fb13556fe54bfbcea1d7d526 Mon Sep 17 00:00:00 2001 From: Guilherme Branco Stracini Date: Wed, 2 Oct 2024 19:05:10 +0100 Subject: [PATCH 1/4] Update Request.php --- src/Request.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Request.php b/src/Request.php index 97312f3..a59f475 100644 --- a/src/Request.php +++ b/src/Request.php @@ -47,6 +47,7 @@ private function execute($fields): stdClass $response = curl_exec($curl); $result = new stdCLass(); $result->url = $fields[CURLOPT_URL]; + $result->requestHeaders = $fields[CURLOPT_HTTPHEADER]; if ($response === false) { $error = curl_error($curl); @@ -97,7 +98,9 @@ public function post($url, $data, $headers = array()): stdClass { $fields = $this->getFields($url, $headers); $fields[CURLOPT_CUSTOMREQUEST] = "POST"; - $fields[CURLOPT_POSTFIELDS] = $data; + if ($data !== null) { + $fields[CURLOPT_POSTFIELDS] = $data; + } return $this->execute($fields); } @@ -105,7 +108,9 @@ public function put($url, $data, $headers = array()): stdClass { $fields = $this->getFields($url, $headers); $fields[CURLOPT_CUSTOMREQUEST] = "PUT"; - $fields[CURLOPT_POSTFIELDS] = $data; + if ($data !== null) { + $fields[CURLOPT_POSTFIELDS] = $data; + } return $this->execute($fields); } @@ -113,7 +118,7 @@ public function delete($url, $headers = array(), $data = null): stdClass { $fields = $this->getFields($url, $headers); $fields[CURLOPT_CUSTOMREQUEST] = "DELETE"; - if ($data != null) { + if ($data !== null) { $fields[CURLOPT_POSTFIELDS] = $data; } return $this->execute($fields); @@ -123,7 +128,9 @@ public function patch($url, $data, $headers = array()): stdClass { $fields = $this->getFields($url, $headers); $fields[CURLOPT_CUSTOMREQUEST] = "PATCH"; - $fields[CURLOPT_POSTFIELDS] = $data; + if ($data !== null) { + $fields[CURLOPT_POSTFIELDS] = $data; + } return $this->execute($fields); } From ce4185634377aa63df611ccbbcc3467880b1bbcf Mon Sep 17 00:00:00 2001 From: Guilherme Branco Stracini Date: Wed, 2 Oct 2024 19:08:35 +0100 Subject: [PATCH 2/4] Update Request.php --- src/Request.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Request.php b/src/Request.php index a59f475..2fa909b 100644 --- a/src/Request.php +++ b/src/Request.php @@ -47,7 +47,6 @@ private function execute($fields): stdClass $response = curl_exec($curl); $result = new stdCLass(); $result->url = $fields[CURLOPT_URL]; - $result->requestHeaders = $fields[CURLOPT_HTTPHEADER]; if ($response === false) { $error = curl_error($curl); From 0461a6fd11cadb43f54683a2097cf96f5144ea4f Mon Sep 17 00:00:00 2001 From: Guilherme Branco Stracini Date: Wed, 2 Oct 2024 19:09:07 +0100 Subject: [PATCH 3/4] Update changelog.md --- docs/changelog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/changelog.md b/docs/changelog.md index 7c0d04c..b3d56d2 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,9 @@ # Changelog +## Version 0.11 [2024-10-02] + +- Enhance the Request class with optional data parameter by [@guibranco](https://github.com/guibranco). + ## Version 0.10 [2024-09-23] - [#84](https://github.com/guibranco/pancake/issues/84) - Add MemoryCache class by [@GitAuto](https://github.com/apps/gitauto-ai). From 6505df32ad844e51735b4567d14d800ce437fe6d Mon Sep 17 00:00:00 2001 From: Guilherme Branco Stracini Date: Wed, 2 Oct 2024 19:17:09 +0100 Subject: [PATCH 4/4] Update request.md --- docs/user-guide/request.md | 88 ++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 13 deletions(-) diff --git a/docs/user-guide/request.md b/docs/user-guide/request.md index 616900d..c059070 100644 --- a/docs/user-guide/request.md +++ b/docs/user-guide/request.md @@ -9,8 +9,14 @@ - [Available HTTP verbs](#available-http-verbs) - [Get](#get) - [Post](#post) + - [Post without payload](#post-without-payload) + - [Post with payload](#post-with-payload) - [Put](#put) + - [Put without payload](#put-without-payload) + - [Put with payload](#put-with-payload) - [Patch](#patch) + - [Patch without payload](#patch-without-payload) + - [Patch with payload](#patch-with-payload) - [Delete](#delete) - [Delete without payload](#delete-without-payload) - [Delete with payload](#delete-with-payload) @@ -20,7 +26,7 @@ ## About -This class is responsible for doing [cURL](https://www.php.net/manual/en/book.curl.php) request with custom headers. +This class is responsible for doing [cURL](https://www.php.net/manual/en/book.curl.php) requests with custom headers. ## Requirements @@ -30,7 +36,7 @@ This requires lib curl to be active with your PHP settings. ### Get -Performs HTTP GET request with custom headers. +Performs HTTP GET requests with custom headers. ```php @@ -47,15 +53,34 @@ if ($response->statusCode >= 200 && $response->statusCode < 300) { ### Post -Performs HTTP POST request with custom headers. +#### Post without payload + +Performs HTTP POST requests with custom headers. ```php $headers = array("User-Agent: test/1.0", "Accept: application/json"); -$payload = array("Foo" => "Bar"); $request = new Request(); -$response = $request->post("http://example.com/", json_encode($payload), $headers); +$response = $request->post("https://example.com/", null, $headers); + +if ($response->statusCode >= 200 && $response->statusCode < 300) { + echo $response->body; +} + +``` + +#### Post with payload + +Performs HTTP POST requests with custom headers and a payload. + +```php + +$headers = array("User-Agent: test/1.0", "Accept: application/json"); +$payload = array("some" => "thing"); + +$request = new Request(); +$response = $request->post("https://example.com/", json_encode($payload), $headers); if ($response->statusCode >= 200 && $response->statusCode < 300) { echo $response->body; @@ -65,12 +90,31 @@ if ($response->statusCode >= 200 && $response->statusCode < 300) { ### Put -Performs HTTP PUT request with custom headers. +#### Put without payload + +Performs HTTP PUT requests with custom headers. + +```php + +$headers = array("User-Agent: test/1.0", "Accept: application/json"); + +$request = new Request(); +$response = $request->put("https://example.com/", null, $headers); + +if ($response->statusCode >= 200 && $response->statusCode < 300) { + echo $response->body; +} + +``` + +#### Put with payload + +Performs HTTP PUT requests with custom headers and a payload. ```php $headers = array("User-Agent: test/1.0", "Accept: application/json"); -$payload = array("Foo" => "Rab"); +$payload = array("some" => "thing"); $request = new Request(); $response = $request->put("https://example.com/", json_encode($payload), $headers); @@ -83,10 +127,28 @@ if ($response->statusCode >= 200 && $response->statusCode < 300) { ### Patch -Performs HTTP PATCH request with custom headers. +#### Patch without payload + +Performs HTTP PATCH requests with custom headers. ```php +$headers = array("User-Agent: test/1.0", "Accept: application/json"); + +$request = new Request(); +$response = $request->patch("https://example.com/", null, $headers); + +if ($response->statusCode >= 200 && $response->statusCode < 300) { + echo $response->body; +} + +``` + +#### Patch with payload + +Performs HTTP PATCH requests with custom headers and a payload. + +```php $headers = array("User-Agent: test/1.0", "Accept: application/json"); $payload = array("some" => "thing"); @@ -104,7 +166,7 @@ if ($response->statusCode >= 200 && $response->statusCode < 300) { #### Delete without payload -Performs HTTP DELETE request with custom headers. +Performs HTTP DELETE requests with custom headers. ```php @@ -121,7 +183,7 @@ if ($response->statusCode >= 200 && $response->statusCode < 300) { #### Delete with payload -Performs HTTP DELETE request with custom headers and a payload. +Performs HTTP DELETE requests with custom headers and a payload. ```php @@ -139,7 +201,7 @@ if ($response->statusCode >= 200 && $response->statusCode < 300) { ### Options -Performs HTTP OPTIONS request with custom headers. +Performs HTTP OPTIONS requests with custom headers. ```php @@ -158,7 +220,7 @@ if ($response->statusCode >= 200 && $response->statusCode < 300) { ### Head -Performs HTTP HEAD request with custom headers. +Performs HTTP HEAD requests with custom headers. ```php @@ -177,7 +239,7 @@ if ($response->statusCode >= 200 && $response->statusCode < 300) { ## Troubleshooting -If for some reason the request could not be completed, the result status code will be `-1` and an error property will be set with the content of `curl_error` response. +If the request could not be completed for some reason, the result status code will be `-1`, and an error property will be set with the content of the `curl_error` response. ```php