如何通过在 laravel 中传递参数来获取内容类型 application/x-www-form-urlencoded 的响应

How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel

我已经在 Laravel https://sso/{custom_path}/token 中使用了 Api 单点登录选项 Api 创建使用智威汤逊。 最后,在 Web 应用程序中,使用 http 客户端 guzzle 将 header 中的访问令牌和内容类型传递给 Api 调用。 内容类型 application/x-www-form-urlencoded,参数在 form_params 中。 但作为回应,我失踪了 grant_type。当我在 form_parms 数组中传递 grant_type 时。有没有其他方法可以解决这个问题。任何有价值的回复都会被考虑。

代码:

$uri = $this->userTokenAuthencticateUrl();
        $token = session('token')->access_token;
        $params['header'] = [
            "Content-Type: application/x-www-form-urlencoded",
            "Authorization: Bearer $token"
            ];
        $params['form_params'] = array(
                'grant_type' => 'xxxxx',
                'response_include_resource_name' => 'xxx',
                'audience' => 'xxxx', 
                'permission' => 'xxxxxx',
            );
            $response = Http::post($uri, $params);
            dd($response->json());

回复:

array:2 [▼
  "error" => "invalid_request"
  "error_description" => "Missing form parameter: grant_type"
]

因为您正在使用 HTTP 客户端。你需要改变你的代码。您不需要在 header 中将 Content-Type 作为 application/x-www-form-urlencoded 传递,我相信授权令牌是在 header 中单独传递的,您可以将其传递到参数中。

$uri = $this->userTokenAuthencticateUrl();
$token = session('token')->access_token;

 $params = array(
        'grant_type' => 'xxxxx',
        'response_include_resource_name' => 'xxx',
        'audience' => 'xxxx', 
        'permission' => 'xxxxxx',
 );
 $response = Http::asForm()->withHeaders([
        'Authorization' => 'Bearer ' . $token
     ])->post($uri, $params);

 dd($response->json());

方法二:

文档中也提到了

If you would like to quickly add an Authorization bearer token header to the request, you may use the withToken method so you can do like this as well

$uri = $this->userTokenAuthencticateUrl();
$token = session('token')->access_token;

 $params = array(
        'grant_type' => 'xxxxx',
        'response_include_resource_name' => 'xxx',
        'audience' => 'xxxx', 
        'permission' => 'xxxxxx',
 );
 $response = Http::asForm()->withToken($token)->post($uri, $params);

 dd($response->json());

有关详细信息,请参阅 doc


方法三:

你甚至可以直接使用 guzzle 。
define("form_params", \GuzzleHttp\RequestOptions::FORM_PARAMS );
try{
  $client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Bearer ' . $token]]);
  
  $guzzleResponse = $client->post(
                $api_url, [
                'form_params' => [
                    'grant_type' => 'xxxxx',
                    'response_include_resource_name' => 'xxx',
                    'audience' => 'xxxx', 
                    'permission' => 'xxxxxx'
                ]
            ]);
    if ($guzzleResponse->getStatusCode() == 200) {
         $response = json_decode($guzzleResponse->getBody(),true);
         //perform your action with $response 
    } 
}
catch(\GuzzleHttp\Exception\RequestException $e){
   // you can catch here 400 response errors and 500 response errors
   // see this https://whosebug.com/questions/25040436/guzzle-handle-400-bad-request/25040600
}catch(Exception $e){
   //other errors 
}