【问题标题】:Laravel server hangs whenever I try to request localhost:8000/any using guzzle每当我尝试使用 guzzle 请求 localhost:8000/any 时,Laravel 服务器都会挂起
【发布时间】:2017-12-06 08:50:35
【问题描述】:

如果我向http://localhost:8000http://127.0.0.1:8000 提出任何请求,它会挂起状态挂起。 (和这里一样https://github.com/guzzle/guzzle/issues/1857

有人告诉我这与 guzzle 无关,我最好在这里问一下。

我在关注 laravel.com/docs/5.4/passport 时偶然发现了这个问题

这是挂起的代码:

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'authorization_code',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'redirect_uri' => 'http://example.com/callback',
        'code' => $request->code,
    ],
]);

我尝试向有效的 API 路由发出 GET 和 POST 请求(用邮递员测试),但在使用 guzzle 调用相同的路由时它仍然挂起。

那么有没有办法在使用php artisan serve 时向我自己的 API 发出请求?

【问题讨论】:

    标签: php laravel localhost guzzle


    【解决方案1】:

    我最终通过使用 wamp virtualhost 而不是 php artisan serve 解决了这个问题。不知道为什么它不适用于 localhost。

    更新:有人好心解释了为什么它不起作用。

    https://github.com/guzzle/guzzle/issues/1857#issuecomment-506962175

    这是因为 php artisan serve 是一个单线程应用程序。因此,当我们使用 guzzle 向自身请求时,它基本上只是尝试先完成 guzzle 请求(作为客户端),然后再完成该请求(作为服务器),这是不可能的。

    更多信息:https://php.net/manual/en/features.commandline.webserver.php

    还有this answer:

    当调用自身时,线程阻塞等待自己的回复。解决方案是将提供应用程序和消费应用程序分离到它们自己的实例中,或者在 Apache 或 nginx 等多线程 Web 服务器上运行它。

    【讨论】:

    • 我遇到了完全相同的问题并以完全相同的方式解决了它,如果有人能解释这一点,那就太棒了
    【解决方案2】:
    /**
     * Login function
    */
    public function login(Request $request) { 
        
        /* 
            Sample post object
            {
                "username": "test@gmail.com",
                "password": "test123"
            }
        */
        if (Auth::attempt(['email' => request('username'), 'password' => request('password')])) { 
           
            return $this->getToken($request);
        } 
        else { 
            return response()->json(['error'=>'Unauthorised'], 401); 
        } 
    }
    
    public function getToken(Request $request) {
        //Get client ID and client Secret
        $client = DB::table('oauth_clients')->where('password_client',1)->first();
        $request->request->add([
            "grant_type" => "password",
            "username" => $request->username,
            "password" => $request->password,
            "client_id"     => $client->id,
            "client_secret" => $client->secret,
        ]);
        // Post to "/oauth/token
        $tokenRequest = $request->create('/oauth/token','post');
        $instance = Route::dispatch($tokenRequest);
        //return token_type, expires_in, access_token, refresh_token
        return response()->json(json_decode($instance->getContent()));
    
    }
    

    【讨论】:

      【解决方案3】:

      Carl 对此有一个很好的解决方案。如果您正在寻找快速修复来测试您的更新 - 您可以通过打开两个命令提示符来完成此操作。第一个将运行php artisan serve(本地我的默认端口是8000,您将在http://localhost:8000 上运行您的站点)。第二个将运行php artisan serve --port 8001

      然后您将发布请求更新为:

      $response = $http->post('http://localhost:8001/oauth/token', [
          'form_params' => [
              'grant_type' => 'authorization_code',
              'client_id' => 'client-id',
              'client_secret' => 'client-secret',
              'redirect_uri' => 'http://example.com/callback',
              'code' => $request->code,
          ],
      ]);
      

      这应该会在您的测试过程中有所帮助,直到您能够在服务器或本地虚拟主机上进行所有操作。

      【讨论】:

        【解决方案4】:
        try this.
        namespace App\Http\Controllers\Api;
        
        use Illuminate\Http\Request;
        use App\Http\Controllers\Controller;
        use Illuminate\Support\Facades\Route;
        use App\User;
        
        class UserController extends Controller
        {
        
            //use AuthenticatesUsers;
            protected function login(Request $request)
            {
        
                 $request->request->add([
                        'grant_type'    => 'password',
                        'client_id'     => '3',
                        'client_secret' => '6BHCRpB4tpXnQvC1DmpT7CXCSz7ukdw7IeZofiKn',
                        'scope' => '*'
                    ]);
        
                // forward the request to the oauth token request endpoint
                $tokenRequest = Request::create('/oauth/token','post');
                return Route::dispatch($tokenRequest);
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-16
          • 1970-01-01
          • 1970-01-01
          • 2014-08-22
          • 1970-01-01
          • 1970-01-01
          • 2018-08-29
          相关资源
          最近更新 更多