Question

I am creating a web service, I have added an operation which receive an object but my soap client doesn't recognize its attributes

<?php
class Application_Model_Contact {

    private $id;
    private $name;
    private $phone;

    /**
     * 
     * @param String $nome
     * @param String $phone
     */
    public function __construct($nome = null, $phone = null) {
    ....    
    }

}

...

class Application_Model_WebServices
{    
    /**
     * 
     * @param Application_Model_Contact $contact
     * @return boolean
     */
    public function adicionar(Application_Model_Contact $contact){
        return true;
    }

}

....

if (isset($_GET['wsdl'])) {
    $autodiscover = new Zend_Soap_AutoDiscover();
    $autodiscover->setClass('Application_Model_WebServices');
    $autodiscover->handle();
} else {
    $server = new Zend_Soap_Server();
    $server->setOptions(array(
        'soap_version' => SOAP_1_2,
        'actor' => 'http://localhost/AgendaTelefonicaPHPSOAP/public/webservice.php',
        'encoding' => 'UTF-8'
    ));
    $server->setWsdl('http://localhost/AgendaTelefonicaPHPSOAP/public/webservice.php?wsdl');
    $server->setClass('Application_Model_WebServices');
    $server->handle();
}

Using soapUI, I get the following xml to add this object

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://localhost/AgendaTelefonicaPHPSOAP/public/webservice.php">
   <soapenv:Header/>
   <soapenv:Body>
      <web:adicionar soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <contact xsi:type="web:Application_Model_Contact"/>
      </web:adicionar>
   </soapenv:Body>
</soapenv:Envelope>

Why my class attributes aren't being recognized?

Was it helpful?

Solution

In order for Zend_Soap_Autodiscover to add complex types to the WSDL, it needs to be able to see the attributes using reflection and it needs docblocks to inform it as to the type of each attribute.

So in order for your WSDL to include these complex types, which will then allow your client to pass those types in the SOAP request, you'll need to adjust the Applicaton_Model_Contact class as follows:

class Application_Model_Contact {

    /** @var string */
    public $id;
    /** @var string */
    public $name;
    /** @var string */
    public $phone;

    /**
     * 
     * @param String $nome
     * @param String $phone
     */
    public function __construct($nome = null, $phone = null) {
    ....    
    }

}

I ran your code before and after this modification and can confirm that after this modification the attributes of the contact record were successfully passed into the adicionar() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top