Home > Net >  Implementing api client with multiple base urls
Implementing api client with multiple base urls

Time:09-21

I need to build a client for soap api that has a separate base url for get and post http methods.

There are two separate WSDLs: one lists actions to read data and another one to insert data, for example: https://www.example.com/webservices3/ExampleWS.asmx and https://www.example.com/webservices3/ExampleWSPost.asmx.

How do I implement the client with multiple base urls?

Normally, with a single base url, I'd do something like this:

use SoapClient;

class ExampleApiClient
{
    protected $client;

    public function __construct()
    {
        $this->client = new SoapClient('example.com/path?wsdl');
    }

    public function getUser($args)
    {
        return $this->client->soapCall('exampleAction', $args);
    }

    public function addUser($args)
    {
        return $this->client->soapCall('exampleAction', $args);
    }
}

// which then will be called somewhere in the code like this:

$api = new ExampleApiClient();
$api->getUser(12);

CodePudding user response:

There may be other ways to do this, but one way is to simply maintain two SoapClient instances in your class, and use each one as appropriate within the individual functions.

For example:

use SoapClient;

class ExampleApiClient
{
    protected $getClient;
    protected $postClient;

    public function __construct()
    {
        $this->getClient = new SoapClient('example.com/path?wsdl');
        $this->postClient = new SoapClient('example.com/path2?wsdl');
    }

    public function getUser($args)
    {
        return $this->getClient->soapCall('exampleAction', $args);
    }

    public function addUser($args)
    {
        return $this->postClient->soapCall('exampleAction', $args);
    }
}
  • Related