Language Specific Instructions for consuming web services

ASP.NET with C#

For .NET to consume a Web Service, a proxy stub must be created. This can be done using either the Visual Studio IDE or via command line utility wsdl.exe.
  • Visual Studio

    The Visual Studio interface automatically creates a proxy stub for you when you add a reference to the web service.

    Steps:

    1. Right-click on your project and choose "Add Web Reference"
    2. In the dialog box that appears enter the Uri of the web service (i.e. the asmx file), give the web reference a name, and click Add Reference
    3. Enter the URL and Name the reference - this will become the proxy class name
    4. Create an instance of the proxy class (Note: you may need a
      using
      command depending on whether you are coding in a class library or a web page)
    5. Create any parameters needed
    6. Call desired operation of the web service using dot notation, sending in any needed parameters
    7. Process the response (in this example the response is displayed to the user)

    Webpage Code:

    ImportantDates importantDatesProxy = new ImportantDates();
    DateTime startDate = Convert.ToDateTime("6/1/2006");
    DateTime endDate = Convert.ToDateTime("6/30/2006");
    Response.Write(importantDatesProxy.GetDates(startDate, endDate));
    

    Alternate Webpage Code - Prints Details of Each Record:

    ImportantDates importantDatesProxy = new ImportantDates();
    DateTime startDate = Convert.ToDateTime("6/1/2006");
    DateTime endDate = Convert.ToDateTime("6/30/2006");
    ImportantDateEntry[] entryDates = importantDatesProxy.GetDates(startDate, endDate);
    foreach(ImportantDateEntry entry in entryDates)
    {
       Response.Write(entry.Category.Description  + ": " + entry.Title + " - " + entry.StartDate.ToShortDateString() + " to " entry.EndDate.ToShortDateString() + "<br />" );
    }
    
  • wsdl.exe (http://msdn.microsoft.com/library/en-us/cptools/html/cpgrfWebServicesDescriptionLanguageToolWsdlexe.asp)

    The command line utility (wsdl.exe) manually creates a proxy stub for the web service

    Steps:

    1. Run the wsdl.exe utility with the full URL of the WSDL as the parameter
    2. Compile the code into a dll and move into your bin directory
    3. Create an instance of the proxy class
    4. Create any parameters needed
    5. Call desired operation of the web service using dot notation, sending in any needed parameters
    6. Process the response (in this example the response is displayed to the user)

    wsdl.exe Code:

    wsdl.exe http://projects.ls.ucdavis.edu/webservices/ImportantDates.asmx?WSDL

    Webpage Code

    ImportantDates importantDatesProxy = new ImportantDates();
    DateTime startDate = Convert.ToDateTime("6/1/2006");
    DateTime endDate = Convert.ToDateTime("6/30/2006");
    Response.Write(importantDatesProxy.GetDates(startDate, endDate));
    

    Alternate Webpage Code - Prints Details of Each Record:

    ImportantDates importantDatesProxy = new ImportantDates();
    DateTime startDate = Convert.ToDateTime("6/1/2006");
    DateTime endDate = Convert.ToDateTime("6/30/2006");
    ImportantDateEntry[] entryDates = importantDatesProxy.GetDates(startDate, endDate);
    foreach(ImportantDateEntry entry in entryDates)
    {
       Response.Write(entry.Category.Description  + ": " + entry.Title + " - " + entry.StartDate.ToShortDateString() + " to " entry.EndDate.ToShortDateString() + "<br />" );
    }
    

ColdFusion

ColdFusion provides several methods for consuming web services including: using the CFML tag cfinvoke and using CreateObject() within cfscript. An overview of the methods and how to process the return values is located here: http://livedocs.macromedia.com/coldfusion/6/Developing_ColdFusion_MX_Applications_with_CFML/webservices4.htm.
  • <cfinvoke ...> tag (http://livedocs.macromedia.com/coldfusion/7/htmldocs/00001537.htm)

    With the cfinvoke tag, you reference the WSDL file and invoke an operation on the web service with a single tag.

    Steps:

    1. Start the cfinvoke tag with the required fields
    2. Send in any needed parameters
    3. Close the cfinvoke tag
    4. Process the response (in this example the response is displayed to the user)

    Code:

    <cfinvoke
    	webservice="http://projects.ls.ucdavis.edu/webservices/ImportantDates.asmx?WSDL"
    	method="GetDates"
    	returnVariable="results">
    		<cfinvokeargument name="startDate" value="2006-06-01">
    		<cfinvokeargument name="endDate" value="2006-06-30">
    </cfinvoke>
    <cfoutput>#results#</cfoutput>
    
  • CreateObject() within cfscript (http://livedocs.macromedia.com/coldfusion/7/htmldocs/00001538.htm)

    In CFScript, you use the CreateObject function to connect to the web service. After connecting, you can make requests to the service.

    Steps:

    1. Start the cfscript tag
    2. Use the CreateObject function to connect to the web service
    3. Call desired operation of the web service using dot notation, sending in any needed parameters, and store response in a variable
    4. Close the cfscript tag
    5. Process the response (in this example the response is displayed to the user)

    Code:

    <cfscript>
    myWebservice = CreateObject("webservice", "http://projects.ls.ucdavis.edu/webservices/ImportantDates.asmx?WSDL");
    results = myWebservice.GetDates(startDate="2006-06-01", endDate="2006-06-30");
    </cfscript>
    <cfoutput>#results#</cfoutput>
    

PHP

PHP by default does not come with a bundled SOAP extension. There are a few major SOAP implementations for PHP including: NuSOAP and PEAR::SOAP.
  • NuSOAP (http://sourceforge.net/projects/nusoap/)

    NuSOAP is a set of PHP classes - no PHP extensions required - that allow developers to create and consume web services based on SOAP 1.1, WSDL 1.1 and HTTP 1.0/1.1.

    Steps:

    1. Add library files/functions to script
    2. Define where the WSDL is located
    3. Create an instance of the soapclient class to access the web service
    4. Create an array of parameters for the SOAP request
    5. Call the function and pass our parameters to get a response from the remote server (in this example the response is displayed to the user)

    Code:

    <?php
    require_once('nusoap.php'); 
    $wsdl="http://projects.ls.ucdavis.edu/webservices/ImportantDates.asmx?WSDL";
    $client=new soapclient($wsdl, 'wsdl');
    $params=array('startDate'=>'2006-06-01', 'endDate'=>'2006-06-30');
    echo $client->call('GetDates', $params);
    ?>
    
  • PEAR::SOAP (http://pear.php.net/package/SOAP/)

    PEAR is a framework and distribution system for reusable PHP components. PEAR::SOAP is a module that allows integrating SOAP into PHP using the PEAR framework.

    Steps:

    1. Install PEAR::SOAP package (and any needed dependencies)
    2. Access using SOAP_WSDL or SOAP_Client
      • SOAP_WSDL
        1. Include PEAR::SOAP's SOAP_Client class
        2. Create an instance of the SOAP_WSDL class, based on the location of the WSDL file for the service you are accessing
        3. Create a proxy object based on the WSDL
        4. Create an array of parameters for the SOAP request
        5. Call the function and pass our parameters to get a response from the remote server (in this example the response is displayed to the user)
      • SOAP_Client
        1. Include PEAR::SOAP's SOAP_Client class
        2. Create a new SOAP client using PEAR::SOAP's SOAP_Client-class, where the parameter is the Uri to the web service
        3. Create an array of parameters for the SOAP request
        4. Call the function and pass our parameters to get a response from the remote server (in this example the response is displayed to the user)

    Install Code:

    % pear install SOAP

    SOAP_WSDL Code:

    <?php
    require_once('SOAP/Client.php');
    $wsdl=new SOAP_WSDL('http://projects.ls.ucdavis.edu/webservices/ImportantDates.asmx?WSDL');
    $client=$wsdl->getProxy();
    $params=array('startDate'=>'2006-06-01', 'endDate'=>'2006-06-30');
    echo $client->GetDates($params);
    ?>
    

    SOAP_Client Code:

    <?php
    require_once('SOAP/Client.php');
    $client=new SOAP_Client('http://projects.ls.ucdavis.edu/webservices/ImportantDates.asmx');
    $params=array('startDate'=>'2006-06-01', 'endDate'=>'2006-06-30');
    echo $client->call("GetDates", $params);
    ?>