传递给 Guzzle 的 createRequest 方法时类型未知的资源

Resource of type unknown when passed to Guzzle's createRequest method

当我在 $option 参数的 body 元素中为 createRequest 传递资源引用时,Guzzle 5.3.0 将资源类型 stream 转换为 unknown。这会使资源无法使用并导致 Guzzle 内部出现异常。

例如

$fp = fopen('path/to/a/file.txt');

$request = $client->createRequest('POST', $someUrl, ['body' => $fp]);

就在 createRequest 之前,$fp 的类型将是 stream。但在该行之后,$fp 的类型为 unknown.

Guzzle 在发送请求之前在内部将流转换为 GuzzleHttp\Stream,但不知为何它每次都被破坏了。

调用 $client->send($request) 时会导致异常 fstat(): 501 is not a valid stream resource

可能是什么原因导致的,我该如何解决?

您可以使用 Stream 对象来创建请求。

首先添加Streamclass

use GuzzleHttp\Stream\Stream;

从文件创建流对象并将其用于创建请求。

$stream = Stream::factory(fopen('path/to/a/file.txt'));

$request = $client->createRequest('POST', $someUrl, ['body' => $stream ]);