PHP Web Service Development
Wso2 WSF/PHP (wso2 Web Services Framework/PHP, wso2 web service framework) is a PHP extension that allows you to create and use web services. It supports soap1.1, soap1.2, MTOM, Web service addressing, Web Service Security, and rest-style calls. The latest wso2 WSF/PHP version (v2.0.0) has just been released.
The following is a brief guide explaining how to use the wso2 WSF/PHP extension to create a simple calculator service.
(Assume that the Apache HTTP server has been installed on your machine and you are familiar with running PHP scripts on the Apache server)
Step 1: Install wso2 WSF/PHP Extension
In ubuntu, perform the following steps:
1. APT-Get install PhP5
2. APT-Get install php5-dev
3. APT-Get libapache2-mod-php5
3. APT-Get install libxml2
4. APT-Get install libxml2-dev
5. Download WSF/PHP v2.0.0 and decompress it to a directory.
6. Access the Directory through the command line and run the following command as "root:
./Configure
Make
Make install
7./etc/init. d/apache2 restart
Step 2: Compile the calculator Service
Create a script named calculatorservice. php and put it into the web root (usually/var/www) of the Apache HTTP Server ).
<? PHP
Function calculate ($ inmessage ){
$ Simplexml = new simplexmlelement ($ inmessage-> Str );
$ Operand1 = $ simplexml-> param1 [0];
$ Operand2 = $ simplexml-> param2 [0];
$ Operation = $ simplexml-> param3 [0];
If ($ operation! = NULL)
{
Switch ($ Operation)
{
Case "add": $ result = $ operand1 + $ operand2; break;
Case "sub": $ result = $ operand1-$ operand2; break;
Case "Mul": $ result = $ operand1 * $ operand2; break;
Case "Div": $ result = $ operand1/$ operand2; break;
}
}
$ Response = <XML
<Result> $ result </result>
XML;
$ Returnmsg = new wsmessage ($ response );
Return $ returnmsg;
}
$ Service = new wsservice (Array ("operations" => array ("Calculate ")));
$ Service-> reply ();
?>
Once deployed, you can access it from http: // localhost: <port>/calculatorservice. php.
Step 3: Compile the calculator Client
Write a client, call this calculator service, and print the result.
The script is named calculatorclient. php and put into the web root of the Apache HTTP Server.
Do not forget to change the Apache server port (http: // localhost: 81/calculatorservice. php) to match the server.
<? PHP
$ Requestpayload = <XML
<Calculate>
<Param1> 100 </param1>
<Param2> 43 </param2>
<Param3> Add </param3>
</Calculate>
XML;
Try {
$ Message = new wsmessage ($ requestpayload,
Array ("to" => "http: // localhost: 81/calculatorservice. php "));
$ Client = new wsclient ();
$ Response = $ client-> request ($ message );
Echo "Answer: $ response-> Str ";
}
Catch (exception $ e ){
If ($ e instanceof wsfault ){
$ Fault = $ E;
Printf ("Soap fault received. Code: '% s'. Reason:' % s'/N ",
$ Fault-> code, $ fault-> reason );
} Else {
Printf ("exception occurred. Message: '% s'/N", $ e-> getmessage ());
}
}
?>
Step 4: access the service
Run calculatorclient. php to access the service, as shown below:
Http: // localhost: <port>/calculatorservice. php
The above is just a basic example of creating a calculator service.