api请求在邮递员中有效,但在带有Guzzle的php代码中无效,多个请求

时间:2019-10-01 22:44:49

标签: php laravel guzzle

我正在尝试向API发出两个请求。

我第一次请求获取Api令牌,我等待获取令牌响应,然后提出第二个请求。


        $result = $token->post($uriToken, [
            'form_params' => [
                'grant_type' => 'client_credentials',
                'client_id' => '25',
                'client_secret' => 'l6gLGd4SXuOCnYgUNDqkIMXL9djaEVZLqTqevM8U',
                'scope' => '*'
            ]
        ]);

        $tokenResult = json_decode($result->getBody()->getContents())->access_token;

if($tokenResult){
     $cli = new Client([
            'headers' => [
                'Accept' => 'application/json',
                'Authorization' => 'Bearer ' . $tokenResult,
                'Content-Type' => 'application/json',
            ]
        ]);

        $post = $cli->requestAsync('POST', $uriClient,
            [
                'json' =>
                    [
                        'dni' => $dni,
                        'target' => $target
                    ]
            ])->wait();
}

with this code, I always get 400 error.

When I made the first request in Postman and copy in code, it sometimes works and other get 400 error Bad Request

0 个答案:

没有答案