PHP soap server and dotNet c# client – part 1 [Nested Arrays]

Avatar von manfred

This entry will show you how to model a dotnet valid wsdl file, how to send dotnet valid responses from php and finally how to connect a c# client. This is the wsdl ‚DOTNET‘ way 3->5->7->2 to continue Johann’s previous post. It is easy, at least if you know what desires the Microsoft’s xml-serializer. To put the difficulty level one step higher we define and respond with a nested array.

Download the whole stuff

 

  • Model the wsdl file

    A general knowledge of wsdl should exist. You know ‚binding‘, ‚porttype‘ and ‚message‘ … lets move to the harder part: defining the complex type for a nested array that can be read by dotNet.


    <xsd:complexType name="Users">
      <xsd:complexContent>
        <xsd:restriction base="soapenc:Array">
          <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="typens:User[]"/>
        </xsd:restriction>
      </xsd:complexContent>
    </xsd:complexType>

    This arrayType with the name ‚Users‘ links to a single ‚User‘


    <xsd:complexType name="User">
      <xsd:all>
        <xsd:element name="Firstname" type="xsd:string" />
        <xsd:element name="Lastname" type="xsd:string" />
        <xsd:element name="Street" type="xsd:string" />
        <xsd:element name="ZIP" type="xsd:int" />
        <xsd:element name="City" type="xsd:string" />
        <xsd:element name="Country" type="xsd:string" />
      </xsd:all>
    </xsd:complexType>

     

  • Adding the webreference to your dotNet application will extend your project library as in figure

    cs class view

    Remember to name the webreference something like ‚blog.thinkphp.de‘ to add the namespaces to the classes.

    Access the user properties

    At any following line now add a breakpoint to trace the local variables.

     

  • Implementing the php soap-server

    The PHP and PEAR_SOAP version is somewhat uncomfortable. But I will show you the tricky part of a dotNet-valid soap response.


    <Users xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soap-enc:Array" soap-enc:arrayType="auth:User[2]">

    Have a look at the last attribute holding the ’soap-enc‘ namespace. Figuring out that dotNet xml-serializer needs the exact size of the response array took us several hours. Also make sure that variable names defined in the wsdl file have the same name and type in the response. That is also a common mistake.


    <Users xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soap-enc:Array" soap-enc:arrayType="auth:User[2]">
      <User xsi:type="auth:User">
        <Firstname xsi:type="xsd:string">Manfred</Firstname>
        <Lastname xsi:type="xsd:string">Weber</Lastname>
        <Street xsi:type="xsd:string">Street1</Street>
        <ZIP xsi:type="xsd:int">81543</ZIP>
        <City xsi:type="xsd:string">Munich</City>
        <Country xsi:type="xsd:string">Germany</Country>
      </User>
      <User xsi:type="auth:User">
        <Firstname xsi:type="xsd:string">Wolfram</Firstname>
        <Lastname xsi:type="xsd:string">Kriesing</Lastname>
        <Street xsi:type="xsd:string">Street2</Street>
        <ZIP xsi:type="xsd:int">81544</ZIP>
        <City xsi:type="xsd:string">Munich</City>
        <Country xsi:type="xsd:string">Germany</Country>
      </User>
    </Users>

     

  • PHP4 Soap Server

     

    Most important when writing the server with php4 and the pear package is to rewrite the response message because PEAR_SOAP does not define the size of arrays.

     

  • PHP5 Soap Server

    As announced PHP5 offers greater flexibility. No need for rewriting responses.

     

  • Links

    During the development process we discovered following tools as helpful.
    Webstudio 2.0

    Fiddler HTTP Proxy and Debugger

    Author:
    Manfred / Mayflower

  • Avatar von manfred

    Kommentare

    7 Antworten zu „PHP soap server and dotNet c# client – part 1 [Nested Arrays]“

    1. The method name „Authentification“ is not an english word, please use „Authenticate“ or „Identification“.

    2. Hi there, I’ve been trying to use your WSDL as the base for a PHP5 server and C# client, as in your example. I’m extremely confused, as soon as I change the text ‚Authentification‘ to something else, I immediately get parser errors from the .Net framework (not when I import the wsdl, but when the service is accessed).

      For example, I did a global search and replace on your wsdl, and changed ‚Authentification‘ to ‚Authentifications‘. As far as I’m aware, this should have pretty much no significance whatsover. But, when I invoke the GetUsers Method, I get a parser error from the response message. This makes no sense to me at all, if you could shed any light on the issue I’d be immensely grateful :)

      Cheers for the article, it’s been well useful so far, hopefully I can deal with this problem…

    3. hmm, I think I may have answered my own question. PHP seems to be caching the wsdl, despite my having turned off caching via

      ini_set(„soap.wsdl_cache_enabled“, „0“);

      The SOAP response is therefore using the urn:Authentification namespace, rather than the updated one I gave it.

      Do you know how to force PHP to update the WSDL?

    4. I tried to access this web service with a PHP client and had to change the WSDL file slightly to make it work :

      you mustn’t write „soapenc“ but „SOAP-ENC“ in the and elements.

    5. Hi,
      I’ve been trying to implement a Nusoap webserver with a C# client. Every works accept Soap Encoded Arrays. After trying quite a few things, I still receive the error „Reponse is not well-formed XML. — System.XML.XMLException: Root element is missing.“

      Any Ideas will be greatly appreaciated.
      Thanks.

    6. Avatar von
      Anonymous

      Hmmm… how secure is that? :)

    7. PHP soap server and dotNet c# client – part 1 [Nested Arrays] http://t.co/DnocnNYKFy via @mayflowerphp

    Schreibe einen Kommentar

    Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert


    Für das Handling unseres Newsletters nutzen wir den Dienst HubSpot. Mehr Informationen, insbesondere auch zu Deinem Widerrufsrecht, kannst Du jederzeit unserer Datenschutzerklärung entnehmen.