Wednesday, 20 May 2009

Quickstart web services with SOAP and Zend Framework

Web services are software systems designed to support interoperable machine-to-machine interaction over a network. Nowadays if you want to connect external systems, you probably want or have to use web services. What I will discuss here is how to get your own SOAP web service up in minutes.

SOAP(Simple Object Access Protocol ) is probably the most used web service protocol today. It relies on XML as its message format, and it uses HTTP for message transmission. The SOAP server uses WSDL(Web Services Description Language ) to describe its services to external clients. WSDL is simply an XML-based language that provides a model for describing Web services.

Back in the old days you had to know a lot about SOAP and WSDL create a web service. Have a look at http://www.php.net/soap to see what I mean. Definitely not very good looking. Luckily Zend Framework has a nice component, Zend_Soap, that handles all the SOAP hard work you would be supposed to do.

So without further ado, here's the code(discussing a Zend Framework component, the code presented here uses the Zend MVC, but you can use it without the Zend MVC):

This is the source code for the controller:
require_once realpath(APPLICATION_PATH .
'/../library/').'/Soaptest.php';


class SoapController extends Zend_Controller_Action
{
//change this to your WSDL URI!
private
$_WSDL_URI="http://192.168.188.128:8081/soap?wsdl";


public function indexAction()
{
$this->_helper->viewRenderer->setNoRender();

if(isset($_GET['wsdl'])) {
//return the WSDL
$this->hadleWSDL();
} else {
//handle SOAP request
$this->handleSOAP();
}
}

private function hadleWSDL() {
$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->setClass('Soaptest');
$autodiscover->handle();
}

private function handleSOAP() {
$soap = new Zend_Soap_Server($this->_WSDL_URI);
$soap->setClass('Soaptest');
$soap->handle();
}

public function clientAction() {
$client = new Zend_Soap_Client($this->_WSDL_URI);

$this->view->add_result = $client->math_add(11, 55);
$this->view->not_result = $client->logical_not(true);
$this->view->sort_result = $client->simple_sort(
array("d" => "lemon", "a" => "orange",
"b" => "banana", "c" => "apple"));


}

}

And the code for the Soaptest.php class:

class Soaptest {
/**
* Add method
*
* @param Int $param1
* @param Int $param2
* @return Int
*/
public function math_add($param1, $param2) {
return $param1+$param2;
}

/**
* Logical not method
*
* @param boolean $param1
* @return boolean
*/
public function logical_not($param1) {
return !$param1;
}

/**
* Simple array sort
*
* @param Array $array
* @return Array
*/
public function simple_sort($array) {
asort($array);
return $array;
}

}

You can also download the full project here.

As you can see you don't have to write a lot of code to back up the web service.

Let's discuss the controller first, because there's where the “magic” happens. The index action handles two types of requests: the request for the WSDL, handled by the hadleWSDL() method and the actual SOAP request, handled by the handleSOAP() method.

You can go ahead and try to see how your WSDL looks by accessing http://URL_TO_WEB_SERVICE/soap?wsdl , where URL_TO_WEB_SERVICE is the URL where you have deployed the example. Now imagine that you would have to construct and maintain this yourself, by hand, as old school bearded guys would. Well you don't, because this is handled by Zend_Soap_AutoDiscover which will create the WSDL file for you. The only thing that Zend_Soap_AutoDiscover needs to know is the class you want to use for the web service. Also, because PHP is not strongly typed, you will have to put PHPDoc blocks, because SOAP needs to know what types you are using as parameters and what types you are returning. Have a look here if PHPDoc does not ring a bell .

The SOAP server is handled by the Zend_Soap_Server class, and all it needs is the class you intend to use for the web service, and the URI to your WSDL file. Remember when you checked out how the WSDL file looks? That's exactly the URI you will have to use. In the example you will have to put that into the $_WSDL_URI variable, defined in the SoapController.

That was the SOAP server. Simple, right? Now let's have some tests on the server by implementing a simple SOAP client. The client is handled by the Zend_Soap_Client class that is constructed in the same manner as the server class, it needs just the URI to the WSDL file. After you have constructed the client, you can access the methods defined by the SOAP server in the same way you would access the methods of an object. In the example above you have a simple class, called Soaptest, that defines three very simple methods. Feel free to change the class and test your own methods. While you are playing with the server, you might notice that the WSDL file is cached, so if you change something into the Soaptest.php file, you might not get the expected result. Just delete the cached WSDL file from /tmp/wsdl-* while you do your tests.

You definitely want to have a look at the Zend Framework documentation located here.

That was it, as promissed: your SOAP web service up in minutes.

Tuesday, 5 May 2009

Create your perfect virtualised PHP development environment

The development environment is one of the most important factors to be taken into consideration for successful projects. The environment has three basic elements: the operating system, the text editor and the test environment. The operating system is not really important as long as you're used to it, and this also applies to the text editor. The test environment is something else, because you want to avoid “surprises” when your code goes on the production or company test environment. Your test environment should be very similar, if not identical to the production environment. Unfortunately a lot of organisations have rules over rules that stop you from having your perfect environment. Those of you who are allowed to develop on Linux, consider yourself lucky, because most of the companies will provide a Windows/MacOS environment.

How are you supposed to have a PHP test environment under Windows/MaxOS that is similar to your production Linux? The answer is quite simple: virtualisation. You can have a Linux virtual machine that runs your test environment, and the best part is that you have total control of the test environment, with root access. If that sounds good, read on, and follow these easy steps:
  • Start by downloading VMware Server for your operating system from http://www.vmware.com/products/server/ . You have to register, but the product is free.
  • Install VMWare Server on your machine
  • Go to the VMware Infrastructure Web Access, located at http://127.0.0.1:8222/ui/ , and login with your operating system credentials. If you have problems with that, check out this post
  • Next, you have to create the virtual machine. In the Vmware Infrastructure Web Access main page, go to Virtual Machine->Create Virtual Machine.
  • In the “Name and location” screen, just give a name to your virtual machine, and select the standard datastore. Click next after each step to move forward.
  • In the “Guest and Operating System” screen, you have to choose the right operating system you intend to install. For Linux, select “Linux operating system”, and the version you want.
  • In the “Memory as Processors” screen, it depends on your machine how much you want to use for your virtual machine. I would recommend at least 512MB for a simple Linux installation. If you're not sure, start with 512MB, and you can increase the memory later if you need to. The default setting for Processors should be ok, usually 1 processor is fine for testing.
  • In the “Hard Disk” screen, select “Create a new virtual disk” and input a proper size for your Linux installation. Disk space is really cheap these days, so put enough space.
  • In the “Network adapter” screen, you definitely want to add networking capabilities to your virtual machine. Select “Add a network adapter”, and choose from the three possible connections. I would recommend bridged, so that your virtual machine will be visible not only from your computer, but from your network.
  • In the “CD/DVD drive” screen, select “Use a physical drive” if you have your Linux distribution written on a CD/DVD of “Use an ISO image” if you have just the .iso file.
  • Don't add no floppy drive on the “Floppy drive” screen, unless you have a good reason to add one.
  • If you need USB, add an USB controller from the “USB controller” screen.
  • After completing all these steps, the virtual machine is ready to be created, click “Finish” to do that. Wait for the machine to be created, you can see the progress at the bottom of the screen. You have your virtual machine now, next you have to install the operating system.
  • Select your newly created virtual machine from the left side of the screen and go to the “Console” tab. In order for you to be able to access the virtual machine, you will need to install a browser plugin that allows you to see the virtual operating system inside your browser. Click on the install plugin link, you will get the plugin installed, and the browser will restart. Log in again to the VMware Infrastructure Web Access (http://127.0.0.1:8222/ui/ ) and go to the Console tab of your virtual machine.
  • Now you can start the machine by clicking in the middle of the console. You will have to click again the “Open console in new window” link to open the console.
  • Finally you can see the console. Depending on what you have selected on the “CD/DVD drive” screen, your Linux installation will start automatically if you have selected an existing iso image, or you will have to provide the CD/DVD for the installation to start. From this point it is up to you what you install and how you install it. Try to avoid installing graphical Linux, because it's pointless and it will consume a lot of memory.
After installing Linux, if you want to quickly install PHP and Apache, you might have a look at Zend Server Community Edition. I have detailed in my last post the installation, you might want to have a look(http://bogdan-albei.blogspot.com/2009/05/zend-server-community-edition.html).

Now you have your own Linux machine inside your Windows and you can tune it as closely as possible to your production server. It's not necessary to access your machine through the console. As long as it is started you can access it with your favourite SSH client.

Don't forget that you will have to start the virtual machine every time you start your operating system.

Friday, 1 May 2009

Zend Server Community Edition setup(Fedora Linux)

Problem: Zend has published documentation on how to install the full version of Zend Server. Some might want the Community Edition, which is free, and has no license warnings and extra modules loaded.

Solution: (this works under Fedora Linux, I'm sure that simmilar solutions can be applied to other distributions).

1. Create /etc/yum.repos.d/zend.repo with the following content:
[Zend]
name=Zend CE $releasever - $basearch - Released Updates
baseurl=http://repos.zend.com/rpm/ce/$basearch/
enabled=1
gpgcheck=0

[Zendce-noarch]
name=Zend CE - noarch
baseurl=http://repos.zend.com/rpm/ce/noarch
enabled=1
gpgcheck=0

2. If you have installed the full version, remove it:
/usr/local/zend/bin/zendctl.sh stop
yum remove *zend*
rm -rf /usr/local/zend

3. Install Zend Server CE:
yum install zend-ce

You can now go on the Zend Server admin page, which is http://127.0.0.1:10081/ZendServer/ on my server. Unfortunately I had to discover this myself, bacause I've seen no message after the installation as the documentation specifies.