如何解决错误:从对端 php GuzzleHttp 接收数据时失败
我正在尝试发出帖子请求并且帖子请求正在运行,但我没有收到回复
const iconsSet: Record<IconName,Partial<Record<IconSize,IconConfig>>> = {
icon1: {
s1: {
source: 0,size: IconSize.s1
}
},icon2: {
s1: {
source: 0,size: IconSize.s1
},s2: {
source: 0,size: IconSize.s2
},},};
我失眠的原因
$client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Basic ' . 'token==']]);
$data = $client->post(
'url',[
'form_params' => [
'address' => 'addreww','name' => 'Zia Sultan','phone_number' => '2136000000',]
]
);
return $data;
解决方法
您的代码正在运行,post
方法返回 ResponseInterface
,我们需要从中获取内容,首先需要通过调用 StreamInterface
获取 getBody()
并链接它会 getContents()
给我们实际的响应。保持debug
模式开启,以找到Error: Failure when receiving data from the peer
的确切错误,并在发生此错误时与我们分享整个跟踪
try {
$response = (new \GuzzleHttp\Client())->post(
'url',[
'headers' => [
'Authorization' => 'Basic ' . 'token=='
],'form_params' => [
'address' => 'addreww','name' => 'Zia Sultan','phone_number' => '2136000000',],// 'http_errors' => false,// Set to false to disable throwing exceptions on an HTTP protocol errors (i.e.,4xx and 5xx responses)
// 'debug' => true,// 'connect_timeout' => 30 // number of seconds to wait while trying to connect to a server,Use 0 to wait indefinitely (the default behavior)
// 'read_timeout' => 10 // timeout to use when reading a streamed body,default value is default_socket_timeout in php.ini
// 'timeout' => 30 // the total timeout of the request in seconds. Use 0 to wait indefinitely (the default behavior).
]
);
return $response->getBody()->getContents();
} catch (Throwable $exception) {
print_r($exception);
}
,
我只是返回数据,但我需要像这样返回 getBody()
$data->getBody()
它现在工作
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。