My system needs to authenticate with an external service (out of my control in any way), and the authentication requires two HTTP requests in very short sequence (the token I get in the first request expires in 2 seconds and I have to use it to finish the auth process).
So I'm basically doing this:
// create client
$client = HttpClient::create();
$baseUrl = "https://example.com/webservice.php";
$username = "foo";
$token = "foobarbaz";
// first request
$tokenResponse = $client->request( Method::GET, $baseUrl . '?operation=getchallenge&username=' . $username );
$decoded = $tokenResponse->toArray();
// second request
$loginResponse = $client->request( Method::POST, $this->baseUrl, [
'body' => [
'operation' => 'login',
'username' => $username,
'accessKey' => md5( $decoded['result']['token'] . $token ),
],
]
);
$sessionData = $loginResponse->toArray();
Doing this, I get an exception ( TransportExceptionInterface):
Failed sending data to the peer for "https://example.com/webservice.php"
But, if instead of using the same $client I instantiate a new one ($client2 = HttpClient::create();) and I use that one to make the second request, without making any other change to the code, it will work flawlessly.
My guess would be that this is related to the default request concurrency that HTTP Client uses, but I checked the docs and the options available on HttpClientInterface but I haven't been anyway to change the client behaviour in a way that doesn't require me to create a new instance (nor a way to increase error logging verbosity).
I have the cURL extension installed, so the client being created is CurlHttpClient and not NativeHttpClient.
Is there a way around this, without having to create a new client instance?