简体   繁体   中英

PHP SOAP Client and Server

I'm attempting to write my first SOAP server, having done a bit with SOAP clients. 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:

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

Use var_export() instead. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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