You are here

public function SoapClient::trySoap in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 modules/salesforce_soap/src/Soap/SoapClient.php \Drupal\salesforce_soap\Soap\SoapClient::trySoap()
  2. 5.0.x modules/salesforce_soap/src/Soap/SoapClient.php \Drupal\salesforce_soap\Soap\SoapClient::trySoap()

Salesforce SOAP API resource wrapper.

Ensures the connection is established with the SOAP API prior to making the call and automatically attempts a re-auth when the API responds with invalid session ID / access token.

Parameters

string $function: The name of the SOAP API function to attempt.

array $params: (Optional) An array of parameters to pass through to the function.

bool $refresh: (Optional) Refresh the access token prior to making the call. Defaults to FALSE, in which case a refresh is only attempted if the API responds invalid session ID / access token.

Return value

mixed The return value from $function.

Throws

\SoapFault

\Exception

Overrides SoapClientInterface::trySoap

See also

\SforcePartnerClient

File

modules/salesforce_soap/src/Soap/SoapClient.php, line 87

Class

SoapClient
A client for communicating with the Salesforce SOAP API.

Namespace

Drupal\salesforce_soap\Soap

Code

public function trySoap($function, array $params = [], $refresh = FALSE) {
  if ($refresh) {
    $this->authMan
      ->refreshToken();
  }
  if (!$this->isConnected) {
    $this
      ->connect();
  }
  try {
    $results = call_user_func_array([
      $this,
      $function,
    ], $params);
    return $results;
  } catch (\SoapFault $e) {

    // sf:INVALID_SESSION_ID is thrown on expired login (and other reasons).
    // Our only recourse is to try refreshing our auth token. If we get any
    // other exception, bubble it up.
    if ($e->faultcode != 'sf:INVALID_SESSION_ID') {
      throw $e;
    }

    // If we didn't already try it, refresh the access token and try the call
    // again.
    if (!$refresh) {
      return $this
        ->trySoap($function, $params, TRUE);
    }
    else {

      // Our connection is not working.
      $this->isConnected = FALSE;
      throw $e;
    }
  }
}