Jens Kirk
Jens Kirk

Reputation: 536

Re-use PHP SOAP connection if already created?

How do I in the easiest way re-use the SOAP connection if it has already been created?

The soap connection function could in many cases be run multible times duing one PHP page load so there is not need to re-create the connection if it has been created - it just needs to be re-used.

(And sometimes it is not even run at all because it is not needed so it would be a waste of time calling it in the beginning of all PHP page. The function only needs to be run it there is a need to connect.)

I know that there other solutions for this problem (when I google it) but I did not manage to understand them. I tried a lot of things but they did not work for me. I even tried storing the SOAP object in a session so the next PHP page that was loaded could re-use the soap connection (from the previous PHP load) but it did not work eighter.

The best solution is that the connection is remembered for all PHP pages loads in a browser session and the next best solution is that it remembered for the current PHP page load.

Here is my code:

protected static function Economic_API() {

    static $client; 

    $settingsOld = Settings::GetOld();      

    try {               
        $client = new SoapClient("https://api.e-conomic.com/secure/api1/EconomicWebservice.asmx?WSDL", array("trace" => 1, "exceptions" => 1));
        $client->ConnectWithToken(array(
            'token' => $settingsOld->economic_token_secret,
            'appToken' => $settingsOld->economic_token_app
        ));         
    }
    .
    .
    .

UPDATED CODE:

class EcoAPI {

static $client;

static public function getClient() {

    if (empty(self::$client)) {
        self::initClient();
    }

    return self::$client;
}

static private function initClient() {

    $settingsOld = Settings::GetOld();
    self::$client = new SoapClient("https://api.e-conomic.com/secure/api1/EconomicWebservice.asmx?WSDL", array("trace" => 1, "exceptions" => 1));
    self::$client->ConnectWithToken(array('token' => $settingsOld->economic_token_secret, 'appToken' => $settingsOld->economic_token_app));        

}
}

And call it by:

$result = EcoAPI::getClient()->Account_FindByNumber(array('number' => intval($accountID)));

Upvotes: 1

Views: 906

Answers (1)

Maarten van Middelaar
Maarten van Middelaar

Reputation: 1721

perhaps you could try using a class instead of a function. The connection will be living in your class instance while the public function getClient() is available for the application to use the soapClient connection.

class Economic_API {

     private $client;
     private $token;
     private $appToken;

     public function __construct($token, $appToken){
         $this->token = $token;
         $this->appToken = $appToken;
     }

     private function initClient() {
         $this->client = new SoapClient("https://api.e-conomic.com/secure/api1/EconomicWebservice.asmx?WSDL", array("trace" => 1, "exceptions" => 1));
         $this->client->ConnectWithToken(array(
             'token' => $settingsOld->economic_token_secret,
             'appToken' => $settingsOld->economic_token_app
         ));        
     }

     /**
      * @returns SoapClient
      */
     public function getClient() {
         if($this->client === null) {
             $this->initClient();
         }
         return $this->client;
     }         
}
$token = '123';
$appToken = "abc"
$economicApi = new Economic_API($token, $appToken);

$economicApi->getClient()->YourSoapFunction();

Upvotes: 2

Related Questions