如何从 guzzle 使用 auth 和 body raw 获得响应?

How to get response from guzzle use auth and body raw?

我在邮递员中尝试这样:

我填写刚刚输入的密码。然后我点击按钮更新请求

这样的观点:

这是header:

这是body。我 select 原始数据和输入数据是这样的:

然后我点击发送按钮,它可以得到响应

但是当我尝试像这样使用 guzzle 6 时:

public function testApi()
{
    $requestContent = [
        'headers' => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json'
        ],
        'json' => [
            'auth' => ['', 'b0a619c152031d6ec735dabd2c6a7bf3f5faaedd'],
            'ids' => ["1"]
        ]
    ];
    try {
        $client = new GuzzleHttpClient();
        $apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent);
        $response = json_decode($apiRequest->getBody());
        dd($response);
    } catch (RequestException $re) {
          // For handling exception.
    }
}

结果为空

我该如何解决这个问题?

在 Postman 中看到,您在 "headers" 选项卡中正确指定了字段 Authorization。所以当你使用 Guzzle 时应该是一样的,把它放在 headers:

public function testApi()
{
    $requestContent = [
        'auth' => ['username', 'password']
        'headers' => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
        ],
        'json' => [
            'ids' => ["1"]
        ]
    ];

    try {
        $client = new GuzzleHttpClient;
        $apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent);
        $response = json_decode($apiRequest->getBody());
        dd($response);
    } catch (RequestException $re) {
          // For handling exception.
    }
}