laravel 中的 Guzzle 不断加载,不返回值

Guzzle in laravel keeps loading, not returning values

我使用 Laravel 护照进行 API 身份验证。我有两条路线 /api/login 和 /oauth/token

因为我不能对我的客户端 ID 进行硬编码并且登录从 JS 接收并且参数和客户端 ID 在登录方法中被硬编码(laravel),我尝试使用 Guzzle post 值(6.0) 至 oauth/token(POST 个请求)。

我关注了一个 youtube 视频,它在那里有效,但我的无效。我正在使用 5.6,不确定视频中的哪个版本。有人可以帮忙吗?

下面是视频

https://www.youtube.com/watch?v=HGh0cKEVXPI&t=838s

下面是代码

$http = new GuzzleHttp\Client();
$request = $http->post(URI, '/oauth/token', [
    'form_params' => [
        'username' => 'bar',
        'password' => 'xxxxxx',
        'client_id' => 2,
        'grant_type' => 'password',
        'client_secret' => '00000000000000000'
    ]
]);

return $request;

您没有收到仅返回 guzzle $request 初始化的响应,因此请添加 getBody()

   $http = new GuzzleHttp\Client();
    $request = $http->post(URI, '/oauth/token', [
        'form_params' => [
            'username' => 'bar',
            'password' => 'xxxxxx',
            'client_id' => 2,
            'grant_type' => 'password',
            'client_secret' => '00000000000000000'
        ]
    ]);

    return $request->getBody();

您应该先检查状态,以确保一切正常,方法是使用

$request->getStatusCode(); 

您可以通过

得到您的回复
$request->getBody();

您还可以从此处查看使用 GuzzulHttp 的完整文档 http://docs.guzzlephp.org/en/stable/

我认为您正试图在 build-in 服务器中请求。 所以你尝试两个服务器来发送请求。 它会起作用的。

喜欢 localhost:8000localhost:9000 服务器

使用这个命令

php artisan 服务

php artisan 服务 --port=9000

谢谢。