2

I get a problem when I use the SOAP component in zend framework 2.0

The following is the code :

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

}

when I visit http://localhost/test/index?wsdl ,It returns the WSDL.

But when I visit http://localhost/test/index,It returns the error:

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"

When I visit http://localhost/test/client, It returns

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)

here is the MyTest.php file

namespace User\Model;

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

Thanks in advance.

Leo
  • 127
  • 2
  • 8

1 Answers1

0

To create a Soap server, you only need the WSDL file, not the URL that is used to serve it to other clients. In fact, it is better performing NOT to use a URL for this, because every request to the Soap server will start a subrequest to fetch the WSDL.

Try to put the WSDL somewhere once it's created, and fetch it from there on subsequent requests. Autodetecting it every time is only good when doing development without a specified interface. Which will break things once somebody else is using this interface and relies on its stability.

Sven
  • 69,403
  • 10
  • 107
  • 109