dsx5201 2018-05-16 06:04
浏览 189

如何在PHP中的SOAP API中调用和设置头和函数中的参数?

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条回答 默认 最新

  • dotif64826 2018-05-16 07:42
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题