4

I am using Guzzle to consume a SOAP API. I have to make 6 requests, but in the future this might be even an indeterminate amount of requests.

Problem is that the requests are being send sync, instead of async. Every request on it's own takes +-2.5s. When I send all 6 requests paralell (at least thats what I am trying) it takes +- 15s..

I tried all the examples on Guzzle, the one with a fixed array with $promises, and even the pool (which I need eventually). When I put everything in 1 file (functional) I manage to get the total timing back to 5-6s (which is OK right?). But when I put everything in Objects and functions somehow I do something that makes Guzzle decide to do them sync again.

Checks.php:

    public function request()
    {
        $promises = [];
        $promises['requestOne'] = $this->requestOne();
        $promises['requestTwo'] = $this->requestTwo();
        $promises['requestThree'] = $this->requestThree();
        // etc

        // wait for all requests to complete
        $results = \GuzzleHttp\Promise\settle($promises)->wait();

        // Return results
        return $results;
    }

    public function requestOne()
    {
         $promise = (new API\GetProposition())
            ->requestAsync();
         return $promise;
    }            

    // requestTwo, requestThree, etc

API\GetProposition.php

public function requestAsync()
{
    $webservice = new Webservice();
    $xmlBody = '<some-xml></some-xml>';
    return $webservice->requestAsync($xmlBody, 'GetProposition');
}

Webservice.php

public function requestAsync($xmlBody, $soapAction)
{
    $client = new Client([
        'base_uri' => 'some_url',
        'timeout'  => 5.0
    ]);

    $xml = '<soapenv:Envelope>
       <soapenv:Body>
          '.$xmlBody.'
       </soapenv:Body>
    </soapenv:Envelope>';

    $promise = $client->requestAsync('POST', 'NameOfService', [
        'body'    => $xml,
        'headers' => [
            'Content-Type' => 'text/xml',
            'SOAPAction'   => $soapAction, // SOAP Method to post to
        ],
    ]);

    return $promise;
}

I changed the XML and some of the parameters for abbreviation. The structure is like this, because I eventually have to talk against multiple API's, to thats why I have a webservice class in between that does all the preparation needed for that API. Most API's have multiple methods/actions that you can call, so that why I have something like. API\GetProposition.

Before the ->wait() statement I can see all $promises pending. So it looks like there are being send async. After ->wait() they have all been fulfilled.

It's all working, minus the performance. All 6 requests don't take more then 2.5 to max 3s.

Hope someone can help.

Nick

nicktc
  • 93
  • 1
  • 7

1 Answers1

5

The problem was that the $client object was created with every request. Causing the curl multi curl not to be able to know which handler to use. Found the answer via https://stackoverflow.com/a/46019201/7924519.

nicktc
  • 93
  • 1
  • 7