CodeDeveloper.net | A Site For And By Web Developers

May/10

20

NuSoap, SOAP, and PHP Web Services Tutorial – Part 2

As I stated in Part One, in this tutorial my aim was to create a PHP based web services client/server. SOAP and NuSoap were the tools I chose to use after looking into my options. This tutorial is beginner level as my experience with SOAP and NuSoap prior to this was very little. This tutorial post will cover client setup and testing.

The Client

The web services server was setup and working, and so now it was time to work on the client. NuSoap installation is the same for the client site. First, download the latest NuSoap Code. Install NuSoap by extracting the PHP files that are needed and putting them somewhere accessible. It is useful to place the class files in a place within your PHP path. The main NuSoap file (nusoap.php) can be placed somewhere more convenient. As with setting up a server using NuSoap, setting up a client is also very simple. The following is what I came up with and named client.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
 
require_once('nusoap.php');
 
$url = "http://tcwebdevelopment.com/service/server.php?wsdl";
 
$client = new nusoap_client($url);
 
$err = $client->getError();
 
if ($err) {
    echo '<p><b>Error: ' . $err . '</b></p>';
}
 
$args = array('value' => 'Good');
 
$return = $client->call('pollServer', array($args));
 
echo "<p>Value returned from the server is: " . $return . "</p>";
 
?>

Let’s take a look at some of the important lines of code here and what they do.

  • Line 3 – We include the nusoap.php file. The path to the file itself needs to be used here. This will give us access to all of the NuSoap features we will be using.
  • Line 5 – Here we set a variable with the URL of our server. Note that we add ?wsdl to the end of the URL.
  • Line 7 – We create a new nusoap client passing in the URL as an argument.
  • Line 9 – Line 9 is where we see if there were any errors setting up the client using the nusoap client’s getError() method.
  • Line 11-14 – If there were any errors let’s notify the user.
  • Line 15 – The server we setup in part one of the tutorial is expecting an array with a key of value containing the value. We create the array here, giving it a value of ‘Good’ to begin.
  • Line 17 – In this line we call the server using the nusoap client’s call method. The first parameter we pass to the method is the name of the exposed function we wish to use. The second is an array of the params we wish to pass to the server.
  • Line 19 – Let’s print out what is returned.

After saving this file I uploaded it to another one of my websites for testing. By navigating to the location in my browser I saw the following:

NuSoap Client Good Return

As expected, the return showed “good data” being returned. Just to make sure the server is working as expected I changed line 15 to the following:

1
$args = array('value' => 'Bad');

After uploading and refreshing the client location I saw the following:

NuSoap Client Bad Return

It seemed that my server was returning data as expected! That’s how easy it is to create a working client/server using NuSoap and PHP for your web services. That’s all for this beginner level tutorial. There are a lot of other useful things you can do using SOAP, NuSoap, and PHP with ease!

· · · · ·

9 comments

  • Ricardo · May 21, 2010 at 8:25 pm

    I congratulate them on the library NuSOAP there are very few tutorials and examples of use. Please clarify several doubts about all that with PHP 5.3 occurred several errors.

  • Author comment by johng · May 22, 2010 at 8:12 am

    What sort of errors were you having that need clarification?

  • Greg · August 19, 2010 at 10:58 am

    Its really great script, this i tested and $value['value'] is not giving “Good”

    its giving just A and if i return $value, its giving Array.

    can you please help me.

  • Author comment by johng · August 19, 2010 at 6:12 pm

    Hi Greg,

    Is this just giving you the actual character “A”? Also, did you setup a script similar to what I did in the 1st part of the tutorial for the server? What happens if you switch the argument passed in to “Bad”?

  • Farshid Alavi Moghadam · December 22, 2010 at 6:51 am

    Hey man, I have tried to test your example, but the result wasn’t as same as it is shown above. The result I was given is : “Value returned from the server is:”. Tell me where the problem comes from!!!!

  • Farshid Alavi Moghadam · December 22, 2010 at 7:16 am

    The error i was given is “Error: no transport found, or selected transport is not yet supported!”;

  • Ryan · January 5, 2011 at 5:59 am

    I hope this helps you guys with sending multiple data through to the server

    /************************************
    * Server Code
    ************************************/

    require_once(‘./classes/nusoap/nusoap.php’);

    $server = new nusoap_server;

    $server->configureWSDL(‘server’, ‘urn:server’);

    $server->wsdl->schemaTargetNamespace = ‘urn:server’;

    $server->register(‘pollServer’,
    //make a name and surname array for the request wich will
    //be passed to the function parameter 1 as $value array
    array(‘name’ => ‘xsd:string’, ‘surname’ => ‘xsd:string’),
    array(‘return’ => ‘xsd:string’),
    ‘urn:server’,
    ‘urn:server#pollServer’);

    function pollServer($value)
    {
    //modded the function
    $name = isset($value['name']) ? $value['name'] : ”;
    $surname = isset($value['surname']) ? $value['surname'] : ”;

    if( $name !== ” && $surname !== ” )
    {
    return “You have been Registered”;
    }else
    {
    return “Not registered!!”;
    }
    }

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ”;

    $server->service($HTTP_RAW_POST_DATA);

    /********************************************
    * Client Script
    ********************************************/

    require_once(‘./classes/nusoap/nusoap.php’);

    //Start the SOAP Client
    $client = new soapclient(‘http://soaptest.rmeintjes.eleven/server.php?wsdl', true);

    //Get the first error if any
    $error = $client->getError();

    if($error)
    {
    echo ‘Error :

    '.$error.'

    ‘;
    }

    //Define the result
    $result = $client->call(‘pollServer’ , array(‘name’ => ‘Good’, ‘surname’ => ‘NOTGOOD’)); //fix

    //Check for Faults
    if( $client->fault )
    {
    echo ‘Fault :

    '.$result.'

    ‘;
    }else
    {
    $error = $client->getError();

    if($error)
    {
    echo ‘Error :

    '.$error.'

    ‘;
    }else
    {
    echo ‘Result :

    '.$result.'

    ‘;
    }
    }

    //Display the request
    echo “Request : “;
    echo ‘

    '.htmlspecialchars($client->request, ENT_QUOTES).'

    ‘;

    //Display the result
    echo ‘Response : ‘;
    echo ‘

    '.htmlspecialchars($client->response, ENT_QUOTES).'

    ‘;

    //Display the debug information
    echo ‘Debug Code : ‘;
    echo ‘

    '.htmlspecialchars($client->debug_str, ENT_QUOTES).'

    ‘;

  • Ryan · January 5, 2011 at 6:00 am

    I hope this helps you guys with sending multiple data through to the server

    /************************************
    * Server Code
    ************************************/

    require_once(‘./classes/nusoap/nusoap.php’);

    $server = new nusoap_server;

    $server->configureWSDL(‘server’, ‘urn:server’);

    $server->wsdl->schemaTargetNamespace = ‘urn:server’;

    $server->register(‘pollServer’,
    //make a name and surname array for the request wich will
    //be passed to the function parameter 1 as $value array
    array(‘name’ => ‘xsd:string’, ‘surname’ => ‘xsd:string’),
    array(‘return’ => ‘xsd:string’),
    ‘urn:server’,
    ‘urn:server#pollServer’);

    function pollServer($value)
    {
    //modded the function
    $name = isset($value['name']) ? $value['name'] : ”;
    $surname = isset($value['surname']) ? $value['surname'] : ”;

    if( $name !== ” && $surname !== ” )
    {
    return “You have been Registered”;
    }else
    {
    return “Not registered!!”;
    }
    }

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ”;

    $server->service($HTTP_RAW_POST_DATA);

    /********************************************
    * Client Script
    ********************************************/

    require_once(‘./classes/nusoap/nusoap.php’);

    //Start the SOAP Client
    $client = new soapclient(‘server script url here ‘, true);

    //Get the first error if any
    $error = $client->getError();

    if($error)
    {
    echo ‘Error :

    '.$error.'

    ‘;
    }

    //Define the result
    $result = $client->call(‘pollServer’ , array(‘name’ => ‘Good’, ‘surname’ => ‘NOTGOOD’)); //fix

    //Check for Faults
    if( $client->fault )
    {
    echo ‘Fault :

    '.$result.'

    ‘;
    }else
    {
    $error = $client->getError();

    if($error)
    {
    echo ‘Error :

    '.$error.'

    ‘;
    }else
    {
    echo ‘Result :

    '.$result.'

    ‘;
    }
    }

    //Display the request
    echo “Request : “;
    echo ‘

    '.htmlspecialchars($client->request, ENT_QUOTES).'

    ‘;

    //Display the result
    echo ‘Response : ‘;
    echo ‘

    '.htmlspecialchars($client->response, ENT_QUOTES).'

    ‘;

    //Display the debug information
    echo ‘Debug Code : ‘;
    echo ‘

    '.htmlspecialchars($client->debug_str, ENT_QUOTES).'

    ‘;

  • Rajasekhar · April 13, 2011 at 12:09 am

    Hi, this tutorial give me good idea on web services using soap.

    thank u

Leave a Reply

>>

Theme Design by devolux.nh2.me