PHP SOAP WSDL 模式在方法调用时返回 Null [英] PHP SOAP WSDL Mode returning Null on method Calls

查看:29
本文介绍了PHP SOAP WSDL 模式在方法调用时返回 Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在 PHP 中学习 SOAP,非 wsdl 模式运行良好.我已将它们包含在内,以供寻求学习的人参考.

I have been trying to learn SOAP in PHP and non-wsdl mode works nicely. I've Included them for information purposes for people seeking to learn.

公开 library.php 的类

Class to expose library.php

<?php
  class Library {
    public function getDwarves(){
      $dwarves = array("Bashful","Doc","Dopey");
        return $dwarves;
      }
      public function greetUser($name){
         return array("message"=>"hello,".$name);
      }
    }
 ?>

非 wsdl 客户端

<?php
try{
  $options = array("location" => "http://192.168.1.20/ws/server.php" , "uri" => "http://192.168.1.20/ws");
 $client=new SoapClient(null,$options);
  $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/>"; 

}
?>

非 wsdl 服务器

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

然而,当 WSDL 被引入图片时,服务器将返回 NULL 作为方法调用的结果.谁能帮我确定我做错了什么?

However when WSDL is introduced to the picture the server is returning NULL as result for method calls. Can anyone help me identifying what I am doing wrong?

server.php

<?php
   require('library.php');
   $server = new SoapServer("wsdl");
   $server->setClass('Library');
   $server->handle();
?>

client.php

<?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/>"; 

}
?>

带有调试信息的client.php

client.php with debugging information

    <?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/>"; 
}
?>

wsdl

<?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 

推荐答案

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

PHP SOAP can be very frustrating to work with as most of the errors you may likely encounter are ambiguous and hence, difficult to debug.

在 WSDL 模式下使用 PHP SOAP 时必须注意一些事项.PHP 函数的输入和输出参数必须在 WSDL 文件(在types"标签或message"标签中)和 PHP 代码(作为类定义)中明确定义.

There are a couple of things that must be noted when working with PHP SOAP in WSDL mode. The input and output parameters of your PHP functions must be clearly defined both in the WSDL file (either in the "types" tag or in the "message" tag) and in your PHP code (as class definition).

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

These two requirements are missing from your implementation. Your debug output information ($client->__getFunctions()), also pointed out the errors as you will notice that the return "type" for method getDwarves() and greetUser() are both "void". This means that PHP SOAP will return null (void) when you call any of these methods even if your PHP function definition returns a value.

有关更多信息,请查看关于 以 WSDL 模式创建 PHP SOAP 服务器

For more info, check out this simple tutorial on Creating PHP SOAP server in WSDL mode

这篇关于PHP SOAP WSDL 模式在方法调用时返回 Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆