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>

Mono – web development on Linux

It is really easy to get some web c# (csharp) (asp.net) development within a Linux setup, since I use kubuntu which is derived from ubuntu. All you need to do is to

this installs the mono module for the apache2

aptitude install libapache2-mod-mono

to enable the module to work within apache2 you have to update the mods-enabled directory and to do this, just use the

a2enmod mod_mono_auto

which stands for apache2 (a2) enable (en) mod (module) and then the module name, you will need to restart apache2 for it to work

/etc/init.d/apache2 restart

that is it., then if you look within the /etc/apache2/mods-enabled you will notice the

mod_mono_auto.conf
mod_mono_auto.load

of course you could symlink them yourself, but it is just easier to use the a2enmod script.

Within the .conf file it tells apache what extensions to “listen” to direct to the module mono instead of either php module or just a static html web page.

To test the setup, just create a directory within your hosting directory this is normally /var/www/ or just place a file in that base directory /var/www it is up to you, but within that file you could create a basic test file like.

<%@ Page Language="C#" %>
<html>
  <head>
    <title>Apache2 Mod mono test page</title>
  </head>
  <body>
        <form id="form1" runat="server">
          <asp:label id="lbl1" runat="server">Apache2 Mod mono test page</asp:label>
        </form>
  </body>
</html>

the asp:label will be the main test, since that part of the csharp library as such. you should see something like

Apache2 Mod mono test page

C++ DLL Objects accessed from C#

Because there is a few things that c# cannot do compared to c++, e.g. cpu/memory management. And also there is probably allot of dll’s out there are still required for some events, c# is able to communicate with these files and use there functions within the c# language.

To create an dll within Visual Studio 2005 within the language c++, if you do the following

1. New project -> c++ -> empty project
2. Enter project name (e.g. hellocdll)
3. Right click source files ( in the solution explorer) add-> new item
enter an cpp filename.
4, Right click on the main project heading in the solution explorer -> properties
configuration properties->general inner screen project defaults -> configuration type, alter to dynamic library (.dll)
5. Copy and paste code and then compile.

#include <stdio.h>
 
extern "C"
{
  __declspec(dllexport) void DisplayMessageFromDLL()
  {
              printf ("Hi From the C DLL!\n");
  }
 
  __declspec(dllexport) int DisplayValueAndReturn(int i)
  {
         printf("Value %i\n", i);
         return i+2;
  }
}

The extern “C” means that the references within the code are going to be available externally and marco __declsepc(dllexport) is for the MS compile to inform that functions are to be available within an dll file.

To create an c# project to communicate with the above dll

1. New project -> c# -> empty project
2. Enter project name (e.g. hellodlltest)
3. Right click on the project name ( in the solution explorer) add-> new item
select class and enter a filename
4. Copy and paste the code and then compile. (you may need to change the Dllimport to the dll file name that has been created from the c dll project)

using System;
using System.Runtime.InteropServices;     // DLL support
 
namespace hellodlltest
{
    class hellodllC
    {
        [DllImport("hellocdll.dll")]
        public static extern void DisplayMessageFromDLL();
 
        [DllImport("hellocdll.dll")]
        public static extern int DisplayValueAndReturn(int i);
 
        static void Main ()
           {
                  Console.WriteLine ("C# program 'talking' to an C DLL");
            DisplayMessageFromDLL();
            Console.WriteLine(DisplayValueAndReturn(3).ToString());
           Console.ReadLine()
           }
    }
}

5. Copy the dll file from the debug directory of the c++ created dll project and place into the created bin/debug directory for this c# project

Operator

The standard class function are not able to utilize the standard operators within the c# language, for example the multiple, add, subtraction and divide in a mathematics example.

To overcome this problem, there is a ‘operator’ special syntax that allows for this to take place. The operator can work with any of the c# language standard functions of which you are able to program there functional aspects within the class. The syntax for this is

public static <return type> operator <operator type>(<parameter list of passed variables>);

for example if there was an return type of int and two integer values passed in the parameter list and using the addition operator.

public static int operator +(int a, int b)

Below is some code that will demonstrate this further within a general code development.

using System;
 
class operatorBase
{
       private int i;       // private member of the class
 
       public operatorBase() 
       {
              i = 0;       
       }
 
       public operatorBase(int init)  
       {
              this.i = init; 
       }
 
       // get and set the value for the private member i
       public int Value
       {
              get { return i;}
              set { i = value;}
       }
 
       // the operator +, parameters are the two values that you want to add, can be overloaded with different values
       // e.g. (int i2, int i3) for example.
       public static operatorBase operator +(operatorBase i2, operatorBase i3)
       {
              // create the return;
              operatorBase locali= new operatorBase();
              locali.i = i2.i + i3.i;  have access to the internals of passed parameters
              return  locali;       // return the operatorBase class
       }
}
 
class operatorTest
{
 
       public static void Main()
       {
              operatorBase opBase = new operatorBase();
 
              // set the value to 3 and also output the value;
              opBase.Value = 3;
              Console.WriteLine(opBase.Value);
 
              operatorBase opBase2 = new operatorBase(4);
 
              // to add to the operatorbases together, but will return an operatorBase, thus bracket the equation and use the .Value to get the value. 
              Console.WriteLine((opBase + opBase2).Value);
 
              // since creating two new on the fly operatorBase, then the result is an int value again.
              Console.WriteLine((new operatorBase().Value = 3) + (new operatorBase().Value = 2));
       }
}

Yielding results

Yield is a new syntax word within the .net 2 language. This allows for the IEnumerable interface to have direct interaction instead of using the IEnumerator interface to iterate though a result set. For easier demonstration, I think, the code examples, the first code is a .net 1 version of the interface IEnumerable.

using System;
using System.Collections;
 
// the inital class is IEnumerable- means that it has to implement the GetEnumerator
public class Class : IEnumerable 
{
       // internal class to implement the IEnumerator interface, which inturn has to do the MoveNext, Current, Reset methods.
       private class ClassEnumerator : IEnumerator
       {
              // internal values to go through the array
              private int index = -1;
              private Class P;
              // setup the internal variable
              public ClassEnumerator(Class P) 
              {
                     this.P = P; 
              }
              // move within the array, if any values are present.
              public bool MoveNext()
              {
                     index++;
                     return index < P._Names.Length;
              }
              // set the internal to -1, thus restart
              public void Reset() 
              { 
                     index = -1;
              }
 
              // get the Current object
              public object Current 
              { 
                     get 
                     { 
                            return P._Names[index]; 
                     } 
              }
       }
 
       // return a IEnumerator object of the above with itself passed
       public IEnumerator GetEnumerator()
       {
              return new ClassEnumerator(this);
       }
 
       // the internal string array of names
       private string[] _Names;
       // the new class setup process, 
       public Class(params string[] Names)
       {
              _Names = new string[Names.Length];
              // copy the passed parameter starting at position 0
              Names.CopyTo(_Names,0);
       }
}
 
class ClassProgram
{
       static void Main(string[] args)
       {
              Class ClassArr = new Class("Student1", "Student2","Studentx");
              foreach (string s in ClassArr)
                     Console.WriteLine(s);
       }
}

This is able to output a string of

Student1
Student2
Studentx

Since the Class class (it is a class of students), there is allot of code to iterate with the foreach function.

Within .net 2 there is a nice and more tighter method called yield, an yield will yield a result and be able to iterate though a result set with the IEnumerable return set. Again, here is the source code.

using System;
using System.Collections;
 
class yielding
{
       // same as the class as in .net 1 version.
       public class Class : IEnumerable
       {
              // but only need to implement this one method to do the same as the pervious separate class
              public IEnumerator GetEnumerator()
              {
                     int counter = -1;
                     while (counter++ < (_Names.Length-1))
                            yield return _Names[counter];
              }
 
              // same as the other .net 1 class internals for the private variable and the constructor
              private string[] _Names;              
              public Class(params string[] Names)
              {
                     _Names = new string[Names.Length];
                     Names.CopyTo(_Names,0);
              }
       }       
 
       static void Main(string[] args)
       {
              // same as the .net 1 version
              Class ClassArr = new Class("Student1", "Student2", "Studentx");
              foreach (string s in ClassArr)
                     Console.WriteLine(s);
       }
}

Far less code and also the result is exactly the same.

Also there are other benefits of the yield attached to an IEnumerable interface, here is some more code to demonstrate how to implement a power function to display the power result being built up. This demonstrates how to use just a single method to return an iterate-able result.

using System;
using System.Collections;
 
class yielding
{
       // same as the class as in .net 1 version.
       public class Class : IEnumerable
       {
              // but only need to implement this one method to do the same as the pervious separate class
              public IEnumerator GetEnumerator()
              {
                     int counter = -1;
                     while (counter++ < (_Names.Length-1))
                            yield return _Names[counter];
              }
 
              // same as the other .net 1 class internals for the private variable and the constructor
              private string[] _Names;              
              public Class(params string[] Names)
              {
                     _Names = new string[Names.Length];
                     Names.CopyTo(_Names,0);
              }
       }       
 
       // IEnumerable linked to yield is new in .net 2, it allows for the 
       // iteration of this method, acting like array instead of a single return.
       public static IEnumerable Power(int number, int exp)
       {
              int counter = 0, result = 1;       // setup the default values.
              while (counter++ < exp)              // increament counter and whilst lower than the passed parameter exp
              {
                     result *= number;       // result = result * number;
                     yield return result;       // yield the result and return
              }
       }
 
       // can also just iterate though a array of strings (in this example)
       public static IEnumerable StReturn(params string[] names)
       {
              int counter = -1;
              while (counter++ < (names.Length-1))
                     yield return names[counter];
       }
 
       static void Main(string[] args)
       {
              // can do a return of a power function as well
              foreach(int e in Power(4,3))
                     Console.WriteLine(e.ToString());
 
              // you can just iterate a string 
              string[] st = {"student1","student2","studentx"};
              foreach(string s in StReturn(st))
                     Console.WriteLine(s.ToString());
 
              // same as the .net 1 version
              Class ClassArr = new Class("Student1", "Student2", "Studentx");
              foreach (string s in ClassArr)
                     Console.WriteLine(s);
       }
}

I have also include the code from above to demonstrate the similar usage for iterate though a string array.

Output would be

4
16
64
student1
student2
studentx
Student1
Student2
Studentx

You will need to have .net 2 installed or mono (use the gmcs part of mono to use the .net 2 functions.

Generics

A Generic is a way to declassify the type of variable used, e.g. if there was a function to add up two integer values, but you had two double values, then the int function would not function correctly, so there would be a need to function overload the function to allow for the double increment as well. But just envisage that the same function would be required for characters, floats, personally created ones etc, then there would be more functions per type of variable than the actual work done.

A generic class negates this by allowing any class type passed to the function, as long as the type has the standard increment process defined, it will work with any type.

This will demonstrate the basics of the generics.

using System;
 
// create a class with a generic type of T (e.g. pass in the type of your choice
public class GenTest<T>;
{
       // the private value
       private T genericValue;
 
       // default constructor for the class (same name as the class)
       // ~ is the deconstructor
       public GenTest(T generic) 
       {
              genericValue = generic;       // set the internal T = passed value
       }
 
       // return the value of the internal value
       public T ReturnValue()
       {
              return genericValue;
       }
}
 
public class MainProgram
{
       static void Main()
       {
              // create a integer type class, with the default value of 2
              GenTest<int> genInt = new GenTest<int>(2);
              Console.WriteLine("Int generic = " + genInt.ReturnValue());
 
              // same class create a string with the default value of "cool"
              GenTest<string> genString = new GenTest<string>("cool");
              Console.WriteLine("String generic = " + genString.ReturnValue());
       }
}

If you save the code and run with .net 2 or greater (.NET Framework) ( since this was part of .net 2 and not with .net 1). The output will be

Int generic = 2
String generic = cool

As you can see the same class is able to use both integer and strings as the default variable type.

Collections – ArrayList

Collections – ArrayList, the ArrayList is part of the Collections namespace. The ArrayList acts as the same as the array (Array Example) apart from it allows other functional aspects to access the list of array.

Shall comment more on the different aspects in later tutorials, but basically there many different parts to a class that allow for better functionality e.g
1. Interfaces = base of a class for strict implementation)
2. IEnumerable (which is a interface) = means that you implement GetEnumerator aspects of a class, e.g. for the foreach command)
3. Generics (which is part of .net 2) = Similar to templates (Generics) in c++ and also generics from Java).
4. Etc. shall comment more on these.

This is the code

using System;
 
namespace ArrayListTest
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
            arrayList.Add(2);
            arrayList.Add(3);
            arrayList.Add("hi there");
 
            foreach (object obj in arrayList)
                Console.WriteLine(obj.ToString());
        }
    }
}

If you save as arraylisttest.cs, and then run, the output will be

2
3
hi there

It basically allows you to add in any object into the ArrayList, and since objects have the ToString() function, then it is able to print out the object, of course custom made objects will either display there type or the ToString() will have to be implemented.