How to Create Web Service in PHP

Written by ian low
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Create Web Service in PHP
WSDL provides more integrated web application using the SOAP protocol. (cd and usb image by ana malin from Fotolia.com)

Creating a Web Service in PHP involves developing a web application that handles SOAP data using standardised WSDL classes and function calls. PHP5 has a SOAP extension module that can be used to develop a Web Service. Other open source packages are PHPxmlp (compliant up to WSDL 1.0), WSO2 WSF/PHP, Jelix (this can also create other types of web applications) and NuSoap (a rewrite of SOAPX4 from NuSphere and Dietrich Ayala, compliant with WSDL 1.1). It is possible to create a Web Service in PHP using NuSoap. Other packages will follow the principles, but possibly involve further steps.

Skill level:
Easy

Other People Are Reading

Things you need

  • Website that runs PHP
  • FTP upload software
  • Web browser

Show MoreHide

Instructions

  1. 1

    Download nusoap-0.9.5.zip (see Resources for link). Extract the contents of nusoap-0.9.5.zip to a folder called C:\nusoap on your computer.

  2. 2

    Upload the lib folder in C:\nusoap to the root directory of your website using FTP software.

  3. 3

    Copy and paste the following code into a file called C:\convserver.php. This code creates a wsdl/soap server object and then declares two functions, "ConvC2Fahrenheit" and "ConvF2Celsius" that will provide the service.

    <?php

    //#1) Import the Nusoap library:

    require_once('./lib/nusoap.php');

    //#2) Instantiate the soap server object:

    $server = new soap_server;

    //#3) Initialize WSDL for conserver.php

    $server->configureWSDL('convserver', 'urn:convserver');

    //#4) Declare input Parameter array WSDL data object

    $server->wsdl->addComplexType(

    'ParmsIn',

    'complex Type',

    'struct',

    'all',

    '',

    array(

                 'temperature' =&gt; array('parms' =&gt; 'temperature', 'type' =&gt; 'xsd:int'),
    
    
    
                 'precision' =&gt; array('parms' =&gt; 'precision', 'type' =&gt; 'xsd:int'),
    

    )

    );

    //#5) Declare output Parameter array WSDL data object

    $server->wsdl->addComplexType(

    'ParmsOut',

    'complex Type',

    'struct',

    'all',

    '',

    array(

                 'input' =&gt; array('parms' =&gt; 'input', 'type' =&gt; 'xsd:string'),
    
    
    
                  'output' =&gt; array('parms' =&gt; 'output', 'type' =&gt;'xsd:string')
    

    ));

    //#6) Register i.e. "declare" the ConvC2Fahrenheit WSDL function:

    $server->register('ConvC2Fahrenheit', //<-function name

    array('parms_in' =&gt; 'tns:ParmsIn'),  //&lt;-input parameter array
    
    
    
    array('parms_out' =&gt; 'tns:ParmsOut'), //&lt;-output parameter array
    
    
    
    'urn:convserver', //&lt;-soap namespace
    
    
    
    'urn:convserver#ConvC2Fahrenheit', //&lt;-soap function address
    
    
    
    'rpc', //&lt;-use rpc
    
    
    
    'encoded', //&lt;encode data
    
    
    
    'Convert Celsius to Fahrenheit' //description
    

    );

    //#7) Register i.e. "declare" the ConvC2Fahrenheit WSDL function:

    $server->register('ConvF2Celsius', //<-function name

    array('parms_in' =&gt; 'tns:ParmsIn'), //&lt;-input parameter array
    
    
    
    array('parms_out' =&gt; 'tns:ParmsOut'), //&lt;-output parameter array
    
    
    
    'urn:convserver', //&lt;-soap namespace
    
    
    
    'urn:convserver#ConvF2Celsius', //&lt;-soap function address
    
    
    
    'rpc', //&lt;-use rpc
    
    
    
    'encoded', //&lt;encode data
    
    
    
    'Convert Fahrenheit to Celsius' //description
    

    );

    function ConvC2Fahrenheit($parms_in){

    $input = 'Celsius to Fahrenheit Conversion:&lt;br/&gt;' .
    
    
    
                'Input :' . $parms_in['temperature'] . '&lt;br/&gt;' .
    
    
    
                'Precision: ' . $parms_in['precision'] . '&lt;br/&gt;';
    
    
    
    
    
    $fahrenheit = round( (float)(1.8 * $parms_in['temperature'] + 32),
    
    
    
              $parms_in['precision']);
    
    
    
    //#5) Return the WSDL input and output values:
    
    
    
    return( array('input'=&gt;$input,'output'=&gt;'Output: '.$fahrenheit));
    

    }

    function ConvF2Celsius($parms_in) {

    $input = 'Fahrenheit to Celsius Conversion:&lt;br/&gt;' .
    
    
    
                'Input :' . $parms_in['temperature'] . '&lt;br/&gt;' .
    
    
    
                'Precision: ' . $parms_in['precision'] . '&lt;br/&gt;';
    
    
    
    
    
    $celsius = round( (float)(($parms_in['temperature'] - 32) / 1.8 ),
    
    
    
              $parms_in['precision']);
    
    
    
    //#6) Return the WSDL input and output values:
    
    
    
    return( array('input'=&gt;$input,'output'=&gt;'Output: '.$celsius));
    

    }

    //Now call the soap server object service member using HTTP Post data:

    $server->service($HTTP_RAW_POST_DATA);

    ?>

  4. 4

    Copy and paste the following code into a file called C:\convclient.php. This code instantiates a wsdl/soap client object, which calls convserver.php.

    <?php

    //#1) Import the Nusoap library:

    require_once('./lib/nusoap.php');

    //#2) Create the WSDL soap client instance

    $client = new soapclient('http://www.4meo.org/nusoap/convserver.php?wsdl', true);

    if ($_GET['f'] != ""){

    $parms = array('temperature' =&gt; $_GET[f], 'precision'=&gt;2);
    
    
    
    
    
    //#3) Call the convserver's ConvF2Celsius using normal soap:
    
    
    
    //$result = $client-&gt;call('ConvF2Celsius', array('parms_in' =&gt; new soapval(
    
    
    
    //          'parms_in', 'Parameters',  $parms, false, 'urn:exampleURN'))  );
    
    
    
    //#3) Call the convserver's ConvF2Celsius using WSDLl soap:
    
    
    
    $result = $client-&gt;call('ConvF2Celsius',array('parms_in' =&gt; $parms));
    

    }else if($_GET['c'] != ""){

    $parms = array('temperature' =&gt; $_GET[c], 'precision'=&gt;2);
    
    
    
    
    
    //#4) Call the convserver's ConvC2Fahrenheit using normal soap:
    
    
    
    //$result = $client-&gt;call('ConvC2Fahrenheit', array('parms_in' =&gt; new soapval(
    
    
    
    //          'parms_in', 'Parameters',   $parms, false, 'urn:exampleURN')) );
    
    
    
    //#4) Call the convserver's ConvC2Fahrenheit using WSDL soap:
    
    
    
    $result = $client-&gt;call('ConvC2Fahrenheit',array('parms_in'=&gt;$parms));
    

    }else {

    echo 'Usage: convclient.php?[f=?|c=?]';

    }

    //Output result:

    print_r($result['input'] . $result['output']);

    ?>

  5. 5

    Upload convserver.php and convclient.php to the root directory of your website.

  6. 6

    Open your browser at the uploaded convclient.php file on your website, and specify "?f=70" at the end of the URL, e.g. "http://www.mysite.com/convclient.php?f=70". The browser will return the input values "Fahrenheit=70 and precision=2" and the output value "Celsius=21".

  7. 7

    Edit the URL so that it now specifies "?c=21" at the end of the URL (e.g., "http://www.mysite.comconvclient.php?c=21.") and then press "Enter." It will return the input values "Celsius=21 and precision=2" and the output value "Fahrenheit=69.8".

  8. 8

    Open your browser at the uploaded convserver.php and specify "?wsdl" at the end of the URL (e.g., "http://www.mysite.com/convserver.php?wsdl"). The NUSoap libraries will generate a Web Service documentation page.

  9. 9

    Click on the WSDL link to view the WSDL schema document for your PHP Web Service.

Tips and warnings

  • The following can be used to debug any problems in the soap client object in convclient.php:
  • 1) To check for any faults in the result output xml, use "if ($client->fault)".
  • 2) To check for any errors, use "if($client->get Error())".
  • 3) To view the input data soap xml, use "echo $client->request".
  • 4) To view the output data soap xml, use "echo $client->response".
  • 5) To view the trace debug for the application, use "echo $client->debug_str".

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.