Guzzle 无法向同一台服务器发送 Web 请求?

Guzzle cannot send a web request to the same server?

目前我正在 PHP 制作软件,它使用 Guzzle 到托管它的同一 IP 来发送 Web 请求。目前,我提出的请求如下所示:

http://localhost:8000/temp/utils/transactions/callback?address=long_btc_address&balance=0&completed=0`

应返回的代码应如下所示

$router->get('/temp/utils/transactions/callback', function (Illuminate\Http\Request $request)
{
   \Illuminate\Support\Facades\Log::info($request, 'Callback');
   return 'yo';
});

目前,网站 运行 使用:

php -S localhost:8000 -t public

此外,发送此请求的代码如下所示:

$client = new Client();
$addy = decrypt($address->address);
$callback = $transaction->callback . "?address={$addy}&balance={$address->current_balance}&completed=0";
$res = $client->request('GET', $callback);
if ($res->getStatusCode() === 200) {
    return json_decode($res->getBody()->getContents());
}

总而言之,如果没有真实世界的测试,我不太确定如何解决这个问题,但我想在本地进行测试,看看我收到了什么。

php -S 视为单线程服务器。

来自documentation

The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.

因此,当 Guzzle 向同一服务器发送请求时,它会导致所谓的 deadlock。原请求和新请求都在等待。

因此,您不应让脚本自行调用。您应该使用支持多线程的真实服务器,例如 Apache 或 Nginx。

或者我更喜欢的选项:你可以让 Laravel 调用它自己。类似于:

$req = Request::create($transaction->callback, 'GET',
    [
        'address' => $addy,
        'balance' => $address->current_balance,
        'completed' => 0,
    ]
);
$res = app()->handle($req); 

// deal with the response here...