nusoap codeigniter Web服务服务器和客户端

时间:2013-06-25 03:08:55

标签: php web-services codeigniter nusoap

我是webservice的新手,并且一直在寻找如何创建Web服务,此刻,我想我以某种方式设法创建一个,但它不会返回任何结果。我正在使用nusoap和Codeigniter。

WebService Server位于名为WebServiceTester

的应用程序中 下面的

是用作服务器的Bills_WS控制器的代码:

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";
    }
}

并且要调用它,我在另一个在事务控制器中使用的ws_client库下名为ussccsv1的应用程序(同一台机器)中调用它:

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);

        }
    }
}

我还包括我正在使用的nusoap_lib

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

我的问题是:  1.如何在bills_ws中调用Web服务? $url正确吗?因为它到目前为止它给我404错误HTTP找不到。  2. ws_clientbills_ws中的错误是?  3.当我回应它时,它会在count($r)中给我一个ws_client = 1

一直在尝试按照本教程,但我似乎并不完全理解它: - http://www.phpeveryday.com/articles/PHP-Web-Services-Fetching-Data-From-Database-P105.html - http://ellislab.com/forums/viewthread/59710/

提前谢谢。

1 个答案:

答案 0 :(得分:2)

上述代码的解决方案。

你的控制器:

<?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"));
    }

}

在/config/routes.php中输入

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

通过此URL访问WSDL

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

我希望你现在可以在浏览器上看到XML。

SOAP客户端代码。

<?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>';
            }
        }
    }



}

立即访问SOAP客户端

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

完成。