Advertisement
andreasolssoncdab

soapclient.class.php

Nov 15th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php    
  2.     namespace MyPage;
  3.  
  4.     // Disable SOAP WSDL Caching. Sometimes new wsdl structures aren't loaded as they should
  5.     ini_set('soap.wsdl_cache_enabled', '0');
  6.     ini_set('soap.wsdl_cache_ttl', '0');
  7.  
  8.     class WSSEAuth {
  9.         private $Username;
  10.         private $Password;
  11.  
  12.         function __construct($username, $password) {
  13.             $this->Username=$username;
  14.             $this->Password=$password;
  15.         }
  16.     }
  17.  
  18.     class WSSEToken {
  19.         private $UsernameToken;
  20.  
  21.         function __construct ($innerVal){
  22.             $this->UsernameToken = $innerVal;
  23.         }
  24.     }
  25.  
  26.     // Create SOAP client, this could maybe be a class instead of function
  27.     class SoapClient {
  28.         public $client;
  29.  
  30.         public function __construct() {
  31.             $strWSSENS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
  32.             $objSoapVarWSSEHeader;
  33.  
  34.             $objSoapVarUser = new \SoapVar(getenv("NS_USER"), XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);
  35.             $objSoapVarPass = new \SoapVar(getenv("NS_PASSWORD"), XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);    
  36.  
  37.             $objWSSEAuth = new WSSEAuth($objSoapVarUser, $objSoapVarPass);
  38.  
  39.             $objSoapVarWSSEAuth = new \SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, getenv("NS_TOKEN"), $strWSSENS);
  40.  
  41.             $objWSSEToken = new WSSEToken($objSoapVarWSSEAuth);
  42.  
  43.             $objSoapVarWSSEToken = new \SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, getenv("NS_TOKEN"), $strWSSENS);
  44.             $objSoapVarHeaderVal = new \SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);
  45.  
  46.             $objSoapVarWSSEHeader = new \SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal, true);
  47.            
  48.             $this->client = new \SoapClient(getenv("GATEWAY_URL"), array('trace' => 1));
  49.             $this->client->__setSoapHeaders(array($objSoapVarWSSEHeader));    
  50.         }
  51.  
  52.         /**
  53.         * Calls method with request.
  54.         *
  55.         * @param string $method The name of the method
  56.         * @param array $request The data to send to the method
  57.         *
  58.         * @return stdClass The response from Gateway
  59.         */
  60.         public function call($method, $request) {
  61.             try{
  62.                 // Log resopnse in development
  63.                 if(getenv("WP_ENV") == "development") {
  64.                     $t = new \Timer($method);
  65.                     $result = $this->client->$method($request);
  66.  
  67.                     $response = $this->client->__getLastResponse();
  68.                     file_put_contents($_SERVER["DOCUMENT_ROOT"] . "/log/soap.xml", $response);
  69.  
  70.                     unset($t);
  71.                 } else {
  72.                     $result = $this->client->$method($request);
  73.                 }
  74.                
  75.                 return $result;
  76.             } catch (SoapFault $exception) {
  77.                 throw $exception;
  78.             }            
  79.         }
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement