WSF/PHP WSDL Generation 2 Minutes Introduction

Whenever you deploy a service script in WSF/PHP you can access its WSDL by adding “?wsdl” or “?wsdl2” (for WSDL version 2.0) to the service endpoint. Anyway you may have noticed that for some services it will just list the operations with xsd:anyType as the input and output message schemes.

For an example for an operation like this,

<?php

function getInvoice($order_no) {
   //something that return an invoice for this order 
}

$operations = array("getInvoice" => "getInvoice");

$svr = new WSService(array("operations" => $operations));

$svr->reply();
?>

This will generate you a nice WSDL but with a schema similar to this,

<xsd:element name="getInvoice">
    <xsd:complexType>
       <xsd:sequence>
           <xsd:element name="order_no" type="xsd:anyType"/>
       </xsd:sequence>
    </xsd:complexType>
</xsd:element>

This schema is not enough for get an understanding about the request, response message formats, because it uses xsd:anyType to represent a general message. In order to get more specific schema, you need to provide more details to the WSDL generator. Here is how it is done using annotations.

<?php
/**
 * @param int $orderNo Order Number
 * (maps to xsd:int)
 * @return string $invoice Invoice as a string
 * (maps to xsd:string)
 */
function getInvoice($order_no) {
   //something that return an invoice for this order 

}

$operations = array("getInvoice" => "getInvoice");
$opParams = array("getInvoice" => "MIXED"); // Have to declare parameters as MIXED

$svr = new WSService(array("operations" => $operations, "opParams" => $opParams));

$svr->reply();
?>

Now check the schema. It will gives you the information specific to your request, response messages.

<xsd:schema elementFormDefault="qualified"
    targetNamespace="http://www.wso2.org/php/xsd">
    <xsd:element name="getInvoice">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="orderNo" type="xsd:int"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="getInvoiceResponse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="invoice" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

So it is your chance to do more experiment with this feature. Check WSDL generation API for more snippets.

This entry was posted in 2 minutes guide, Tutorial/Guide, web services, WSDL, wsf/php, wso2, xml schema and tagged , , , , . Bookmark the permalink.

5 Responses to WSF/PHP WSDL Generation 2 Minutes Introduction

  1. David says:

    Thankyou

  2. Garth says:

    Dimithu,

    How does one explicitly invoke the WSDL generation mode? For instance, if within a framework, the query itself is filtered, and the service does not seem to notice the ‘?wsdl’ in the $_GET; Setting it by hand doesn’t seem to help either.

    Any idea?

  3. dimuthu says:

    Is that your deployment setting that filter ?wsdl. Then you can get the wsdl just by trying hosting it in some other deployment. I don’t think any other possible way a user can get the wsdl, without doing some hacking in the wsf/php.

  4. vasagan says:

    You can use $wsdl = file_get_contents(‘http://www.yourdomain.com/wsdlfile.php?wsdl’);
    echo $wsdl;

  5. James says:

    Hi Dimuthu,
    Thanks for this. The WSDL generation functionality is great.
    I just have one minor issue. I’ve written a web service in php that is being accessed by a .NET client. Their framework is generating a WCF object based on my WSDL and the name of their object is coming out as ws_webService_phpPortTypeClient. I’m assuming this is being generated by the “service name” section in the WSDL, which is “ws_webService.php”.

    Is there any way I can specify the service name? There’s nothing mentioned in the WSDL generation API docs.

    Thanks in advance.
    – James

Leave a Reply

Your email address will not be published. Required fields are marked *