Cloud King PHP SOAP Server: Implement an SOAP server API with handler classes

Recommend this page to a friend!
  Info   View files Example   View files View files (31)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2020-12-29 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 56 All time: 9,902 This week: 342Up
Version License PHP version Categories
cloudking 1.0GNU Lesser Genera...5PHP 5, Web services
Description Author

This package can implement an SOAP server API with handler classes.

It can use a class that defines all the API endpoint functions and another class that implements the actual code of each API function.

The package can handle HTTP requests and dispatches them according to the configured API definition.

Example client code is also provided within the package for testing purposes.

Innovation Award
PHP Programming Innovation award nominee
December 2020
Number 8
SOAP is a mature protocol to implement Web services based on the exchange of XML messages.

Setting up a server to implement SOAP based Web services is not a simple task.

This package simplifies that task by allowing the customization of the functionality of the SOAP Web services be implemented in an easier way.

It just requires writing simple PHP classes that define how the Web service functions should respond.

Manuel Lemos
Picture of Jorge Castro
  Performance   Level  
Name: Jorge Castro <contact>
Classes: 21 packages by
Country: Chile Chile
Innovation award
Innovation award
Nominee: 10x

Winner: 2x

Details

cloudking

A SOAP Server Engine for PHP 5.6 and higher.

It supports SOAP 1.1, 1.2 or both. JAVA usually supports 1.1, C# supports both.

Packagist Total Downloads [Maintenance]() [composer]() [php]() [php]() [CocoaPods]()

Example of the UI

![](docs/screenshot1.jpg)

Why we need Web Service SOAP?

Because some legacy projects are still using it. SOAP it is also declarative (instead of REST that lacks of a specification)

And why to use this library?

  • It generates the server (service class and the client)
  • It generates the WSDL.
  • It works with complex structures.

Getting started

1.Creating a Definition

What is a definition class. It is a class that defined the name,namespace, functions and complex-types defined by the web service.

In this example, we are defining a simple function : function hello($param);

class ExampleDefinition {
    public static $service;

    /
     * You must create this file manually.
     *
     * @param bool $gui if true then it shows the web gui
     */
    public static function init($gui = true) {
        $FILE = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
        $NAMESPACE
            = 'http://www.examplenamespace.cl/'; // the namespace of the web service. It could be anything and not specifically an existing url
        $NAME_WS = 'Example2WS'; // the name of the service

        self::$service = new CloudKing($FILE, $NAMESPACE, $NAME_WS);
        self::$service->allowed_input['gui'] = $gui; // set to false to disable the web gui.
        self::$service->serviceInstance = null;
        self::$service->verbose = 2; // for debug purpose
        self::$service->description = 'Example server SoapKing';

        self::$service->addfunction('hello',
            [
                self::$service->param('param', 'string', true, false),
            ],
            [
                self::$service->param('return', 'string')
            ],
            'Example of function'
        );
    }

    public static function run() {
        $r = self::$service->run();
        echo $r;
    }
}

We will explain more about it later.

2. Running the first time.

Let's call the definition as follow

<?php

include 'ExampleDefinition.php';

ExampleDefinition::init();
ExampleDefinition::run();

Right now, this web service doesn't run because we are not associate the definition with a real function. The functions are defined in a class SERVICE CLASS.

docs/helloservice.jpg

Here you have the definition of the services and the information about it. If you want to disable it, then you could disable the $GUI, or lower the VERBOSE to 0.

3. Calling the Service

Right now ,the service is not complete but you can test it. If you click in WSDL description, it will show a huge xml file. You could download it or copy the address (ended with ?wsdl)

In this example, we will use the program SOAPUI (there is a free and paid version)

Open the SOAPUI (or the program that you want to use), and paste the url of the wsdl (obtained in the previous step) and runs it.

docs/hellosoap.jpg

And it will show all the methods defined. In our case, there is a single function called "hello". Then, you can run it.

docs/hellosoap2.jpg

If you run it, it will fail. Why? It is because we are yet to define the service class.

4. Service class

In our website, there is a link called SOURCE GENERATION. Click on it and it will show the next screen.

You can generate a c# code, and both php code (from server and client). We need to generate the server source. If you click on the View PHP Server Source, you can look at the code. However, you can also generate it directly. However, for that, you will need to set the folder

docs/helloservice2.jpg

Let's modify the file of the step 2

<?php

include 'ExampleDefinition.php';

ExampleDefinition::init();
ExampleDefinition::$service->folderServer=__DIR__; // or you could select any folder.
ExampleDefinition::run();

And if we refresh the website, it will show

docs/helloservice3.jpg

So, you could generate 1 class and 1 interface automatically. Click on it, and it will generate both files

docs/helloservice4.jpg

It will generate the folder service and 2 files

? service

___ ? ExampleHelloService.php (our service class)

___ ? IExampleHelloService.php (the interface class)

5. Editing the Service Class.

This class is half-generated. We could edit our operations inside this class, so let's change our code.

class ExampleHelloService implements IExampleHelloService {

   /
    * @inheritDoc
    */
   public function hello(&$param) {
      return $param." world!"; // <--- edit this.
   }
} // end class 

6. Editing our service

Let's modify our service defined in the step 2 again and now, we must indicate our service class (created in the step 4)

ExampleDefinition::init();
ExampleDefinition::$service->folderServer=__DIR__; // or you could select any folder.run_initial.php
ExampleDefinition::$service->serviceInstance=new ExampleHelloService();
ExampleDefinition::run();

And let's run it again using SOAPUI

docs/hellosoap3.jpg

And now, we have the service up and running.

You could later disable the GUI

Definition

Parameters

Parameters are used to indicate the arguments of a function, the return value of a function or the fields of a complex structure.

Defining a single parameter.

> param(name of the parameter, type , reference, required , description)

  • name of the parameter : it is the name of the parameter, for example "idCompany", "money" and such
  • type: the type of the parameter. It could be a primitive value (string,integer,etc.) or it could be defined by a structure/class (called complex). If we want to use a complex type, we need to define it after we want to use it.
  • reference: (optional)If true then the value is also returned. If false, then the value is not returned.
  • required: (optional)If true then the value is required.
  • description: (optional) An optional description of the parameter.
self::$service->param('counter', 'integer') // a parameter called "counter" of the type integer
self::$service->param('name', 'string', true, false, "description") // a string parameter called "name"
self::$service->param('prod', 'Product', true, false, "description") // a parameter called "prod" of the complex type Product (it must be defined)

Defining an array of parameters

It is also possible to define an array (list) of parameters.

> paramList(name of the parameter, type , reference, required , description )

self::$service->paramList('names', 'string') // defining an array of string called "names"
self::$service->paramList('productList', 'Product',false,false,'List of products') // defining an array of complex type Product called "productList"

> Note: This function defines automatically a complex type called ArrayOf\<name of the parameter\> . If the complex type exists (complex with the same name), then it uses it instead of create a new one.

Complex Types

It is also possible to define a complex type. A complex type is used when we need to define a model or structure.

> addType( name of the type , [ parameters ] , description)

  • name of the type: it is the name of the time. The case of the name matters.
  • parameters: We could define one of more parameters for the type. We could even define list of parameters or even parameters that use complex types.
  • description: (optional) The description of the type
self::$service->addtype('Product',
    [
        self::$service->param('idProduct', 'int',false,true, 'comentary'),
        self::$service->param('name', 'string'),
        self::$service->param('price', 'int')
    ]);

Example: Defining a complex type of an invoice with the invoice detail.

self::$service->addtype('InvoiceDetail', [
    self::$service->param('idInvoiceDetail', 'int'),
    self::$service->param('idInvoice', 'int'),
    self::$service->param('detail', 'string')
]);
self::$service->addtype('Invoice', [
    self::$service->param('idInvoice', 'int'),
    self::$service->paramList('details','InvoiceDetail'),
]);

Versions

  • 3.0 * Rebuild the engine. Now SOAP and JSON works correctly.
  • 2.6 * Fixed the client when it returns an array of objects.
  • 2.5 * xml serialization changed
  • 2.4.1 A fix with the composer.json
  • 2.4 A new version
  Files folder image Files  
File Role Description
Files folder imagedocs (10 files)
Files folder imageexamples (3 files, 2 directories)
Files folder imagelib (2 files)
Files folder imagetest (3 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file license.txt Doc. Documentation
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  docs  
File Role Description
  Accessible without login Image file generation.jpg Icon Icon image
  Accessible without login Image file helloservice.jpg Data Auxiliary data
  Accessible without login Image file helloservice2.jpg Data Auxiliary data
  Accessible without login Image file helloservice3.jpg Data Auxiliary data
  Accessible without login Image file helloservice4.jpg Data Auxiliary data
  Accessible without login Image file hellosoap.jpg Data Auxiliary data
  Accessible without login Image file hellosoap2.jpg Data Auxiliary data
  Accessible without login Image file hellosoap3.jpg Data Auxiliary data
  Accessible without login Image file screenshot1.jpg Data Auxiliary data
  Accessible without login Image file soap1.jpg Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
Files folder imageservice (3 files)
Files folder imagestartup_example (3 files, 1 directory)
  Accessible without login Plain text file clienteexample.php Example Example script
  Plain text file Definition.php Class Class source
  Accessible without login Plain text file Server.php Example Example script

  Files folder image Files  /  examples  /  service  
File Role Description
  Plain text file Example2WSClient.php Class Class source
  Plain text file Example2WSService.php Class Class source
  Plain text file IExample2WSService.php Class Class source

  Files folder image Files  /  examples  /  startup_example  
File Role Description
Files folder imageservice (2 files)
  Plain text file ExampleDefinition.php Class Class source
  Accessible without login Plain text file run_initial.php Example Example script
  Accessible without login Plain text file run_with_service.php Example Example script

  Files folder image Files  /  examples  /  startup_example  /  service  
File Role Description
  Plain text file ExampleHelloService.php Class Class source
  Plain text file IExampleHelloService.php Class Class source

  Files folder image Files  /  lib  
File Role Description
  Plain text file CloudKing.php Class Class source
  Plain text file CloudKingClient.php Class Class source

  Files folder image Files  /  test  
File Role Description
  Plain text file AbstractCloudKingTestCase.php Class Class source
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file CloudKingTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:56
This week:0
All time:9,902
This week:342Up

For more information send a message to info at phpclasses dot org.