Getting started

This library acts as a frontend that processes the SOAP service request. The request can be for service documentation (HTML API), description (WSDL) or invoking the SOAP methods. You creates an handler class that is used as the backend by the library or for gathering service documentation/description.

Create SOAP handler

Define your class as an ordinary PHP class. Add annotiations on methods declaring input parameters and return values. These annotations will be used by the library for generating WSDL and API documentation for your web service.

<?php

namespace Blogger\WebServices\Soap;

use Blogger\Models\Post;
use Blogger\Database\Connection;

class BlogService implements SoapHandler
{

        public function __construct()
        {
                // initialize i.e. database connection
        }

        /**
         * Add blog post.
         * @param Post $post The blog post.
         * @return boolean
         */
        public function addPost($post)
        {
                return $this->_database->save($post);
        }

        /**
         * Get all posts by author.
         * @param string $author The post author.
         * @param string $date Get posts since this date.
         * @return Post[]
         */
        public function getPosts($author, $date)
        {
                return $this->_database->find('post', array(
                            'author' => $author,
                            'date'   => $date
                ));
        }

}

Publish SOAP service

Pass you class literal string or an class instance to the soap service class to register it as the SOAP service handler. We call the useWrapper() method to tell the library to use wrapped mode for input parameters and return values (without doing so your class methods gets a single arguement and needs to have its return value wrappped in an array).

<?php

require_once("../../vendor/autoload.php");

use Blogger\WebServices\Soap\BlogService;
use UUP\WebService\Soap\SoapService;

// 
// Create the SOAP service:
// 
$service = new SoapService(BlogService::class);
$service->useWrapper();

// 
// Handle the SOAP request:
// 
$service->sendRequestResponse();

The WSDL and API documentation is generated by calling methods on the soap service object. You can decide if you want to let the SOAP service class handle it automatic for you (as in the example above).

Use with frameworks

A framework usually takes care of routing the request based on i.e. request parameters or URL mapping. In this case we can call the target operations direct from the controller action handling methods.

<?php

use Blogger\WebServices\Soap\BlogService;
use Blogger\WebServices\Soap\Factory;
use Framework\Mvc\Controller;

class BlogController extends Controller
{

        private $_service;

        public function __construct()
        {
                $this->_service = Factory::create(BlogService::class);
        }

        public function index()
        {
                $this->_service->sendHandlerResponse();         // Send SOAP response.
        }

        public function docs($format)
        {
                $this->_service->sendDocumentation($format);    // Send API documentation.
        }

        public function wsdl()
        {
                $this->_service->sendDescription();             // Send WSDL document.
        }

}

Utility classes

The library provides the SOAP request class that hides the details of detecting SOAP service mode and sending various response. Use it if you like to manual send the response:

<?php

/**
 * Process the SOAP service request.
 * 
 * @param UUP\WebService\Soap\SoapService $service The SOAP service.
 * @param UUP\WebService\Soap\SoapRequest $request The SOAP request.
 */
function process($service, $request)
{
        switch ($request->target) {
                case self::TARGET_DOCS:
                        $service->sendDocumentation('html');
                        break;
                case self::TARGET_WSDL:
                        $service->sendDescription();
                        break;
                case self::TARGET_SOAP:
                        $service->sendHandlerResponse();
                        break;
        }
}