0

我在 cake php 2.3 中创建 Web 服务时遇到问题,我在我的网站中使用 nusoap lib 我在 Web 服务输出简单代码中有错误:输出: 错误响应不是 text/xml 类型:text/html

网络服务控制器:

App::uses('AppController', 'Controller');
App::uses('Sanitize', 'Utility');
ini_set('soap.wsdl_cache_enabled', 0);
class WebservicesController  extends AppController{

    var $components = array('RequestHandler');
    var $helpers = array('Text', 'Xml');

    public $name = 'Webservices';
    public $useTable = false;
    public $uses = array();
    public $autoRender = false;
     public $layout = false;

    function process()
    {


         Configure::write('debug',0);
        Configure::write('Session.start', false);


        App::import('Vendor', 'nusoap',array('file'=>'nusoap'.DS.'lib'.DS.'nusoap.php')); 
        $server = new soap_server(); 

        $endpoint = 'http://localhost/mysite/webservices/process';

        //initialize WSDL support
        $server->configureWSDL('helloWorldwsdl', 'urn:helloWorldwsdl', $endpoint);

        $server->soap_defencoding='UTF-8';
        $server->decode_utf8 = false;

         $this->RequestHandler->respondAs('xml');
        //$this->layoutPath = 'xml';

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

        $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
        $server->service($HTTP_RAW_POST_DATA);
        $this->autoRender = false;
        exit();
    }


     function helloWorld() {
          return 'Hello';
    } 



} 

客户端代码:

require_once('lib/nusoap.php');
// Create the client instance
$client = new nusoap_client('http://localhost/mysite/webservices/process?wsdl');
$client->soap_defencoding='UTF-8';
$client->decode_utf8 = false;
// Check for an error
$err = $client->getError();
if ($err) {
    // Display the error
    echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
    // At this point, you know the call that follows will fail
}
//====================================================== 

// Call the SOAP method
$result = $client->call('helloWorld') ;
// Check for a fault
if ($client->fault) {
    echo '<h2>Fault</h2><pre>';
    print_r($result);
    echo '</pre>';
} else {
    // Check for errors
    $err = $client->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>';
    }
} 
4

1 回答 1

1

我尝试了您的示例,并且在soap服务器中记录方法“helloworld”时,它可以正常添加一个空数组,例如:

$server->register('helloWorld',            // method name
    array(),                               //input parameters
    array('return' => 'xsd:string'),       // output parameters
    'urn:helloWorldwsdl',                  // namespace
    'urn:helloWorldwsdl#helloWorld',       // soapaction
    'rpc',                                 // style
    'encoded',                             // use
    'Says hello to the caller'             // documentation
于 2013-10-09T14:08:45.727 回答