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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)