laravel 使用 curl 或 guzzle 的 passport oauth 功能

laravel passport oauth function using curl or guzzle

我在 api 路由文件中有这条路由

Route::post('/user/register', 'HomeController@register');

注册函数是

public function register(Request $request)
 {
    $user = User::create([
        ...
    ]);
    return $this->getAccessToken($request);
}

public function getAccessToken(Request $request)
{
    $url = url('/oauth/token');
    $headers = ['Accept' => 'application/json'];
    $http = new Client;

    $response = $http->post($url, [
        'headers' => $headers,
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => 'my_client_id',
            'client_secret' => 'my_client_secret',
            'username' => 'username',
            'password' => 'password'
        ],
    ]);

    return json_decode((string)$response->getBody(), true);
}

现在,当我向 url 发送 post 请求时,用户已创建,但请求一直在加载并且没有响应,整个应用程序挂起,直到我关闭 IDE - phpstorm - 我将相同的参数从 postman 发送到相同的 url /oauth/token,它工作正常。 任何帮助或任何人告诉我我错过了什么?

问题是 curl 在 url 包含类似

的端口时不起作用

本地主机:8000

在 Stack Overflow 上查看这个问题:

Why does file_get_contents work with google.com but not with my site?

For anyone having this problem using PHP Built-in web server (with Laravel in my case), it is caused by your request being blocked by file_get_contents() / curl functions.

Docs of dev server say that

PHP applications will stall if a request is blocked.

Since the PHP built-in server is single threaded, requesting another url on your server will halt first request and it gets timed out.

As a solution, you can use nginx (LEMP stack) or other web servers.

Edit: As of now, I really suggest you to use Homestead as development environment for PHP projects. It saves you lots of work with configuration, creation of virtual hosts and DB configuration for more projects.