简体   繁体   English

PHP SOAP客户端和服务器

[英]PHP SOAP Client and Server

I'm attempting to write my first SOAP server, having done a bit with SOAP clients. 我尝试编写我的第一个SOAP服务器,对SOAP客户端做了一些工作。 When I tried with a sending a single string, this worked fine. 当我尝试发送单个字符串时,此方法工作正常。 But when trying to send multiple parameters to the server I'm coming unstuck. 但是,当尝试将多个参数发送到服务器时,我无法解决。 Is there a better method of tackling this? 有解决这个问题的更好方法吗?

Server: 服务器:

<?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();
?>

Client: 客户:

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

I see your problem you return a var_dump() but var_dump has no return value. 我看到您的问题,您返回了var_dump()var_dump没有返回值。

Use var_export() instead. 请改用var_export() Take a look at the manual . 看一下手册

So in your code this will look like: 因此,在您的代码中,它将类似于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM