需要帮助调试PHP 5 SOAP hello world应用程序

时间:2010-05-05 10:04:58

标签: php web-services soap wsdl

在阅读了网络上的每个教程之后,我一直在努力让PHP 5 SOAP扩展工作,但无济于事。这非常令人沮丧,如果有人能指出我出错的地方以及原因,我会非常感激。

感谢您的帮助,我需要提供更多详细信息。

WSDL如下:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="test"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="http://tempuri.org/"
xmlns:s1="http://microsoft.com/wsdl/types/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
targetNamespace="http://tempuri.org/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:import namespace="http://microsoft.com/wsdl/types/" />

<s:element name="getUser">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="username" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="password" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getUserResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getUserResult" type="tns:bookUser" />
</s:sequence>
</s:complexType>

</s:element>

<s:complexType name="bookUser">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="ID" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="GUID" type="s1:guid" />
<s:element minOccurs="0" maxOccurs="1" name="login" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="pass" type="s:string" />
</s:sequence>
</s:complexType>

</s:schema>
</wsdl:types>

<wsdl:message name="getUserSoapIn">
<wsdl:part name="parameters" element="tns:getUser" />
</wsdl:message>
<wsdl:message name="getUserSoapOut">
<wsdl:part name="parameters" element="tns:getUserResponse" />
</wsdl:message>


<wsdl:portType name="test">
<wsdl:operation name="getUser">
<wsdl:input message="tns:getUserSoapIn" />
<wsdl:output message="tns:getUserSoapOut" />
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="testBinding" type="tns:test">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getUser">
<soap:operation soapAction="http://tempuri.org/getUser" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="testService">
<wsdl:port name="testPort" binding="tns:testBinding">
<soap:address location="http://127.0.0.1/index.php" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

服务器的代码:

<?php

function getUser($param) {

return array(
'bookUser'=>array
(
'ID'=>1,
'GUID'=>2,
'login'=>$param->username,
'pass'=>$param->password
)
);
}

ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer("http://127.0.0.1/1.wsdl");
$server->addFunction("getUser");

$server->handle();

?>

和客户端的代码:

$client = new SoapClient("http://127.0.0.1/index.php?wsdl", array('exceptions' => 0));

try {
$arr_data = array
(
array
(
'username'=>'xyz',
'password'=>'abc'
)
);
print_r($client->__soapCall("getUser",$arr_data));
}
catch (SoapFault $result) {
print_r($result);
} 

1 个答案:

答案 0 :(得分:1)

尝试更改wsdl的以下行:

<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">

到您的开发服务器的实际主机名/ IP地址。例如。如果您的开发服务器位于本地网络192.168.0.100:

<s:schema elementFormDefault="qualified" targetNamespace="http://192.168.0.100/">

我在开发服务器上使用了上面的代码,直到我修复了wsdl的那一行,才导致错误。