Friday, 14 December 2012

Web Services: How PHP Kiss VB.NET? (Part 1)

XML, SOAP, and multitier programming is amazing technology that make we can collaborate with other programmer building big size and complex application in multiplatforms. This technology make us more easy and more simple in complicated world.

This tutorial about connecting PHP with VB.NET using web services. Why I use VB.NET to explain web services in PHP? I just want to show one of function of web services, connecting multiplatform.

In php, we can use nusoap library to create web services. You can download this library at http://sourceforge.net/projects/nusoap/. In this tutorial use nusoap-0.7.3.

    Download nusoap library.
    Extract to web directory, example within C:\AppServ\www\test. Rename folder became "nusoap". So, you get structure like this:

    Now, we will create server function. Create "server.php" within C:\AppServ\www\test\nusoap. Enter following code:
    01    <?
    02    //call library
    03    require_once ('lib/nusoap.php');
    04   
    05    //using soap_server to create server object
    06    $server = new soap_server;
    07   
    08    //register a function that works on server
    09    $server->register('hello');
    10   
    11    // create the function
    12    function hello($name)
    13    {
    14      if(!$name){
    15        return new soap_fault('Client','','Put your name!');
    16      }
    17        
    18      $result = "Hello, ".$name;
    19      return $result;
    20    }
    21   
    22    // create HTTP listener
    23    $server->service($HTTP_RAW_POST_DATA);
    24   
    25    exit();
    26    ?>
    Now, it is time to test. Point your browser to http://localhost/test/nusoap/server.php. If work, you will get like following screen:

    After create server, now we will test this server function. Create "client.php" at test/nusoap. Enter following code:
    01    <?
    02    require_once ('lib/nusoap.php');
    03   
    04    //Give it value at parameter
    05    $param = array( 'name' => 'Babablackship');
    06   
    07    //Create object that referer a web services
    08    $client = new soapclient('http://localhost:8048/test/nusoap/server.php');
    09   
    10    //Call a function at server and send parameters too
    11    $response = $client->call('hello',$param);
    12   
    13    //Process result
    14    if($client->fault)
    15    {
    16      echo "FAULT: <p>Code: (".$client->faultcode."</p>";
    17      echo "String: ".$client->faultstring;
    18    }
    19    else
    20    {
    21      echo $response;
    22    }
    23    ?>

Now, point your browser to http://localhost/test/nusoap/client.php. You should see:
You can change value of parameter at line 5. But if value is nothing, it raise error message. Next tutorial, we will try to read the web services from VB.NET. Don't miss it!

No comments:

Post a Comment