2

在 zend 框架 2.0 中使用 SOAP 组件时出现问题

以下是代码:

namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Soap\Server;
use Zend\Soap\Client;
use Zend\Soap\AutoDiscover;
use User\Model\MyTest;

class TestController extends AbstractActionController {
private $_WSDL_URI = "http://127.0.0.1/test/index?wsdl";

private function handleWSDL() {
    $autodiscover = new AutoDiscover(); 
    //ini_set("soap.wsdl_cache_enabled", 0);
    $autodiscover->setClass('User\Model\MyTest')
                 ->setUri($this->_WSDL_URI);
    $wsdl = $autodiscover->generate();
    header("Content-type: text/xml");
    echo $wsdl->toXml();
    exit;   
}

private function handleSOAP() {
    $soap = new Server($this->_WSDL_URI,array('encoding' => 'utf-8','soap_version'=>SOAP_1_2));
    $soap->setClass('User\Model\MyTest');
    $soap->handle();
    exit;
}

public function indexAction(){
    if(isset($_GET['wsdl'])) {
        $this->handleWSDL();
    } else {
        $this->handleSOAP();
    }
}

public function clientAction(){
    $client = new Client('http://127.0.0.1/test/index?wsdl');
    $result = $client->getUser(31);
    var_dump($result);exit;
}

}

当我访问时http://localhost/test/index?wsdl ,它返回 WSDL。

但是当我访问时http://localhost/test/index,它返回错误:

SOAP-ERROR: Parsing WSDL: Couldn't load from http://127.0.0.1/test/index?wsdl: failed to load external entity"http://127.0.0.1/test/index?wsdl"

当我访问时http://localhost/test/client,它会返回

An error occurred
An error occurred during execution; please try again later.
Additional information:
SoapFault
File:
E:\WWW\vendor\ZF2\library\Zend\Soap\Client.php:1087
Message:
Wrong Version

Stack trace:

 #0 E:\WWW\vendor\ZF2\library\Zend\Soap\Client.php(1087):  SoapClient->__soapCall('getUser', Array, NULL, NULL, Array)
 #1 E:\WWW\module\User\src\User\Controller\TestController.php(44): Zend\Soap\Client->__call('getUser', Array)

这是 MyTest.php 文件

namespace User\Model;

class MyTest{
/**
 * To get the register information of the user
 * 
 * @return string
 */
public function getUser(){
    return 'hello';
}
}

提前致谢。

4

1 回答 1

0

要创建 Soap 服务器,您只需要 WSDL 文件,而不需要用于将其提供给其他客户端的 URL。事实上,最好不要为此使用 URL,因为对 Soap 服务器的每个请求都会启动一个子请求来获取 WSDL。

尝试在创建 WSDL 后将其放在某个地方,并在后续请求中从那里获取它。只有在没有指定接口的情况下进行开发时,每次都自动检测它才是好的。一旦其他人使用此接口并依赖于其稳定性,这将破坏事物。

于 2012-12-18T19:19:22.243 回答