-->

nusoap codeigniter webservice server and client

2019-04-17 11:49发布

问题:

I am new in webservice, and have been looking in how to create web service, at the moment, I think I somehow manage to make one but it does not return any result. I am using nusoap and also Codeigniter.

The WebService Server is in an application called WebServiceTester

below is the code for the Bills_WS controller that serve as the server:

class Bills_WS extends CI_Controller
{
    function __construct() 
    {
        parent:: __construct ();         
    }

    public function index()
    {
       $this->load->library('Nusoap_lib');

       $namespace = "http://localhost:8080/webservicetester/bills_ws.php?wsdl";

       $server = new nusoap_server;
       $server->configureWSDL('WebServiceTester');
       $server->wsdl->schemaTargetNamespace = $namespace;

       $server->register('hello');

       $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
       $server->service($HTTP_RAW_POST_DATA);
    }
    function hello()
    {
        return "greetings from server";
    }
}

and to call it, I am calling it in another application(same machine) called ussccsv1 under ws_client library that is being used in transaction controller:

class Ws_client
{
    private $CI = null;
    function __construct() 
    {
        $this->CI =& get_instance();
    }

    public function transaction_send_ws($param)
    {
        $this->CI->load->library('nuSoap_lib');

        $url = 'http://localhost/webservicetester.php/bills_ws?wsdl';  

        $client = new nusoap_client($url);
        $response = $client->call('hello');

        if($client->fault)
        {
            echo "FAULT:".$client->faultcode;
            echo "string: ".$client->faultstring;
        }
        else
        {
            $r = $response;
            count($r);
echo "count".count($r);

        }
    }
}

I am also including the nusoap_lib I am using:

class Nusoap_lib
{
    function nusoap_lib()
    {
        include(APPPATH.'libraries/nusoap/nusoap'.EXT); 
    }
}

my questions are: 1. How do I invoke the webservice in the bills_ws ? is the $url correct? coz so far it gives me 404 error HTTP not found. 2. Is the fault in ws_client or bills_ws? 3. but it gives me a count($r) in ws_client = 1 when i echo it.

Been trying to follow this tutorial, but I don't seem to understand it fully: -http://www.phpeveryday.com/articles/PHP-Web-Services-Fetching-Data-From-Database-P105.html -http://ellislab.com/forums/viewthread/59710/

Thank you in advance.

回答1:

Solution for above code.

Your Controller:

<?php
class Bills_WS extends CI_controller {
    function __construct() {
        parent::__construct();

        $this->load->library("Nusoap_lib");
        $this->load->model("Member");

        $this->nusoap_server = new soap_server();
        $this->nusoap_server->configureWSDL("Bills_WSDL", "urn:Bills_WSDL");

        $this->nusoap_server->register('hello',                // method name
            array('name' => 'xsd:string'),        // input parameters
            array('return' => 'xsd:string'),      // output parameters
            'urn:Bills_WSDL',                      // namespace
            'urn:Bills_WSDL#hello',                // soapaction
            'rpc',                                // style
            'encoded',                            // use
            'Says hello to the caller'            // documentation
        );
    }

    function index(){

        if($this->uri->rsegment(3) == "wsdl") {
            $_SERVER['QUERY_STRING'] = "wsdl";
        } else {
            $_SERVER['QUERY_STRING'] = "";
        }        

        function hello($name) {
                return 'Hello, ' . $name;
        }
        $this->nusoap_server->service(file_get_contents("php://input"));
    }

}

Make Entry in /config/routes.php

$route['Bills_WS/wsdl'] = "Bills_WS/index/wsdl";

Access WSDL By This URL

http://localhost/ci_nusoap/index.php/Bills_WS/wsdl

i hope you can see XML on browser now.

SOAP Client Code.

<?php
class Soap_client extends CI_controller {

    function __construct() {
        parent::__construct();

        $this->load->library("Nusoap_lib");
        $this->load->helper("url");

    }

    function index() {

        $this->soapclient = new soapclient(site_url('Bills_WS/index/wsdl'), true);

        $err = $this->soapclient->getError();
        if ($err) {
            echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';

        }

        $result = $this->soapclient->call('hello', array('name' => 'Scott'));
        // Check for a fault
        if ($this->soapclient->fault) {
            echo '<h2>Fault</h2><pre>';
            print_r($result);
            echo '</pre>';
        } else {
            // Check for errors
            $err = $this->soapclient->getError();
            if ($err) {
                // Display the error
                echo '<h2>Error</h2><pre>' . $err . '</pre>';
            } else {
                // Display the result
                echo '<h2>Result</h2><pre>';
                print_r($result);
            echo '</pre>';
            }
        }
    }



}

Access SOAP Client Now

http://localhost/ci_nusoap/index.php/soap_client

Done.