簡體   English   中英

每當我嘗試使用 guzzle 請求 localhost:8000/any 時,Laravel 服務器就會掛起

[英]Laravel server hangs whenever I try to request localhost:8000/any using guzzle

如果我向http://localhost:8000http://127.0.0.1:8000發出任何請求,它會掛起狀態掛起。 (正如這里https://github.com/guzzle/guzzle/issues/1857

有人告訴我這與暴飲暴食無關,我最好在這里問一下。

我在關注 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 發出請求?

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);
    }

}

卡爾對此有一個很好的解決方案。 如果您正在尋找快速修復來測試您的更新 - 您可以通過打開兩個命令提示符來完成此操作。 第一個將運行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,
    ],
]);

這應該在您的測試期間有所幫助,直到您能夠在服務器或本地虛擬主機上進行所有操作。

我最終通過使用 wamp virtualhost 而不是 php artisan serve 來解決它。 不知道為什么它不適用於本地主機。

更新:有人很好地解釋了為什么它不起作用。

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

這樣做的原因是 php artisan serve 是一個單線程應用程序。 因此,當我們使用 guzzle 向其自身發出請求時,它基本上只是嘗試先完成 guzzle 請求(作為客戶端)然后再完成該請求(作為服務器),這是不可能的。

有關此的更多信息: https : //php.net/manual/en/features.commandline.webserver.php

還有這個答案

當調用自己時,線程被阻塞,等待自己的回復。 解決方案是將提供應用程序和消費應用程序分離到它們自己的實例中,或者在多線程 Web 服務器(例如 Apache 或 nginx)上運行它。

/**
 * 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()));

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM