【发布时间】:2016-07-06 18:13:59
【问题描述】:
有几个星期我试图在 php 中创建一个肥皂服务器,它首先提供一个带有身份验证标头的 wsdl,然后它在每个请求中只接受经过身份验证的用户。但我只是让它在没有身份验证的情况下完全工作。我所做的每一个搜索和我找到的每一个解决方案都包含一个 SoapClient、Zend_Soap_Client、nu_soap_client(你可以命名它)和我的类周围的某种包装类,或者只在客户端上添加用户名和密码。 但在我的解决方案中,只有服务器在 php 中,客户端是用 java 等编写的各种程序,不是在 php 中。这是我的wsdl生成和服务器部分的代码(我在这里使用zend,但想法与普通php相同):
use Zend\Soap\AutoDiscover as Zend_Soap_AutoDiscover;
use Zend\Soap\Server as Zend_Soap_Server;
if (isset($_GET['wsdl'])) {
$autodiscover = new Zend\Soap\AutoDiscover();
$autodiscover->setClass('MyClass');
$autodiscover->setUri('http://Myclass/path/');
$autodiscover->handle();
exit;
}
$server = new Zend_Soap_Server(null, array(
'uri' => 'http://Myclass/path/',
));
$server->setClass('Myclass');
$server->handle();
我还使用了piotrooo 的 wsdl 生成器和普通的 php soap 库,如下所示:
use WSDL\WSDLCreator;
// use WSDL\XML\Styles\DocumentLiteralWrapped;
if (isset($_GET['wsdl'])) {
$wsdl = new WSDL\WSDLCreator('Myclass', 'http://Myclass/path/');
$wsdl->setNamespace("http://Myclass/path/");
$wsdl->renderWSDL();
exit;
}
$server = new SoapServer(null, array(
'uri' => 'http://Myclass/path/',
// 'style' => SOAP_DOCUMENT,
// 'use' => SOAP_LITERAL,
));
$server->setClass('Myclass');
$server->handle();
还有我的班级:
class Myclass
{
public function __construct()
{
/*some db stuff with doctrine*/
}
/**
* @param string $id
* @return object
*/
public function Id($id)
{
/*I'm using doctrine to fetch data from db and then return an object/or array*/
}
}
最后这是自动生成的 wsdl:
<definitions name="Myclass" targetNamespace="http://Myclass/path/"><types><xsd:schema targetNamespace="http://Myclass/path/"/></types><portType name="MyclassPort"><operation name="Id"><documentation>Id</documentation><input message="tns:IdIn"/><output message="tns:IdOut"/></operation></portType><binding name="MyclassBinding" type="tns:MyclassPort"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><operation name="Id"><soap:operation soapAction="http://Myclass/path/#Id"/><input><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://Myclass/path/"/></input><output><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://Myclass/path/"/></output></operation></binding><service name="MyclassService"><port name="MyclassPort" binding="tns:MyclassBinding"><soap:address location="http://Myclass/path/"/></port></service><message name="IdIn"><part name="id" type="xsd:string"/></message><message name="IdOut"><part name="return" type="xsd:struct"/></message></definitions>
每个生成器的注释都不同。 我也尝试过 nusoap,但我很失望,因为它是纯类方法发现。我必须补充一点,我正在使用 soapui 测试服务的使用(这就是我不想要 php 的 SoapClient 或等效示例的原因)。 最后我还必须说,我尝试了在我的类中添加身份验证方法的解决方案,并且这有效但是这并没有阻止未经身份验证的用户访问 ws。
一些额外的信息。在我的研究中,我找到的每个答案都是关于 SOAPClient 例如 $client->__addheader() 函数等。请告诉我我是否错了,或者如果这不能用 PHP 完成,因为我必须找人否则用另一种编程语言(如 Java 等)为我做这件事。
提前致谢
迪米特里斯
【问题讨论】:
-
一个 WSDL 应该是公开的。你为什么要阻止它公开?
-
不,我没有暴露,因为它必须暴露!
标签: php web-services authentication zend-framework soap