且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用PHP处理SOAP标头

更新时间:2023-02-19 14:04:14

找到了!这就是你的工作.

Found it! Here's what you do.

创建您的SOAP服务器:

Create your SOAP server:

$server = new SoapServer($your_wsdl,$any_options);
$server->setClass($name_of_your_class);
$server->handle($location_of_request_data);

$name_of_your_class中命名的类,除了包含在$your_wsdl中定义的每个服务的函数外,还应包含一个根据您在<SOAP-ENV:Header>标记外壳中具有的名称命名的函数.我有<ns1:Header>,所以我的函数命名为Header.将您需要的任何逻辑放入其中.对我来说,我想验证API密钥和站点ID,所以我创建了一个私有变量,如果API密钥和站点ID是正确的,则将该变量设置为true.该类中的所有其他函数在继续之前检查该变量是否为true,否则,抛出SOAPFault.

The class named in $name_of_your_class, in addition to containing functions for each service defined in $your_wsdl, should also contain a function named for whatever you have in your <SOAP-ENV:Header> tag enclosure. I have <ns1:Header>, so my function is named Header. Put whatever logic you need in there. For me, I wanted to validate the API key and site ID, so I created a private variable, and if the API key and site ID are correct, the variable is set to true. All of the other functions in the class check to see if that variable is true before proceeding, and if not, a SOAPFault is thrown.

希望这可以帮助在Google上遇到此问题的任何人.

Hope this helps anyone who comes across this question on Google.

P.S .:对于$server->setClass()中定义的类中的函数,请不要忘记它们必须按照WSDL中定义的顺序接受参数.那把我绊倒了.

P.S.: For the functions in the class defined in $server->setClass(), don't forget that they must accept arguments in the order defined in the WSDL. That tripped me up.

祝所有其他PHP/SOAP开发人员好运-似乎我们所有人都需要它.

Good luck to all other PHP/SOAP developers - seems like we all need it.

Edit/P.P.S .:无论出于什么原因,简单地调用$server->addFunction("Header")都对我不起作用-在尝试上述setClass方法之前,我先尝试了该操作.只是对人们的提神.

Edit/P.P.S.: For whatever reason, simply calling $server->addFunction("Header") did not work for me - I tried that first before I tried the setClass approach above. Just a heads-up to folks.