问题
I have this route in api route file
Route::post('/user/register', 'HomeController@register');
and the register function is
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);
}
now when I send post request to the url the user is created but the request keep loading and no response and the whole application is hanging until I close the IDE - phpstorm -
I send the same parameters to the same url /oauth/token
from postman and it works fine.
any help or any one tell me what I am missing ?
回答1:
the problem was the curl do not work when the url contains port like
localhost:8000
回答2:
See this question on 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.
来源:https://stackoverflow.com/questions/41773321/laravel-passport-oauth-function-using-curl-or-guzzle