错误:从对端 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()

它现在工作