2013-06-25 103 views
0

我在新的web服務,並一直在尋找如何創建Web服務,在那一刻,我覺得我莫名其妙地設法使一個,但它不返回任何結果。我使用nusoap和Codeigniter。的NuSOAP web服務CodeIgniter的服務器和客戶端

WebService的服務器是在一個名爲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); 

     } 
    } 
} 

我是al所以包括nusoap_lib我使用:

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

我的問題是: 1.如何調用在bills_ws web服務?是$url是否正確?因爲迄今它給了我404錯誤HTTP未找到。 2.是ws_client還是bills_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

問題解決了:用這個例子來解決這個問題: http://board.phpbuilder.com/showthread.php?10224396-php-xml-NuSoap-!working – Ponce

+0

如果你已經解決了這個問題,請發表答案在這裏,並接受你的答案,這樣其他人也會受益。 – Sushil

回答

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

訪問WSDL通過此URL

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 

完成。

+0

我創建了一個單獨的類庫,從那裏我打電話給使用Master.fruits類的方法,其中Master => class和fruits is =>方法,它在php中工作正常,但在C#中拋出錯誤,因爲它不應該是master.fruits它應該作爲masterfruits,任何想法 – musthafa

+0

你能否提供一些C#代碼片段。 –