PHP SOAP server and .NET Client

On a previous post, it was the other way around a .NET SOAP server and a PHP client and this one is a PHP SOAP server talking to a .NET client. I am using a similar PHP SOAP Server output as before, but having to alter the return type to a complexType instead of a normal PHP SOAP server type.

The basic WSDL generation is very similar to the previous SOAP post, apart from parameter passed the Zend_Soap_AutoDiscover which is “Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex”, which creates the start of the newer return complexTypes.

if(isset($_GET['wsdl'])) {
    $autodiscover = new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex');
    $autodiscover->setClass('QuoteOfTheDay');
    $autodiscover->handle();

here is the new return type for the function to return, it is a class that has public variable that is a string (you define as “” which sets up a string in PHP)

// the return class type
class theQuote {
// have a return value of type string
/** @var string */
  public $getTheQuote="";
}

and then just alter the phpDoc notation for the auto discovery with the Zend SOAP to create a return of theQuote class as above and alter the return variable to the class., below I have included the new WSDL output generated.

  /* phpdoc notation to return a complex type (a class) */
  /**
  * @return theQuote
  */
 function getQuote($quote) {
    $theQuoteR = new theQuote();
    /* just encase the string is in uppercase*/
    $symbol = strtolower($quote);
    /* if there is a quote for the day requested */
    if (isset($this->quotes[$quote])) {
      $theQuoteR->getTheQuote=$this->quotes[$quote];
      return $theQuoteR;
    } else {
      /* else error with default response*/
      $theQuoteR->getTheQuote=$this->quotes["monday"];
      return $theQuoteR;
    }
  }

here is the full source code for the PHP SOAP server

<?php
/* setup the including path for the zend library framework */
ini_set('include_path', '/usr/share/php/libzend-framework-php/');
 
//****************************************************
// Zend Framework 1.8
include_once 'Zend/Loader/Autoloader.php';
require_once "Zend/Soap/Server.php";
require_once "Zend/Soap/AutoDiscover.php";
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);
//****************************************************
 
if(isset($_GET['wsdl'])) {
    $autodiscover = new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex');
    $autodiscover->setClass('QuoteOfTheDay');
    $autodiscover->handle();
} else {
    $soap = new Zend_Soap_Server("http://localhost/projects/webservice/zend_soap_server_net.php?wsdl"); // this current file here
    $soap->setClass('QuoteOfTheDay');
    $soap->handle();
}
 
// the return class type
class theQuote {
// have a return value of type string
/** @var string */
  public $getTheQuote="";
}
 
class QuoteOfTheDay {
 
  /* the quotes to be used from within the function getQuote */
  private $quotes = array("monday" => "Monday's child is fair of face", "tuesday"=>"Tuesday's child is full of grace","wednesday" =>"Wednesday's child is full of woe",
  "thursday" =>"Thursday's child has far to go", "friday" => "Friday's child is loving and giving", "saturday" =>"Saturday's child works hard for a living",
  "sunday" =>"But the child who is born on the Sabbath Day - Is bonny and blithe and good and gay");  
 
  /* phpdoc notation to return a complex type (a class) */
  /**
  * @return theQuote
  */
 function getQuote($quote) {
    $theQuoteR = new theQuote();
    /* just encase the string is in uppercase*/
    $symbol = strtolower($quote);
    /* if there is a quote for the day requested */
    if (isset($this->quotes[$quote])) {
      $theQuoteR->getTheQuote=$this->quotes[$quote];
      return $theQuoteR;
    } else {
      /* else error with default response*/
      $theQuoteR->getTheQuote=$this->quotes["monday"];
      return $theQuoteR;
    }
  }
}
 
?>

Here is the newer WSDL output (snipped) from the new PHP SOAP server that creates a complexType return type for the .NET environment to be able to use.

<types>
-
<xsd:schema targetNamespace="http://localhost/projects/webservice/zend_soap_server_net.php">
-
<xsd:complexType name="theQuote">
-
<xsd:all>
<xsd:element name="getTheQuote" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
</types>
-
<portType name="QuoteOfTheDayPort">
-
<operation name="getQuote">
<documentation>@return theQuote</documentation>
<input message="tns:getQuoteIn"/>
<output message="tns:getQuoteOut"/>
</operation>
</portType>

Here is the .NET client

Since we have a new WSDL generation, will need to generate a newer DLL that will allow the .NET to use the class structure. Please note I am using mono as my .NET environment, so if you are using .NET in Windows then you could either include the WSDL within the Visual Studio or use very similar commands as below.

wsdl2 http://localhost/projects/webservice/zend_soap_server_net.php?wsdl
gmcs QuoteOfTheDayService.cs /t:library /r:System.Web.Services

which will generate the QuoteOfTheDayService.dll and with the code below

using System;
 
namespace codingfriendssoap
{
    class Test_Php_Soap
    {
 
static public void Main()
{
  QuoteOfTheDayService quoteService = new QuoteOfTheDayService();
 
  Console.WriteLine("mondays quote " + quoteService.getQuote("monday").getTheQuote);
}
    }
}

to compile that you once again I am using mono so these are the commands that I am using, but with .NET framework in Windows the commands may be different to compile the program e.g. (mcc)

gmcs web_service_client_php.cs /r:QuoteOfTheDayService.dll

the /r includes the dll generated of the above generated PHP SOAP server this is the output

mondays quote Monday's child is fair of face

here is the XML response from the PHP SOAP server, which includes getTheQuote return wrapped around the theQuote class.

<SOAP-ENV:Body><ns1:getQuoteResponse><return xsi:type="ns1:theQuote"><getTheQuote xsi:type="xsd:string">Monday's child is fair of face</getTheQuote></return></ns1:getQuoteResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

SOAP – Server

SOAP is the acronym for Simple Object Access Protocol, it uses the web and XML for a client to find out what functions are available on a server using the WSDL (Web Server Description Language) (this is also in XML format) and once the client knows what functions are available and also what parameters can be passed, the client is then able to call the function and gain the response from the server.

I am using the Zend Framework to auto generate the WSDL file since there does not appear to be at present a function within the PHP standard framework to create one, I have included below how to install the Zend framework. Once you have installed the framework you need to either alter the php.ini file to tell it where the framework is or within the .php file for your server include details on where the framework is within that and include the relevant files.

Below I have told the php runtime within the .php file where the zend library framework is by using ini_setup include path settings.

/* setup the including path for the zend library framework */
ini_set('include_path', '/usr/share/php/libzend-framework-php/');
/* and include the zend soap files */
require_once 'Zend/Soap/AutoDiscover.php';
require_once "Zend/Soap/Server.php";

with including the auto discover (the WSDL generator part of the zend framework that we are wanting) and the server php files.

For the auto discover to work you need to use the php doc notation so that the WSDL can be generated correctly, so for example if you was using a function that was taking in a string and returning a string as in the main example below, you would use something like this (where the php doc is /** start)

  /**
   * @param string $quote
   * @return string
  */
  function getQuote($quote) {

To generate WSDL file I am using the parameter within the URL (?wsdl) and thus if that is present auto generate the WSDL file else act as the server, so

/* if the client is requesting the WSDL file (the web service definition language) */
if(isset($_GET['wsdl'])) {
   // use the zend soap autodiscover function to create the wsdl file from using the phpdoc info from the class QuoteOfTheDay */
    $autodiscover = new Zend_Soap_AutoDiscover();
    $autodiscover->setClass('QuoteOfTheDay');
    $autodiscover->handle();

with using the Zend_Soap_AutoDiscover class and then setting up the class that you want to expose to the WSDL generator / server and then output (handle) the WSDL request.

Here is the full code

<?php
 
/* setup the including path for the zend library framework */
ini_set('include_path', '/usr/share/php/libzend-framework-php/');
/* and include the zend soap files */
require_once 'Zend/Soap/AutoDiscover.php';
require_once "Zend/Soap/Server.php";
 
/* this is the class to be *expose* to the SOAP interface */
class QuoteOfTheDay {
 
  /* the quotes to be used from within the function getQuote */
  private $quotes = array("monday" => "Monday's child is fair of face", "tuesday"=>"Tuesday's child is full of grace","wednesday" =>"Wednesday's child is full of woe",
  "thursday" =>"Thursday's child has far to go", "friday" => "Friday's child is loving and giving", "saturday" =>"Saturday's child works hard for a living",
  "sunday" =>"But the child who is born on the Sabbath Day - Is bonny and blithe and good and gay");  
 
/* you have to use the phpDoc format for the zend soap auto discover to pick up the parameters and return types */
  /**
   * @param string $quote
   * @return string
  */
  function getQuote($quote) {
    /* just encase the string is in uppercase*/
    $symbol = strtolower($quote);
    /* if there is a quote for the day requested */
    if (isset($this->quotes[$quote])) {
      return $this->quotes[$quote];
    } else {
      /* else error */
      throw new SoapFault("Server","Unknown Symbol '$quote'.");
    }
  }
}
 
/* if the client is requesting the WSDL file (the web service definition language) */
if(isset($_GET['wsdl'])) {
   // use the zend soap autodiscover function to create the wsdl file from using the phpdoc info from the class QuoteOfTheDay */
    $autodiscover = new Zend_Soap_AutoDiscover();
    $autodiscover->setClass('QuoteOfTheDay');
    $autodiscover->handle();
} else {
  // otherwise I am the server in question, setup a new soap server with the a handle on the class QuoteOfTheDay 
    $soap = new Zend_Soap_Server("http://localhost/projects/webservice/zend_soap_server.php?wsdl"); // this current file here
    $soap->setClass('QuoteOfTheDay');
    $soap->handle();
}
?>

Save that as the zend_soap_server.php within your local PHP web serving directory you need to have the zend framework installed, below may help (within the php.ini there will either a extension list normally within a Windows setup)

in Linux (Ubuntu/Kubuntu etc) I installed the zend frame work for php by

aptitude install libzend-framework-php

or on windows this URL may be of use ? installing zend framework on windows

This is the output of the XML for the WSDL generation, it would be the output of a URL similar to http://localhost/zend_soap_server.php?wsdl

<definitions name="QuoteOfTheDay" targetNamespace="http://localhost/projects/webservice/zend_soap_server.php">
-
<types>
<xsd:schema targetNamespace="http://localhost/projects/webservice/zend_soap_server.php"/>
</types>
-
<portType name="QuoteOfTheDayPort">
-
<operation name="getQuote">
<documentation>@param string $quote</documentation>
<input message="tns:getQuoteIn"/>
<output message="tns:getQuoteOut"/>
</operation>
</portType>
-
<binding name="QuoteOfTheDayBinding" type="tns:QuoteOfTheDayPort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
-
<operation name="getQuote">
<soap:operation soapAction="http://localhost/projects/webservice/zend_soap_server.php#getQuote"/>
-
<input>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/projects/webservice/zend_soap_server.php"/>
</input>
-
<output>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/projects/webservice/zend_soap_server.php"/>
</output>
</operation>
</binding>
-
<service name="QuoteOfTheDayService">
-
<port name="QuoteOfTheDayPort" binding="tns:QuoteOfTheDayBinding">
<soap:address location="http://localhost/projects/webservice/zend_soap_server.php"/>
</port>
</service>
-
<message name="getQuoteIn">
<part name="quote" type="xsd:string"/>
</message>
-
<message name="getQuoteOut">
<part name="return" type="xsd:string"/>
</message>
</definitions>

UDP Server

As from the UDP Client here is the UDP Server, it is similar to the TCP Server in that it makes connections with a remote client and sends and receives data, but once again this data is not checked to make sure that it has been received (the “fire and forget” setup again)

So to start with we need to create a server socket for the clients to connect to

DatagramSocket theSocket = new DatagramSocket(9999);

since we now have a place for the client to connect to we now need to wait for a client connection request

// create a place for the client to send data too
theRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);
// wait for a client to request a connection
theSocket.receive(theRecievedPacket);

once a client has requested some data, we can start the process of setting up the response, the response starts of with getting there details, there IP address and also the port number that they wish to talk on ( image attached below of the wireshark request and different port number)

// get the client details
clientAddress = theRecievedPacket.getAddress();
clientPort = theRecievedPacket.getPort();

and then just build up the response to send back and send it

String message = "Server - client sent : " + new String(theRecievedPacket.getData(),0, theRecievedPacket.getLength());
outBuffer = message.getBytes();
 
// send some data to the client
theSendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort);
theSocket.send(theSendPacket);

here is the full java code

import java.io.*;
import java.net.*;
 
public class UDPServer {
 
	DatagramSocket theSocket = null;
	int serverPort = 9999;
 
	public UDPServer()
	{
		try {
			// create the server UDP end point
			theSocket = new DatagramSocket(serverPort);
 
			System.out.println("UDP Socket (end point) created");
		} catch (SocketException ExceSocket)
		{
			System.out.println("Socket creation error : "+ ExceSocket.getMessage());
		}
	}
 
	public void clientRequest()
	{
		DatagramPacket theRecievedPacket;
		DatagramPacket theSendPacket;
		InetAddress clientAddress;
		int clientPort;
		byte[] outBuffer;
		byte[] inBuffer;
 
		// create some space for the text to send and recieve data 
		outBuffer = new byte[500];
		inBuffer = new byte[50];
 
		try {
			// create a place for the client to send data too
			theRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);
			// wait for a client to request a connection
			theSocket.receive(theRecievedPacket);
			System.out.println("Client connected");
 
			// get the client details
			clientAddress = theRecievedPacket.getAddress();
			clientPort = theRecievedPacket.getPort();
 
			String message = "Server - client sent : " + new String(theRecievedPacket.getData(),0, theRecievedPacket.getLength());
			outBuffer = message.getBytes();
 
			System.out.println("Client data sent ("+message+")");
			// send some data to the client
			theSendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort);
			theSocket.send(theSendPacket);
 
		} catch (IOException ExceIO)
		{
			System.out.println("Error with client request : "+ExceIO.getMessage());
		}
		// close the server socket
		theSocket.close();
	}
 
	public static void main(String[] args)
	{
		UDPServer theServer = new UDPServer();
		theServer.clientRequest();
	}
}

if you save that as UDPServer.java and then compile and run

javac UDPServer.java

and then run the java UDPserver program, I have inserted the “— waiting for the client to connect” line because that is where the server will stop waiting, once the client as connected the rest of the output will be outputted.

java UDPServer 
UDP Socket (end point) created
---- waiting for the client to connect
Client connected
Client data sent (Server - client sent : genux)

and then run the UDPClient to connect to the server

java UDPClient 
Client socket created
Message sending is : genux
Client - server response : Server - client sent : genux

Here is the image of a wireshark connection request over the UDP and with the clients port request number.

Request port number
Request port number

TCP Server

As a follow on from the IP Address in java, the next thing would be a server/client over the internet. This is a small demo on how to create TCP connections over IP Address (TCP basically means that the data sent over the connection will fully be sent, e.g. a linux cd image), shall do one in a UDP which is more of a send data and do not really care if data is sent across the network like radio, if you do miss part of a song it does not really matter.

So to start with, I am using the port number 9999, lets say that you are in a office with different phones running on different extension numbers but using the main telephone number to make calls on, so in this setup the IP address would be main office telephone number (one number to talk on) and the port number would be the different telephones extensions that you can talk on.

To create a server socket you need to pass in the port number as well.

ServerSocket theServerSocket = new ServerSocket(9999);

then to just wait for a connection to try and connect you

Socket sock = theServerSocket.accept();

which waits for a client connection (accept) , since the socket also has the clients IP address you can use that to make sure that you want to accept connections from that IP address if you wanted to, but to output the InetAddress you could use the “sock” from the above code

System.out.println("The client IP address is " + sock.getInetAddress());

the only other thing is that you can send data to the client which using a ObjectOutputStream with passing in the “sock” from the above code

ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeChars("Hi from the server");
oos.close();

Here is the full code

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
 
public class TCPServer {
	// the IP class as such
	InetAddress theTCPServer;
	// port to talk over
	int portNumber = 9999;
	// the socket for the communication to happen on
	ServerSocket theServerSocket;
 
	/* 
	 * Setup the server socket communication 
	 */
	public TCPServer() {
		try {
			// create the server socket on the port number
			theServerSocket = new ServerSocket(portNumber);
			System.out.println("Server created on port : "+portNumber);
		} catch (IOException ExecIO)
		{
			System.out.println("Error creating the server socket : "+ExecIO.getMessage());
		}
	}
 
 
	public void connections()
	{
		try {
			// accept a connection 
			Socket sock = theServerSocket.accept();
			System.out.println("Server accepted connection, send to handler");
 
			// print out the clients IP Address
			System.out.println("The client IP address is " + sock.getInetAddress());
 
			// send the message to the client
			ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
			System.out.println("Server socket opened");
			oos.writeChars("Hi from the server");
			oos.close();
 
			// close the socket
			sock.close();
 
		} catch  (IOException ExecIO)
		{
			System.out.println("Error creating connection : "+ExecIO.getMessage());
		}
	}
 
	public static void main(String[] args) {
		TCPServer theServer = new TCPServer();
		theServer.connections();
	}
}

save as TCPServer.java and then to compile, and run

javac TCPServer.java
java TCPServer

and the output would be something like

Server created on port : 9999
Server accepted connection, send to handler
The client address is /127.0.1.1
Server socket opened

you could use wireshark to watch what is happening.