0

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);
6
  • What does the Client class do? Commented Sep 20, 2022 at 23:14
  • Are you meaning a separate domain or just a different path on the same domain? Commented Sep 21, 2022 at 4:18
  • @Rylee different path on the same doaim Commented Sep 21, 2022 at 6:20
  • @ADyson it's a php SoapClient, it sends and receives data. Edited my question to reflect that. Commented Sep 21, 2022 at 6:21
  • I see. Surely the WSDL specifies the URL for each endpoint so you don't have to? Or are you saying there are two separate WSDLs you need to read from? Commented Sep 21, 2022 at 8:14

1 Answer 1

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I've upvoted your answer. Before I accept it, let me consider some things. Will there be any downsides if I move the client instantiations into their own methods, like public function getClient() { return new SoapClient('get_url'); } and then make requests like this $this->getClient()->soapCall('...')?
Well that would create a new instance of SoapClient every time you make a request, which seems a bit wasteful. Why do that when you can create it once and re-use it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.