PHP SOAP客户端和服务器

时间:2012-12-10 16:51:45

标签: php soap wsdl

我正在尝试编写我的第一个SOAP服务器,用SOAP客户端做了一些工作。 当我尝试发送一个字符串时,这很好。但是当试图向服务器发送多个参数时,我正在崩溃。有没有更好的方法来解决这个问题?

服务器:

<?php    
if(!extension_loaded("soap")){
    dl("php_soap.dll");
}
ini_set("soap.wsdl_cache_enabled","0");

function getCatalogEntry($array){
$conn = mysqli_connect($host,$user,$password,$db) or die(mysqli_error($conn));
$sql = "SELECT '" . $array[0]. "' FROM soap WHERE id = '".$array[1]."'";


$result = $conn->query($sql) or die(mysqli_error($conn));
$row = mysqli_fetch_array($result);
    return var_dump($array).$array[0];//$sql.$row[$field];
}

$server = new SoapServer("test.wsdl");
$server->AddFunction("getCatalogEntry");
$server->handle();
?>

客户端:

<?php
try{
    $sClient = new SoapClient('server.php?wsdl');

    $params = array( 
        "field" => "field2",
        "id" => "id"); 

    $response = $sClient->getCatalogEntry($params);

    var_dump($response);


} catch(SoapFault $e){
    var_dump($e);
}
?>

WSDL:

<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='Catalog'
  targetNamespace='http://example.org/catalog'
  xmlns:tns=' http://example.org/catalog '
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
  xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
  xmlns='http://schemas.xmlsoap.org/wsdl/'>

  <xsd:complexType name="KeyValueData">
<xsd:sequence>
  <xsd:element minOccurs="1" maxOccurs="1" name="id" type="string"/>
  <xsd:element minOccurs="1" maxOccurs="1" name="field" type="string"/>
</xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="ArrayOfKeyValueData">
<xsd:sequence>
  <xsd:element minOccurs="0" maxOccurs="unbounded"
           name="keyval" type="tns:KeyValueData"/>
</xsd:sequence>
  </xsd:complexType>

  <message name='getCatalogRequest'>
    <part name='catalogId' type='ArrayOfKeyValueData'/>
  </message>
  <message name='getCatalogResponse'>
    <part name='Result' type='xsd:string'/>
  </message>

  <portType name='CatalogPortType'>
    <operation name='getCatalogEntry'>
      <input message='tns:getCatalogRequest'/>
          <output message='tns:getCatalogResponse'/>
        </operation>
      </portType>

      <binding name='CatalogBinding' type='tns:CatalogPortType'>
    <soap:binding style='rpc'
      transport='http://schemas.xmlsoap.org/soap/http'
  />
    <operation name='getCatalogEntry'>
      <soap:operation soapAction='urn:localhost-catalog#
    getCatalogEntry'/>
      <input>
        <soap:body use='encoded' namespace=
      'urn:localhost-catalog'
          encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
      </input>
      <output>
        <soap:body use='encoded' namespace=
   'urn:localhost-catalog'
          encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' />
      </output>
    </operation>
  </binding>

  <service name='CatalogService'>
    <port name='CatalogPort' binding=
  'CatalogBinding'>
      <soap:address location='server.php'/>
    </port>
  </service>
</definitions>

1 个答案:

答案 0 :(得分:1)

我发现您的问题是var_dump(),但var_dump没有返回值。

请改用var_export()。看看the manual

所以在你的代码中,这将是:

$result = $conn->query($sql) or die(mysqli_error($conn));
$row = mysqli_fetch_array($result);
return var_export($array,true);