laravel guzzle 无法与 amadeus 合作 api

laravel guzzle not working with amadeus api

我正在使用 Guzzle 从 Amadeus 检索数据。它与 Postman 一起工作正常,如果我用 Ajax 调用它也工作正常但是当我想从控制器检索数据时它说请求错误。

邮递员

    public function agentsTicket(Request $request)
    {
        $client = new Client();

        try {
            $res = $client->Get('https://test.api.amadeus.com/v2/shopping/flight-offers', [

                'headers' => [
                    'Authorization' => ['Bearer','123456789'],
                ],
                'form_params' => [
                    "originLocationCode" => "SYD",
                    "destinationLocationCode" => "BKK",
                    "departureDate" => "2021-11-01",
                    "returnDate" => "2021-11-18",
                    "adults" => "2",
                    "max" => "1",
                ]
            ]);

            $res = json_decode($res->getBody()->getContents(), true);
            dd($res);
        } catch (\Exception $e) {
            $response = $res->getResponse();
            $result =  json_decode($response->getBody()->getContents());
            return response()->json(['data' => $result]);
        }
    }

我找到第一个问题的解决方案是将授权从数组更改为字符串。 第二个问题是 'form_params' 正如 #aynber 所说,我将其更改为查询,现在一切正常。

    public function agentsTicket(Request $request)
    {
        $client = new Client();

        try {
            $res = $client->Get('https://test.api.amadeus.com/v2/shopping/flight-offers', [

                'headers' => [
                    'Authorization' => 'Bearer 123456789',
                ],
                'query' => [
                    "originLocationCode" => "SYD",
                    "destinationLocationCode" => "BKK",
                    "departureDate" => "2021-11-01",
                    "returnDate" => "2021-11-18",
                    "adults" => "2",
                    "max" => "1",
                ]
            ]);

            $res = json_decode($res->getBody()->getContents(), true);
            dd($res);
        } catch (\Exception $e) {
            $response = $res->getResponse();
            $result =  json_decode($response->getBody()->getContents());
            return response()->json(['data' => $result]);
        }
    }