Php – How to create php SOAP Server functions using WSDL

PHPsoapweb serviceswsdl

I want to create PHP SOAP Server, but don't understand how to do it correctly. So below is the server.php file:

<?php

class NewOperation {
    public function NewOperation()
    {

    }
}

ini_set("soap.wsdl_cache_enabled", "0");
$server = new SOAPServer('http://localhost:9080/soap-websiteservice-        wsdl/CalculatorService.wsdl', array(
    'soap_version' => SOAP_1_2,
    'style' => SOAP_RPC,
    'use' => SOAP_LITERAL
));
$server->setClass('NewOperation');
$server->handle();

The client.php file:

<?php
// client.php
$options = array(
    'trace' => true
);
$client = new SOAPClient('http://localhost:9080/soap-websiteservice-    wsdl/server.php?wsdl', $options);
var_dump($client->NewOperation());

The wsdl file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"     xmlns:tns="http://localhost:9080/soap-websiteservice-wsdl/CalculatorService/"     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"     xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CalculatorService"     targetNamespace="http://localhost:9080/soap-websiteservice-    wsdl/CalculatorService/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://localhost:9080/soap-websiteservice-    wsdl/CalculatorService/">
      <xsd:element name="add" type="xsd:string">

      </xsd:element>
  <xsd:element name="addResponse">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="addResponse" type="tns:addResponseType" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="addRequestType">
    <xsd:sequence>
        <xsd:element name="num1" type="xsd:int"></xsd:element>
        <xsd:element name="num2" type="xsd:int"></xsd:element>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="addResponseType">
    <xsd:sequence>
        <xsd:element name="result" type="xsd:string"></xsd:element>
    </xsd:sequence>
  </xsd:complexType>
        <xsd:element name="in" type="xsd:string"></xsd:element>
        <xsd:element name="NewOperationResponse">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="out" type="xsd:string"></xsd:element>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
</xsd:schema>
  </wsdl:types>
  <wsdl:message name="addRequest">
<wsdl:part element="tns:add" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="addResponse">
<wsdl:part element="tns:addResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="NewOperationRequest">
<wsdl:part name="NewOperationRequest" element="tns:in"></wsdl:part>
  </wsdl:message>
  <wsdl:message name="NewOperationResponse">
    <wsdl:part name="parameters" element="tns:NewOperationResponse">        </wsdl:part>
  </wsdl:message>
<wsdl:portType name="CalculatorService">

<wsdl:operation name="NewOperation">
    <wsdl:input message="tns:NewOperationRequest"></wsdl:input>
    <wsdl:output message="tns:NewOperationResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CalculatorServiceSOAP"
type="tns:CalculatorService">
<soap:binding style="document"
    transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="NewOperation">
    <soap:operation
        soapAction="http://localhost:9080/soap-websiteservice-        wsdl/CalculatorService/NewOperation" />
    <wsdl:input>
        <soap:body use="literal" />
    </wsdl:input>
    <wsdl:output>
        <soap:body use="literal" />
    </wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CalculatorService">
<wsdl:port binding="tns:CalculatorServiceSOAP" name="CalculatorServiceSOAP">
  <soap:address location="http://localhost:9080/soap-websiteservice-wsdl/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

Image from eclipse

Zipped all files

I created WSDL file with Eclipse web service XML editor. Now I do not know how to create functions. I get the following error:

Fatal error: Uncaught SoapFault exception: [HTTP] Not Found in C:\wamp\www\soap-websiteservice-wsdl\client.php:7 Stack trace: #0 [internal function]: SoapClient->__doRequest('http://localhos…', 'http://localhos…', 1, 0) #1 C:\wamp\www\soap-websiteservice-wsdl\client.php(7): SoapClient->__call('NewOperation', Array) #2 C:\wamp\www\soap-websiteservice-wsdl\client.php(7): SoapClient->NewOperation() #3 {main} thrown in C:\wamp\www\soap-websiteservice-wsdl\client.php on line 7

Best Answer

Try it

Server object:

class Server{

 protected $class_name = '';

 public function __construct($class_name)
    {
        $this->class_name = $class_name;
    }
 public function AuthHeader($Header)
    {
        //if($Header->username == 'foo' && $Header->password == 'bar')
         //   $this->authenticated = true;

    }

 public function log($method_name,$data)
    {
        $filename = 'log.txt';
        $handle = fopen($filename, 'a+');
        fwrite($handle, date("l dS of F Y h:i:s A").' - '.$_SERVER['REMOTE_ADDR']."\r\n".$method_name."\r\n".print_r($data,true));
        fclose($handle);
    }   

 public function __call($method_name, $arguments)
    {
        $this->log($method_name,$arguments); //  log
        if($arguments[0]!=AUTH) return 'Authorization required'; // auth check
        $_method_name = '_'.$method_name; //  method name replace
        if(!method_exists($this->class_name, $_method_name )) return 'Method '.$method_name.' not found'; // methot exist check
        return call_user_func_array(array($this->class_name, $_method_name ), $arguments); //call method
    }
}

It is my a working code with logging request. I had same problem with it.

Server:

$Service = new Server('YouClassHere');

$server->setObject($Service);
Related Topic