繁体   English   中英

nusoap codeigniter webservice服务器和客户端

[英]nusoap codeigniter webservice server and client

我是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 bills_ws $url是否正确? 因为它到目前为止它给我404错误HTTP找不到。 2. ws_clientbills_ws是否有错误? 但是当我回应它时,它给了我ws_client = 1count($r)

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

先感谢您。

上述代码的解决方案。

你的控制器:

<?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

完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM