Skip to content

crayfishuk/webservice-helper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Webservice helper
=================

This is a fork of Webservice helper from David Kingma.
Original sources come from www.jool.nl/webservicehelper/.
Original author : David Kingma david<AT>jool<DOT>nl

1. What does the webservice helper?
2. Manual
3. FAQ
4. Example


1. What does the webservice helper?
-----------------------------------

The webservice helper does what the name says: helping you making a php class 
available as webservice. It generates the documentation, the webservice
description file (WSDL) and handles errorhandling. It consists of three parts: 
* extension of the PHP reflection classes to also parse the comments for
  information on parameter info and return values. The documentation and WSDL
  are generated from these classes.(see also documentation.php as an example)
* extension to the PHP SOAP implementation. It catches all normal exceptions
  and allows typehints in the webservice methods. (ie. saveContact(contact $contact))

2. Manual
----------

So how do you create your own webservice. As an example we create a webservice to
add and show contacts. First you create a class called contactManager in the 
/lib/data_objects with the public functions getContacts(), saveContact(contact 
$contact) and newContact(). To let the Webservice helper know what the parameters 
and return values of each method are we put a comment in front of each method 
specifying the parameters and return types. For example:

/**
 * This method saves the given contact
 * @return contact[] Array with all the contacts
 */
 public function getContacts(){}

/**
 * This method saves the given contact
 * @param contact The contact to save
 * @return void
 */
 public function saveContact(contact $contact){}

/**
 * This method saves the given contact
 * @return contact A new contact template
 */
 public function newContact(){}

/**
 * Gets the current contact list as associative array
 * @return contact[=>]  keys are contact name
 */
public function	getContactsAsAssoc() {


We used the contact type as a return value for newContact() and getContacts() so we 
need to define what a contact looks like. For that we create a class called contact:

class contact{
	/** @var string */
	public $name;
	/** @var string */
	public $address;
}

Since string is (just as boolean and int) a known datatype we don't need to specify it
any further.

The last thing we need to do to finish our webservice is to tell the webservice that de 
contactManager class is an allowed webservice and that contact is an allowed data-
structure (for documentation purpose and classmap). In the config.php you add
"contactmanager" to the WSClasses array and add "contact" to the WSStructures array. 

You can now view the service documentation at /doc/documentation.php and the wsdl at 
/example/service.php?class=contactManager&wsdl

Note about associative array containing objects: if the soap server defines a classmap
('classmap' option to SoapServer in PHP), objects are wrapped into SoapVal objects, else
they are simple stdClass objects.


3. FAQ
-------

* My function doesn't showup in the documentation nor the WSDL file?
Please check if it's a public function and it doesn't start with '__'

* It doesn't work!
    - Do you see any warnings in the generated documentation? Fix them
    - Check case sensitivity of class names
    - Did you check the javaconsole to see if anything goes wrong?
    - Tried cleaning the wsdl cache in the WSDL cache directory?
    - Did you check the WSDL url in the client?

* Can I use the webservice helper in my own project?
Yes you can use it under the terms of LGPL 2.1 and send me mail about the project
and with any codechanges.


4. example and unit tests
-------------------------

To run the example and launch unit tests, you have to setup a web server http://localhost/ws/
where ws/ points to the root directory (the directory where this README is). You should
also to setup write access to your web server on the example/wsdl directory.

For example, on debian/ubuntu, with Apache:

```
sudo ln -s /var/www/html/ws `pwd`
sudo chown :www-data example/wsdl
chmod g+w example/wsdl
```

You can then load in your browser:

```
http://localhost/ws/example/service.php?class=contactManager
```

To run tests, go into the tests directory, and launch phpunit

```
cd tests
../vendor/bin/phpunit
```

If you made a change in the code and want to relaunch tests (which is a good idea ;-)),
you must be sure that WSDL cache is clear.

```
sudo rm -rf /tmp/wsdl-*
sudo rm -f ../example/wsdl/contactManager.wsdl
```

About

PHP Library to generate WSDL content from classes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%