Tuesday, March 31, 2009

Creating The Web Services Client (Part 2)

The web services can be accessed by anykind of applications such as desktop application, web application, even mobile application. In the previous tutorial, we have learnt how to create a desktop application to access the web service. Now, we are going to create a web application with PHP to access that web service.

The Code

This is the PHP script. But, there is a requirement before you can run this script. You need a supporting library named NuSOAP. You can find it from www.sourceforge.net.

NuSOAP came in a zipped file. So, unzip it and copy the nusoap.php file to the same location with this script so it can be included with this script.

<html>
    <head><title>Web Service Client</title></head>
<body>
    <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
        <input type="text" name="name"><input type="submit" name="submit" value="SUBMIT">
    </form>
    <div id="result" style="font-weight:bold;">
    <?php
    if($_POST['submit']){
        include("nusoap.php");
        
        // THE LOCATION OF WEB SERVICES
        $client=new soap_client("http://localhost:8080/axis/services");
        
        // CALL THE WEB SERVICE WITH THE METHOD NAME, INPUT PARAMETER, AND SERVICE NAME
        $result = $client->call('greeting', array("name" => $_POST['name']), 'helloService');
        if($client->getError()){
            echo $client->getError();
        }
        else{
            echo $result;
        }
    }
    ?>
    </div>
</body>
</html>

Run It!

This is the screenshot of this application while running.


No comments: