2012-09-04 47 views
2

我想用php和zend框架創建一個web服務。 服務器端代碼如下:使用php獲得空web服務構建響應

csiService.php:

<?php 
require_once 'Zend/Loader.php'; 
require_once 'CSI.php'; 
$WSDL_URI="http://csi.chemicalseeker.com/csiService.php?WSDL"; 
if(isset($_GET["WSDL"])) 
{ 
    Zend_Loader::loadClass('Zend_Soap_AutoDiscover'); 
    Zend_Loader::loadClass('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence'); 
    $autodiscover = new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence'); 
    $autodiscover->setBindingStyle(array('style' => 'document')); 
    $autodiscover->setOperationBodyStyle(array('use' => 'literal')); 
    $autodiscover->setClass('CSI'); 
    $autodiscover->handle(); 
} 
else 
{ 
    Zend_Loader::loadClass('Zend_Soap_Server'); 
    $server = new Zend_Soap_Server($WSDL_URI); 
    $server->setClass('CSI'); 
    $server->handle(); 
} 
?> 

其中包括CSI.php

<?php 
class CSI { 
    /** 
    * @return string 
    */ 
    function helloWorld() 
    { 
     return("Hello"); 
    } 
} 
?> 

* 我編輯了主機文件,爲了將域「csi.chemicalseeker.com」綁定到127.0.0.1 WSDL正常工作我遊「http://csi.chemicalseeker.com/csiService.php?WSDL」在我的瀏覽器:

<?xml version="1.0"?> 
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:tns="http://csi.chemicalseeker.com/csiService.php" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="CSI" 
    targetNamespace="http://csi.chemicalseeker.com/csiService.php"> 
    <types> 
     <xsd:schema targetNamespace="http://csi.chemicalseeker.com/csiService.php"> 
      <xsd:element name="helloWorld"> 
       <xsd:complexType /> 
      </xsd:element> 
      <xsd:element name="helloWorldResponse"> 
       <xsd:complexType> 
        <xsd:sequence> 
         <xsd:element name="helloWorldResult" type="xsd:string" /> 
        </xsd:sequence> 
       </xsd:complexType> 
      </xsd:element> 
     </xsd:schema> 
    </types> 
    <portType name="CSIPort"> 
     <operation name="helloWorld"> 
      <documentation>@return string</documentation> 
      <input message="tns:helloWorldIn" /> 
      <output message="tns:helloWorldOut" /> 
     </operation> 
    </portType> 
    <binding name="CSIBinding" type="tns:CSIPort"> 
     <soap:binding style="document" 
      transport="http://schemas.xmlsoap.org/soap/http" /> 
     <operation name="helloWorld"> 
      <soap:operation 
       soapAction="http://csi.chemicalseeker.com/csiService.php#helloWorld" /> 
      <input> 
       <soap:body use="literal" /> 
      </input> 
      <output> 
       <soap:body use="literal" /> 
      </output> 
     </operation> 
    </binding> 
    <service name="CSIService"> 
     <port name="CSIPort" binding="tns:CSIBinding"> 
      <soap:address location="http://csi.chemicalseeker.com/csiService.php" /> 
     </port> 
    </service> 
    <message name="helloWorldIn"> 
     <part name="parameters" element="tns:helloWorld" /> 
    </message> 
    <message name="helloWorldOut"> 
     <part name="parameters" element="tns:helloWorldResponse" /> 
    </message> 
</definitions> 

我也寫了叫CSIClient.php一個PHP客戶端文件,並從瀏覽器訪問它:

CSIClient.php

<?php 
require_once 'Zend/Loader.php'; 
require_once 'CSI.php'; 
Zend_Loader::loadClass('Zend_Soap_Client'); 
$WSDL_URI="http://csi.chemicalseeker.com/csiService.php?WSDL"; 

    $client = new Zend_Soap_Client($WSDL_URI); 
    echo('<pre>'); 
    var_dump($client->helloWorld()); 
    echo('</pre>'); 
?> 

結果預計將與 「你好」 的內容的字符串,但它顯示一個空stdObject:

object(stdClass)#3 (0) { 
} 

我可以通過「$ client-> getFunctions()」和「$ client-> getTypes()」獲取函數列表和類型列表,這意味着「CSI」類已成功連接到Web服務。但結果無法正確返回。

我也試過其他方法來調用Web服務。我使用Flash Builder中調用了HelloWorld()函數,從服務器的響應情況如下:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://csi.chemicalseeker.com/csiService.php"> 
    <SOAP-ENV:Body> 
    <ns1:helloWorldResponse/> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

正如我們可以看到預期的結果「你好」,不包含在SOAP信封要麼。 我錯過了一些重要的東西,或者在我的代碼中犯了些錯誤嗎?如果你有線索,請幫助我。謝謝!

回答

2

我這個苦苦掙扎很好,但通過切換到非WSDL模式,我設法解決了上述問題。

不管我嘗試過什麼,我總是收到與嘗試從我自己的自動發現生成的WSDL加載WSDL時相同的空響應。 (感覺可能會有一些遞歸問題,但我現在看不到)

無論如何,切換到非WSDL模式給了我適當的響應。

嘗試創建您的服務器如下:

$server = new Zend_Soap_Server(null, array('uri' => $WSDL_URI)); 
+0

對不起,這個答案是如此fortunated。看起來Soap Server無法處理函數的返回。 – xsubira