繁体   English   中英

用Guzzle 4发布POST多部分/表单数据

[英]POST multipart/form-data with Guzzle 4

我正在使用Guzzle 4,并且想发布包含multipart / form-data的文件。 我不确定哪种方法是正确的。

我试过了:

$client = new GuzzleHttp\Client();

$request = $client->createRequest('POST', 'https://127.0.0.1/1/files/', ['json' => ['id' => 'dc51bfe4-92af-483d-80c4-f2da4cafa723'], 'cookies' => true]);

$request->getBody()->addFile(new PostFile('a.pdf', fopen('a.pdf', 'rb')));
$response = $this->client->send($request);

我收到此错误:

Fatal error: Call to undefined method GuzzleHttp\Stream\Stream::addFile()

如果将addFile替换为addPostFile,也会发生相同的情况。

有任何想法吗?

从未使用过Guzzle,但我尝试了这段代码,并能够通过替换来解决它

$request = $client->createRequest('POST', 'https://127.0.0.1/1/files/', ['json' => ['id' => 'dc51bfe4-92af-483d-80c4-f2da4cafa723'], 'cookies' => true]);

有了这个

$request = $client->createRequest('POST', 'https://127.0.0.1/1/files/', ['cookies' => true]);

您可能可以在createRequest之后传递json数据。 您应该查看Guzzle文档。

另外,您似乎同时使用$client$this->client 我认为您可能应该只选择两者之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM