0

我在 symfony 上使用 httpClient 我正在调用一个 API 我想使用 json 选项而不是使用 body 但它不起作用,当我使用 body 并输入 json 格式时一切正常但我找不到它很干净,所以我不想使用 json 选项,它只适用于简单的变量,如 json => ['var1' => 'value1, 'var2' => 'value2'...]

但是,一旦我使用数组,它就无法工作,并且出现此错误:

The type of the key "firstname" must be "int", "string" given.

请参阅下面的代码

$procedure = $this->httpClient->request(
        'POST',
        "https://fakeurl.com",
        [
          'headers' =>
            [
              'Accept' => 'application/json',
              'Content-Type' => 'application/json',
            ],
          'auth_bearer' => "key",
          'json' => [
            "name" => "name",
            "description" => "description",
            "start"  => true,
            "members" => [
                "firstname" => $user->getFirstName(),
                "lastname" => $user->getLastName(),
                "email" => $user->getEmail(),
                "phone" =>"+3312345678",
                "fileObjects" => [
                  "file" =>$file['id']
               ]
             ]
          ]
        ]
      );
4

1 回答 1

0

我刚刚找到了解决方案,适当的“成员”期望一个数组,所以正确的方法是这样的:

$procedure = $this->httpClient->request(
        'POST',
        "https://fakeurl.com",
        [
          'headers' =>
            [
              'Accept' => 'application/json',
              'Content-Type' => 'application/json',
            ],
          'auth_bearer' => "key",
          'json' => [
            "name" => "name",
            "description" => "description",
            "start"  => true,
            "members" => [[
                "firstname" => $user->getFirstName(),
                "lastname" => $user->getLastName(),
                "email" => $user->getEmail(),
                "phone" =>"+3312345678",
                "fileObjects" => [[
                  "file" =>$file['id']
               ]]
             ]]
          ]
        ]
      );
于 2021-12-10T16:44:12.747 回答