Skip to content

Commit ae28d27

Browse files
author
Wazabii
committed
Parsed body improvements
1 parent 460ea68 commit ae28d27

File tree

6 files changed

+75
-307
lines changed

6 files changed

+75
-307
lines changed

Client.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPFuse\Http\Interfaces\RequestInterface;
77
use PHPFuse\Http\Interfaces\ResponseInterface;
88
use PHPFuse\Http\Interfaces\ClientInterface;
9+
use PHPFuse\Http\Interfaces\StreamInterface;
910

1011
use PHPFuse\Http\Exceptions\ClientException;
1112
use PHPFuse\Http\Exceptions\RequestException;
@@ -177,11 +178,7 @@ protected function post(): void
177178
*/
178179
protected function put(): void
179180
{
180-
$fh = fopen('php://memory', 'rw');
181-
fwrite($fh, $this->requestData);
182-
rewind($fh);
183-
184-
curl_setopt($this->ch, CURLOPT_INFILE, $fh);
181+
curl_setopt($this->ch, CURLOPT_INFILE, $this->createParsedBody()->getResource());
185182
curl_setopt($this->ch, CURLOPT_INFILESIZE, $this->requestDataLength);
186183
curl_setopt($this->ch, CURLOPT_PUT, true);
187184
}
@@ -230,4 +227,16 @@ private function buildHeaders(RequestInterface $request): void {
230227
}
231228
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $data);
232229
}
230+
231+
/**
232+
* Parsed body can be used in e.g. put method
233+
* @return StreamInterface
234+
*/
235+
private function createParsedBody(): StreamInterface
236+
{
237+
$stream = new Stream('php://memory', 'rw');
238+
$stream->write($this->requestData);
239+
$stream->rewind();
240+
return $stream;
241+
}
233242
}

ClientRequest.php

Lines changed: 0 additions & 258 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@ The library is fully integrated with PSR Http Message and designed for use with
44
## Request
55
None of the construct attributes are required and will auto propagate if you leave them be.
66
```php
7-
$request = new Http\ServerRequest(
8-
(array) Cookies,
9-
(array) QueryParams,
10-
(array) Files,
11-
(array) ParsedBody,
12-
(array) Attribute,
13-
(UriInterface) Uri
14-
);
7+
$request = new Http\ServerRequest(UriInterface $uri, EnvironmentInterface $env);
158
```
169
#### Get request method
1710
```php
@@ -87,8 +80,8 @@ $client = new Http\Client([CURLOPT_HTTPAUTH => CURLAUTH_DIGEST]); // Pass on Cur
8780

8881
// Create request data
8982
$request = new Http\Request(
90-
"POST", // The HTTP Method
91-
"https://admin:mypass@wazabii.se:443/test.php", // The Request URI
83+
"POST", // The HTTP Method (GET, POST, PUT, DELETE, PATCH)
84+
"https://admin:mypass@example.com:443/test.php", // The Request URI
9285
["customHeader" => "lorem"], // Add Headers, empty array is allowed
9386
["email" => "john.doe@example.com"] // Post data
9487
);

Request.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class Request extends Message implements RequestInterface
1919
protected $body;
2020

2121
function __construct(
22-
string $method, UriInterface|string $uri,
22+
string $method,
23+
UriInterface|string $uri,
2324
HeadersInterface|array $headers = [],
2425
StreamInterface|array|string|null $body = NULL
2526
) {

0 commit comments

Comments
 (0)