Zend_Soap_Server returning object – working!

Posted on July 7, 2009

0


Here’s a source code example on how to return an object from a Zend_Soap_Server. Thanks to James Gordon’s blog!


<?php

// Simple Zend_Soap_Server example returning an object

//****************************************************
// Zend Framework 1.8
include_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);
//****************************************************

require_once "Zend/Soap/Server.php";
require_once "Zend/Soap/AutoDiscover.php";

if(isset($_GET['wsdl'])) {
	$autodiscover = new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex');
	$autodiscover->setClass('TestService');
	$autodiscover->handle();
} else {
	$soap = new Zend_Soap_Server("http://localhost/ZendSoap/services/test.php?wsdl"); // this current file here
	$soap->setClass('TestService');
	$soap->handle();
}

//*************************************
// Classes used by getGroup service method below

class Person
{
    /** @var string */
    public $name = '';
}

class Group
{
    /** @var Person[] */
    public $persons;
}

//*************************************

class TestService
{
    /**
     * @return Group
     */
    public function getGroup()
    {
        $group = new Group();

       	$person = new Person();
        $person->name = "Annah";
		$group->persons[] = $person;

       	$person = new Person();
        $person->name = "Sarah";
        $group->persons[] = $person;

        return $group;
    }
}

Advertisements
Tagged: ,
Posted in: PHP, Zend Framework