Nyholm / psr7

A super lightweight PSR-7 implementation
MIT License
1.16k stars 75 forks source link

No content returned from StreamInterface with `Nyholm\Psr7\Factory\HttplugFactory\Factory\HttplugFactory` #189

Closed trailsnail closed 1 year ago

trailsnail commented 2 years ago

Hello all,

since we have a dependency to Nyholm/Psr7 in our project, we get an error with SOAP requests via phpro/soap-client.

PHP Fatal error:  Uncaught ValueError: DOMDocument::loadXML(): Argument #1 ($source) must not be empty in C:\dvl\DefaultWorkspace\soap-error-streamfactory\vendor\phpro\soap-clie
nt\src\Phpro\SoapClient\Xml\Xml.php:93
Stack trace:
0 C:\dvl\DefaultWorkspace\soap-error-streamfactory\vendor\phpro\soap-client\src\Phpro\SoapClient\Xml\Xml.php(93): DOMDocument->loadXML()
1 C:\dvl\DefaultWorkspace\soap-error-streamfactory\soap\DemoMiddleware.php(27): Phpro\SoapClient\Xml\Xml::fromStream()
2 C:\dvl\DefaultWorkspace\soap-error-streamfactory\vendor\phpro\soap-client\src\Phpro\SoapClient\Middleware\Middleware.php(25): dhl\DemoMiddleware->beforeRequest()
3 C:\dvl\DefaultWorkspace\soap-error-streamfactory\vendor\php-http\client-common\src\PluginChain.php(48): Phpro\SoapClient\Middleware\Middleware->handleRequest()
4 C:\dvl\DefaultWorkspace\soap-error-streamfactory\vendor\php-http\client-common\src\PluginChain.php(63): Http\Client\Common\PluginChain->Http\Client\Common\{closure}()#5 C:\dv
l\DefaultWorkspace\soap-error-streamfactory\vendor\php-http\client-common\src\PluginClient.php(90): Http\Client\Common\PluginChain->__invoke()
6 C:\dvl\DefaultWorkspace\soap-error-streamfactory\vendor\phpro\soap-client\src\Phpro\SoapClient\Soap\Handler\HttPlugHandle.php(85): Http\Client\Common\PluginClient->sendReques
t()
7 C:\dvl\DefaultWorkspace\soap-error-streamfactory\vendor\phpro\soap-client\src\Phpro\SoapClient\Soap\Engine\Engine.php(39): Phpro\SoapClient\Soap\Handler\HttPlugHandle->reques
t()
8 C:\dvl\DefaultWorkspace\soap-error-streamfactory\vendor\phpro\soap-client\src\Phpro\SoapClient\Client.php(107): Phpro\SoapClient\Soap\Engine\Engine->request()
9 C:\dvl\DefaultWorkspace\soap-error-streamfactory\soap\exampleClient.php(20): Phpro\SoapClient\Client->call()
10 C:\dvl\DefaultWorkspace\soap-error-streamfactory\example.php(90): dhl\exampleClient->createShipmentOrder()
11 {main}

To make it easier to check, I have created an example repository, just follow the info in the readme. If you have any questions, please contact me. We would be happy if someone could help us with the problem.

Thanks a lot

Link to repo: https://github.com/trailsnail/soap-error-streamfactory

staabm commented 2 years ago

I narrowed the problem further.

the problem is that the stream which is returned in from $request->getBody() does return a empty-string when invoked with with $request->getBody()->getContents() but it works when invoked with e.g. magic $request->getBody()->__toString()

it seems a similar problem was fixed in https://github.com/phpro/soap-client/pull/325/files (or worked arround)

panigrc commented 1 year ago

Copying the __toString() behavior, I solved it like this:

$response->getBody()->seek(0);
echo $response->getBody()->getContents();
nicolas-grekas commented 1 year ago

Your fix is the right one: PSR-7 doesn't specify whether we should seek or not, so it's your job to ensure the offset.

nicolas-grekas commented 1 year ago

Still, this should be fixed by #217