我正在尝试为 WCF Web 服务创建一个 PHP 客户端。但是当我调用服务的功能时出现一些错误。
应用程序配置
<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceBehavior"
name="GSC.Wcf.Services.CartService">
<endpoint address=""
binding="basicHttpBinding"
contract="GSC.Wcf.Services.ICartService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/CartService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
功能:
> public int Addiere(int a, int b)
{
return a + b;
}
PHP 请求:
> $client = new SoapClient("http://localhost:8731/CartService?wsdl");
>
> $result = $client->Addiere(2,4);
对于这些函数,我得到了这样的错误:
"Uncaught SoapFault exception: [a:DeserializationFailed] the Formatierer formatter has realeased an exception during the deserilization of the message: Failed to deserialize the request body of the message is intended for operation "Addiere". The end-element "Body" aus Namespace "http://schemas.xmlsoap.org/soap/envelope/" was expected. Found was the element "param1" of Namespace"".
德语:
Fatal error: Uncaught SoapFault exception: [a:DeserializationFailed] Der Formatierer hat beim Deserialisieren der Nachricht eine Ausnahme ausgelöst: Fehler beim Deserialisieren des Textkörpers der Anforderungsnachricht für Vorgang "Addiere". Es wurde das Endelement "Body" aus Namespace "http://schemas.xmlsoap.org/soap/envelope/" erwartet. Gefunden wurde "Element "param1" aus Namespace """. Zeile 2, Position 148. in C:\xampp\htdocs\TestClient\Client.php:6 Stack trace: 0 C:\xampp\htdocs\TestClient\Client.php(6): SoapClient->__call('Addiere', Array) #1 C:\xampp\htdocs\TestClient\Client.php(6): SoapClient->Addiere(2, 4) #2 {main} thrown in C:\xampp\htdocs\TestClient\Client.php on line 6
但是这个函数是有效的:
C#
> public string Message()
{
return "WORD";
}
PHP
> $result = $client->Message();
var_dump($result);
结果
object(stdClass)#2 (1) { ["MessageResult"]=> string(4) "WORD" } WORD
唯一的问题是返回类型不是字符串。
谁能猜出哪里出了问题,或者 shell 我发布了更多类似 wsdl 的东西? 或者有没有人知道好的资源,我可以在其中查找如何获得正确的配置来与我的服务进行通信?
最佳答案
问题已解决。
调用超过2个参数的方法时,你需要将它们放在这样的数组中
$result = $client->Addiere(array("a"=> 2, "b"=> 3))->AddiereResult;
另一个问题是返回类型,这是用
解决的....->AddiereResult;
不确定最后一部分是什么意思,但它有效,我会弄清楚的。
关于php - 带有 PHP 5 客户端的 WCF 4.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7878804/