How can i call and set parameter in header and function in SOAP API in PHP?

I Work on Rest API Only and I am new in SOAP API, I am integrating Third Party API Which developed on SOAP API?

How can I call it function "GetAvailibility" and also header and set the parameter?

THIRD PARTY XML CODE:

<?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
       <Authenticate xmlns="http://tempuri.org/">
         <InterfaceCode>10</InterfaceCode>
         <InterfaceAuthKey>jskjks</InterfaceAuthKey>
         <AgentCode>ggsjs2222</AgentCode>
         <Password>abcd</Password>
       </Authenticate>
    </soap:Header>
  <soap:Body>
     <GetAvailibility xmlns="http://tempuri.org/">
        <strRequestXML>string</strRequestXML>
     </GetAvailibility>
   </soap:Body>
  </soap:Envelop

1 answer

  • answered 2018-05-16 07:42 Marcel

    First of all you should know the functions and types of your soap webservice. With this knowledge you can With this knowledge you can leave the SoapClient to do all the work and just have to call the functions with the corresponding types.

    Since you have not announced the functions and types of your web service, I'll just give you a very simple example.

    Functions and Types

    As I said before, you should habe a look at the functions and types of your webservice. For this you can initiate the soap client object and print out all functions and types.

    try {
        $wsdl = 'https://path/to/your/webservice.wsdl';
        $options = [
            'cache_wsdl' =>  WSDL_CACHE_NONE,
            'soap_version' => SOAP_1_2,
            'trace' => true,
        ];
        $client = new \SoapClient($wsdl, $options);
    
        // print out all functions
        echo "<pre>";
        var_dump($client->__getFunctions());
        echo "</pre>";
    
        // print out all types
        echo "<pre>";
        var_dump($client->__getTypes());
        echo "</pre>";
    } catch (\SoapFault $fault) {
        // error handling
    }
    

    Now you can program classes that represent all types of web services.

    Structs as Classes

    Suppose the type for the GetAvailability function looks like this.

    GetAvailability(struct GetAvailabilty)
    
    struct GetAvailability {
        string strRequestXML
    }
    

    The Authenticate struct could look like this.

    struct Authenticate {
        int InterfaceCode
        string InterfaceAuthKey
        string AgentCode
        string Password
    }
    

    Remember that this is a guess. I really don 't know how your types look in reality. This is just an example. You have to find out by yourself, which types and structs are used by your webservice with $client->__getTypes(). With this information you can program the following classes.

    declare(strict_types=1);
    namespace Application\Structs;
    
    class Authenticate
    {
        public $InterfaceCode;
    
        public $InterfaceAuthKey;
    
        public $AgentCode;
    
        public $Password;
    
        public function getInterfaceCode() : int
        {
            return $this->InterfaceCode;
        }
    
        public function setInterfaceCode(int $InterfaceCode) : Authenticate
        {
            $this->InterfaceCode = $InterfaceCode;
            return $this;
        }
    
        public function getInterfaceAuthKey() : string
        {
            return $this->InterfaceAuthKey();
        }
    
        public function setInterfaceAuthKey(string $InterfaceAuthKey) : Authenticate
        {
            $this->InterfaceAuthKey = $InterfaceAuthKey;
            return $this;
        }
    
        public function getAgentCode() : string
        {
            return $this->AgentCode;
        }
    
        public function setAgentCode(string $AgentCode) : Authenticate
        {
            $this->AgentCode = $AgentCode;
            return $this;
        }
    
        public function getPassword() : string
        {
            return $this->Password;
        }
    
        public function setPassword(string $Password) : Authenticate
        {
            $this->Password = $Password;
            return $this;
        }
    }
    

    The same you have to do for your GetAvailibity webservice struct.

    declare(strict_types=1);
    namespace Application\Structs;
    
    class GetAvailability
    {
        public $strRequestXML;
    
        public function getStrRequestXML() : string
        {
            return $this->strRequestXML;
        }
    
        public function setStrRequestXML(string $strRequestXML) : GetAvailability
        {
            $this->strRequestXML = $strRequestXML;
            return $this;
        }
    }
    

    Now you have the webservice structs as php classes. These classes represent the types you listed earlier with the $client->__getTypes() method. The php soap client has another option called classmap. The classmap option can be used to map some WSDL types to PHP classes. This option must be an array with WSDL types as keys and names of PHP classes as values.

    Remember the $options we gave as param to the soap client? Just expand the options around the classmap parameter.

    $options = [
        'cache_wsdl' =>  WSDL_CACHE_NONE,
        'soap_version' => SOAP_1_2,
        'trace' => true,
        'classmap' => [
            'Authenticate' => 'Application\Structs\Authenticate',
            'GetAvailability' => 'Application\Structs\GetAvailability',
        ],
    ];
    

    Just initiate your soap client with this options.

    **Header and Function call*

    Now that we've made all the preparations, we can call the function.

    // set the soap header
    $authenticateStruct = (new \Application\Structs\Authenticate())
        ->setInterfaceCode(10)
        ->setInterfaceAuthKey('jskjks')
        ->setAgentCode('ggsjs2222')
        ->setPassword('abcd');
    
    $header = new \SoapHeader(
        'http://schemas.xmlsoap.org/soap/envelope/',
        'Authenticate',
        $authenticateStruct,
        false
    );
    
    $client->__setSoapHeaders($header);
    
    // function call
    $getAvailabilityStruct = (new \Application\Structs\GetAvailability())
        ->setStrRequestXML($strRequestXML);
    
    $result = $client->GetAvailability($getAvailabilityStruct);
    

    That 's all. Keep in mind, that this is just an example.