Search code examples
phplaravellocalhostlumenguzzle

Guzzle cannot send a web request to the same server?


Currently I'm making software in PHP which uses Guzzle to the same IP that it is hosted on to send a web request. At the moment, I'm making a request that looks like this:

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

The code that should be returned should look like this

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

Currently, the site is ran using:

php -S localhost:8000 -t public

Furthermore, the code that sends this request looks like this:

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

In summary, I'm not really sure how to solve this without a real-world test but I'd like to test it locally and see what I receive.


Solution

  • Consider php -S as a single thread server.

    From the documentation:

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

    So when Guzzle sends a request to the same server it causes what known as a deadlock. The original request and the new request are both waiting for each other.

    So because of that, you shouldn't make the script call itself. you should either use a real server that supports multi-threading, like Apache or Nginx.

    Or the option I prefer: you can ask Laravel to call itself. something like:

    $req = Request::create($transaction->callback, 'GET',
        [
            'address' => $addy,
            'balance' => $address->current_balance,
            'completed' => 0,
        ]
    );
    $res = app()->handle($req); 
    
    // deal with the response here...