PHP SOAP WSDL模式在方法调用时返回Null

PHP SOAP WSDL模式在方法调用时返回Null,php,web-services,soap,wsdl,Php,Web Services,Soap,Wsdl,我一直在努力学习PHP中的SOAP,而非wsdl模式工作得很好。我把它们包括进来是为了给那些想要学习的人提供信息 类公开library.php <?php class Library { public function getDwarves(){ $dwarves = array("Bashful","Doc","Dopey"); return $dwarves; } public function greetUser($na

我一直在努力学习PHP中的SOAP,而非wsdl模式工作得很好。我把它们包括进来是为了给那些想要学习的人提供信息

类公开library.php

<?php
  class Library {
    public function getDwarves(){
      $dwarves = array("Bashful","Doc","Dopey");
        return $dwarves;
      }
      public function greetUser($name){
         return array("message"=>"hello,".$name);
      }
    }
 ?>
调试信息客户端的结果

GetFunctions:
array(2) { [0]=> string(17) "void getDwarves()" [1]=> string(29) "void greetUser(anyType $name)" }

GetTypes:
array(0) { }

Request Header:
POST /ws/server.php HTTP/1.1
Host: 192.168.1.20
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.3.3-7+squeeze14
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:LibraryAction"
Content-Length: 385


REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:LibraryWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getDwarves/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response Header:
HTTP/1.1 200 OK
Date: Sat, 01 Mar 2014 00:45:39 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze14
Content-Length: 393
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/xml; charset=utf-8

Response:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:LibraryWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getDwarvesResponse/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Result of getDwarves:
NULL

REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:LibraryWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:greetUser>
<name xsi:type="xsd:string">Fairmutex</name>
</ns1:greetUser>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response Header:
HTTP/1.1 200 OK
Date: Sat, 01 Mar 2014 00:45:39 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze14
Content-Length: 392
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: text/xml; charset=utf-8

Response:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:LibraryWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:greetUserResponse/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Result of greetUser:
NULL 
GetFunctions:
数组(2){[0]=>string(17)“void getDwarves()”[1]=>string(29)“void greetUser(anyType$name)”}
获取类型:
数组(0){}
请求标头:
POST/ws/server.php HTTP/1.1
主机:192.168.1.20
连接:保持活力
用户代理:PHP-SOAP/5.3.3-7+squeeze14
内容类型:text/xml;字符集=utf-8
SOAPAction:“urn:LibraryAction”
内容长度:385
请求:
响应标题:
HTTP/1.1200ok
日期:2014年3月1日星期六00:45:39 GMT
服务器:Apache/2.2.16(Debian)
X-Powered-By:PHP/5.3.3-7+squeeze14
内容长度:393
改变:接受编码
保持活动状态:超时=15,最大=100
连接:保持活力
内容类型:text/xml;字符集=utf-8
答复:
获取矮人的结果:
无效的
请求:
公平互斥
响应标题:
HTTP/1.1200ok
日期:2014年3月1日星期六00:45:39 GMT
服务器:Apache/2.2.16(Debian)
X-Powered-By:PHP/5.3.3-7+squeeze14
内容长度:392
改变:接受编码
保持活动状态:超时=15,最大=99
连接:保持活力
内容类型:text/xml;字符集=utf-8
答复:
greetUser的结果:
无效的

处理PHP SOAP可能会非常令人沮丧,因为您可能遇到的大多数错误都是不明确的,因此很难调试

在WSDL模式下使用PHP SOAP时,必须注意以下几点。PHP函数的输入和输出参数必须在WSDL文件(在“类型”标记或“消息”标记中)和PHP代码(作为类定义)中明确定义

您的实现中缺少这两个需求。调试输出信息($client->_getFunctions())也指出了错误,因为您会注意到方法getDwarves()和greetUser()的返回“type”都是“void”。这意味着,当您调用这些方法中的任何一个时,即使PHP函数定义返回值,PHP SOAP也将返回null(void)

有关更多信息,请查看上的此简单教程

<?php
   require('library.php');
   $options = array("uri" => "http://192.168.1.20");
   $server = new SoapServer(null,$options);
   $server->setClass('Library');
   $server->handle();
?>
<?php
   require('library.php');
   $server = new SoapServer("wsdl");
   $server->setClass('Library');
   $server->handle();
?>
<?php
try{

  $client=new SoapClient("http://192.168.1.20/ws/wsdl",array( "trace" => 1 ) );
  $dwarves = $client->getDwarves();
  echo nl2br("Result of getDwarves:\n"); 
  var_dump($dwarves);
  echo nl2br("\n\n");

  $greeting = $client->greetUser("Fairmutex");
  echo nl2br("Result of greetUser:\n"); 
  var_dump($greeting);
  echo nl2br("\n\n");

}catch(SoapFault $e){

 var_dump($e);
 echo  "<br/>".$e->getMessage()."<br/>"; 

}
?>
    <?php
try{
$client=new SoapClient("http://192.168.1.20/ws/wsdl",array( "trace" => 1 ) );
$dwarves = $client->getDwarves();

 echo nl2br("GetFunctions:\n");  var_dump($client->__getFunctions()); echo nl2br("\n\n");
 echo nl2br("GetTypes:\n");  var_dump($client->__getTypes()); echo nl2br("\n\n");
 echo nl2br("Request Header:\n" . htmlentities(str_ireplace('><', ">\n<",    $client->__getLastRequestHeaders())) . "\n");
 echo nl2br("REQUEST:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastRequest())) . "\n");
  echo nl2br("Response Header:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastResponseHeaders())) . "\n");
echo nl2br("Response:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastResponse())) . "\n");

echo nl2br("Result of getDwarves:\n"); 
 var_dump($dwarves);
 echo nl2br("\n\n");

 $greeting = $client->greetUser("Fairmutex");
 echo nl2br("REQUEST:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastRequest())) . "\n");
   echo nl2br("Response Header:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastResponseHeaders())) . "\n");
echo nl2br("Response:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastResponse())) . "\n");
echo nl2br("Result of greetUser:\n"); 
 var_dump($greeting);
 echo nl2br("\n\n");
}catch(SoapFault $e){
 var_dump($e);
 echo  "<br/>".$e->getMessage()."<br/>"; 
}
?>
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:typens="urn:LibraryWSDL" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="LibraryWSDL" targetNamespace="urn:LibraryWSDL">
   <message name="getDwarves" />
   <message name="getDwarvesResponse" />
   <message name="greetUser">
      <part name="name" type="xsd:anyType" />
   </message>
   <message name="greetUserResponse" />
   <portType name="LibraryPortType">
      <operation name="getDwarves">
         <input message="typens:getDwarves" />
         <output message="typens:getDwarvesResponse" />
      </operation>
      <operation name="greetUser">
         <input message="typens:greetUser" />
         <output message="typens:greetUserResponse" />
      </operation>
   </portType>
   <binding name="LibraryBinding" type="typens:LibraryPortType">
      <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
      <operation name="getDwarves">
         <soap:operation soapAction="urn:LibraryAction" />
         <input>
            <soap:body namespace="urn:LibraryWSDL" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
         </input>
         <output>
            <soap:body namespace="urn:LibraryWSDL" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
         </output>
      </operation>
      <operation name="greetUser">
         <soap:operation soapAction="urn:LibraryAction" />
         <input>
            <soap:body namespace="urn:LibraryWSDL" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
         </input>
         <output>
            <soap:body namespace="urn:LibraryWSDL" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
         </output>
      </operation>
   </binding>
   <service name="LibraryWSDLService">
      <port name="LibraryPort" binding="typens:LibraryBinding">
         <soap:address location="http://192.168.1.20/ws/server.php" />
      </port>
   </service>
</definitions>
http://192.168.1.20/ws/wsdl
http://192.168.1.20/ws/server.php
http://192.168.1.20/ws/client.php
GetFunctions:
array(2) { [0]=> string(17) "void getDwarves()" [1]=> string(29) "void greetUser(anyType $name)" }

GetTypes:
array(0) { }

Request Header:
POST /ws/server.php HTTP/1.1
Host: 192.168.1.20
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.3.3-7+squeeze14
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:LibraryAction"
Content-Length: 385


REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:LibraryWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getDwarves/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response Header:
HTTP/1.1 200 OK
Date: Sat, 01 Mar 2014 00:45:39 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze14
Content-Length: 393
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/xml; charset=utf-8

Response:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:LibraryWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getDwarvesResponse/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Result of getDwarves:
NULL

REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:LibraryWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:greetUser>
<name xsi:type="xsd:string">Fairmutex</name>
</ns1:greetUser>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response Header:
HTTP/1.1 200 OK
Date: Sat, 01 Mar 2014 00:45:39 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze14
Content-Length: 392
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: text/xml; charset=utf-8

Response:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:LibraryWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:greetUserResponse/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Result of greetUser:
NULL