Guzzle 5.3:如果大于约1MB,则无法发布JSON正文 [英] Guzzle 5.3: Unable to POST JSON body if larger than ~1MB
问题描述
我正在通过Guzzle Services使用Guzzle 5.3(通过 https://github.com/ticketevolution/ticketevolution -php ),以尝试使用包含编码为base64的PDF的JSON主体将消息发布到API端点.当身体小于〜1MB时,它可以正常工作.当尸体较大时,似乎尸体永远不会被送出.
I'm using Guzzle 5.3 via Guzzle Services (via https://github.com/ticketevolution/ticketevolution-php) to attempt to POST to an API endpoint with a JSON body that includes a PDF encoded as base64. When the body is less than ~1MB it works fine. When the body is larger it seems that the body is never sent.
我已经在带有和不带有Expect:100标头的情况下进行了测试,但这似乎没有什么不同.
I have tested this with and without the Expect: 100 header and it does not seem to make a difference.
我已经使用Transfer-Encoding:分块进行了测试,但是因为API需要整个POST主体才能使用分块进行身份验证,所以不起作用.
I have tested with Transfer-Encoding: chunked, but because the API needs the entire POST body in order to authenticate using chunked does not work.
我们已经在客户端和应用服务器之间的负载平衡情况下进行了测试.
We have tested with and without the load balance between the client and the appservers.
从所有信息来看,当它大于1MB时,才不会发送邮件.
From everything we can tell the body just doesn't get sent when it is larger than ~1MB.
有没有人知道如何使Guzzle 5.3甚至大于1MB的物体也可以发送?
Does anyone have any ideas on how to get Guzzle 5.3 to send the body even when it is larger than 1MB?
下面是日志输出
[2015-09-01 16:15:43] TEvoAPIClientLogger.CRITICAL:
>>>>>>>>
POST /v9/orders/2100732/deliver_etickets HTTP/1.1
Host: api.ticketevolution.com
User-Agent: ticketevolution-php/3.0.0dev Guzzle/5.3.0 curl/7.44.0 PHP/5.5.28
Content-Type: application/json
Content-Length: 1387036
X-Token: b47dsd8c0ab80a1e2bc24sc341415a2f
X-Signature: SwBOkdUOqG3SDtjVwi2etosdP+gppwuV5dCq8yMw9lM=
{"etickets":[{"item_id":1513651,"eticket":"JVBERi0xLjQKJeLjz9MKNCAwIG9iaiBbXQplb… [a whole lot of base64 snipped] …NwolJUVPRgo="}]}
<<<<<<<< --------
cURL error 52: Empty reply from server
推荐答案
Running into the same problem, a bit of debugging resulted in ending up at GuzzleHttp\Ring\Client\CurlFactory::applyBody()
, and then this fixed the problem for me:
$client = new \GuzzleHttp\Client([
'defaults' => [
'config' => [
'curl' => [
'body_as_string' => true,
],
],
],
]);
在发出请求时设置配置
$client->post('https://example.com', [
'json' => $json,
'config' => [
'curl' => [
'body_as_string' => true,
],
],
]);
倒带先前获取的实际内容流
由于我要从远程服务器获取内容,因此 Matt Downling的文章帮助我发现,在将其用作multipart/form-data
请求的一部分之前,需要先倒回实际的流:
Rewinding the previously fetched actual content stream
Since I'm fetching content from a remote server, this article by Matt Downling helped me in finding out that I need to rewind the actual stream before using it as a part of the multipart/form-data
request:
$response->getBody()->seek(0);
这篇关于Guzzle 5.3:如果大于约1MB,则无法发布JSON正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!