PHP Web Services with WSDL Tutorial

Wednesday, March 26, 2008

Were you finding a starters tutorial for PHP Web Services with WSDL?. I wrote one for wso2 Oxygen Tank. This is based on the WSO2 WSF/PHP release 1.2.0. But it will be working for newer releases like 1.2.1 without problem. Hope you will be find this really useful.

WS-Security with WSDL mode

Tuesday, March 25, 2008

If you ever wanted to use ws-security with WSDL mode, you can check this demo. Observe closely how CalendarService.php and CalendarClient.php is implemented from Calendar.wsdl, this gives you a good idea how you can implement both client and server sides with ws-security in wsdl mode.

Data Services in PHP

The PHP Web services demo site now has a cool Data Services demo. This demo shows how you can expose a database table as a Web service with PHP. You can also sneak a peek into the PHP source to find out how to do that, have a look at the right hand side pane.

If you have any database that you want to expose as either SOAP or a REST service, now you can follow few steps and get the job done.

WSF/PHP team is in the process of enhancing the level of data services support. They are implementing a comprehensive set of classes on top of WSF/PHP to make your data services dreams come true.

WSO2-WSF-PHP 64-bit rpm available

Monday, March 24, 2008

One of our users (Jan-Frode) has build WSO2-WSF-PHP RPM for a 64 bit RadHat Linux platform. He has hosted here. Thanks Jan for your contribution.

Install WSF/PHP 1.2.1 with zend core in Ubuntu 7.10

Monday, March 17, 2008

1. Download the zend core for your linux, using the following link http://www.zend.com/en/products/core/downloads

2. unzip the installation and run the install script, I used the following command to install it,
sudo ./install -n

3. Now download the relevant wso2 wsf/php deb for the zend core for your os,
from http://dist.wso2.org/products/wsf/php/1.2.1/wso2-wsf-php-1.2.1-ubuntu-7.10-zc.deb

4. You can install the wso2/wsf php by running
dpkg -i wso2-wsf-php-1.2.1-ubuntu-7.10-zc.deb

4. you have to enable xsl extension for php manually editing the php.ini, /usr/local/Zend/Core/etc/php.ini
add the following line to the php.ini,

extension=xsl.so

5. And add the the /usr/local/Zend/Core/lib/php/20060613/wsf_php/scripts directory to the include_path directive of the php.ini

include_path= ".:/usr/local/Zend/Core/lib/php/20060613/wsf_php/scripts"

6. Then copy the /usr/local/Zend/Core/lib/php/20060613/wsf_php/samples directory to the /usr/local/Zend/apache2/htdocs

sudo cp /usr/local/Zend/Core/lib/php/20060613/wsf_php/samples /usr/local/Zend/apache2/htdocs

7. Restart the apache,
/usr/local/Zed/apache2/bin/apachctl restart.

8. Again that's all, open the browser and type http://localhost/samples and click on each link for samples.

9. You have successfully install the wsf/php with Zend Core in Ubuntu 7.10.

Installing WSF/PHP 1.2.1 in ubuntu

Here are the commands I ran to install WSF/PHP 1.2.1 on a fresh machine (Just after installing Ubuntu 7.10,

1. First update the apt sources list, so we can install all the required packages using apt-get install, Here is a guide to update the apt repository.
http://ubuntuguide.org/wiki/Ubuntu:Gutsy#Updates_and_Upgrades_and_Installing_Software
There you will uncomment the repository list in the /etc/apt/sources.list and run
apt-get update both as the root. (remember to create a backup of the file before editing, so anything go wrong, you have the original)


2. Install PHP by typing

sudo apt-get install php5

3. Install PHP, XSL extension,

sudo apt-get install php5-xsl

4. Install Apache2

sudo apt-get install apache2

5. Then download the wsf/php version 1.2.1 ubuntu 7.10 deb file. Make sure to filter out the correct deb file for your OS. wso2-wsf-php-1.2.1-ubuntu-7.10.deb

6. Install the wso2-wsf-php-1.2.1-ubuntu-7.10.deb by running the following command,

dpkg -i wso2-wsf-php-1.2.1-ubuntu-7.10.deb

7. Put the /usr/lib/php5/20060613+lfs/wsf_php/scripts to the include_path directive of the php.ini. Open the /etc/php5/apache2/php.ini and put the following line,

include_path= ".:/usr/lib/php5/20060613+lfs/wsf_php/scripts"

8. Copy the /usr/lib/php5/20060613+lfs/wsf_php/samples directory to the /var/www

sudo cp -R /usr/lib/php5/20060613+lfs/wsf_php/samples /var/www

9. Hm, that's all I did, then I opened the browser and type http://localhost/samples

10. Just click on each link for samples, and make sure they are working,

If you are using Ubuntu 7.04, You can still follow the same path, but make sure you download the correct WSO2 WSF/PHP package.

WSO2 WSF/PHP 1.2.1 Released

Sunday, March 16, 2008

WSF/PHP Team is pleased to announce the release of WSF/PHP 1.2.1 release. We focused on improving contract first web services support for this release.Schema types support has been improved in this release. All of the current web services stacks for PHP requires the users to go through the WSDL file and pick up the data types and construct his clients and services accordingly. This is quite a cumbersome task. To make this task easier for the user, WSF/PHP 1.2.1 release includes wsdl2php script which can generate the clients and services for a given WSDL. This is an experimental feature that is still it the testing stage. But we hope that is will make the task of implementing Clients and Services easier than ever before.

Another major problem that was fixed in this release was the large attachment support problem. This was an issue that had propagated in to WSF/PHP from WSF/C. Since the changes in WSF/C attachment handling code, the attachments sending and receiving has been made much more efficient. In addition we were able to fix some documentation errors.  Following is the complete feature list for this release.

1. Client API to consume Web services
      * WSMessage class to handle message level options
      * WSClient class with both one way and two way service invocation support
      * Option of using functions in place of object oriented API with ws_request

2. Service API to provide Web services
      * WSMessage class to handle message level options
      * WSService class with support for both one way and two way operations
      * Option of using functions in place of object oriented API with ws_reply

3. Attachments with MTOM
      * Binary optimized
      * Non-optimized (Base64 binary)

4. WS-Addressing
      * Version 1.0
      * Submission

5. WS-Security
      * UsernameToken and Timestamp
      * Encryption
      * Signing
      * WS-SecurityPolicy based configuration

6. WS-Reliable Messaging
      * Single channel two way reliable messaging

7. WSDL Generation for Server Side
      * WSDL generation based on annotations and function signatures, and
        serving on ?wsdl or ?wsdl2 requests

8. WSDL mode support for both client and server side
      * Write services and client based on a given WSDL
      * WS-Addressing and WS-SecurityPolicy is supported in WSDL mode
9. REST Support
      * Expose a single service script both as SOAP and REST service

10. Provide easy to use classes for common services
      * Consume some well known services such as Yahoo search and Flickr
        and Amazon services using predefined classes

Experimental Features
---------------------

11. wsdl2php.php script. This script can generate PHP classes for services
    and clients for a given WSDL to be used with WSDL Mode .

We welcome you you try out this latest release. You can download it from here.

Also you can checkout the complete release note.

Data Services With WSF/PHP

Friday, March 14, 2008

Today we host an initial version of demo to illustrate how you can do Data Services with WSF/PHP. The demo is built top of an framework which can be used to expose your data in the database as a Web Service in easy steps. There you only need to specify the input and result format as xml and the specific SQL query.
Hope this will be really useful for people who were eager to see how Data Services can be done in PHP.

Calling https service with WSClient

Wednesday, March 12, 2008

Past few days I saw several users have raised the question, how to call https services from WSClient. In order to call such kind of services, you need to have the server's Certificate Authoritys Certificate (CACert), and specify it in the construction of WSClient,

$client = new WSClient(array("to" => "https://somehost.com/somewhere/service",
"CACert" => "cert.pem"));

If for some reason you don't have the servers CACert you can use the server's certificate itself as the CACert. In order to obtain the server's certificate use the following command, (assuming you already have openssl installed)

openssl s_client -connect somehost.com:443
Note: you should change the somehost.com:443 to the servers name and port.

Then extract out the text between "-----BEGIN CERTIFICATE-----" to "-----END CERTIFICATE-----"(inclusive of those two lines too) and save them in a file (cert.pem),

This way your server will be validated and will do the service call securely.

[ANN] WSO2 WSF/PHP Demo site Launched

Tuesday, March 11, 2008

WSO2 WSF/PHP team is pleased to announce the launch of WSO2 WSF/PHP Demo Site - http://labs.wso2.org/wsf/php

WSO2 WSF/PHP Demo site is a place where users can try out demonstrations powered by WSO2 Web Service Framework for PHP (WSF/PHP) online. In addition to that it gives the facility to study, view the source code and refer related articles, tutorials for each demo.

We invite the WSF/PHP community to login to the site and give feedback on demos by rating and commenting on them.

You can post suggestions and possible improvements to the demo site via WSO2 WSF/PHP mailing list wsf-php-user@wso2.org

Thanks for you interest in WSO2 WSF/PHP Demo site

-- WSO2 WSF/PHP Team --
http://wso2.org/projects/wsf/php

Simple Calculator - Demo of use of WSDL mode

Here are the steps I followed to create a simple Calculator Service + client.

1. Stated with the CalculatorDoc.wsdl found in here, http://svn.apache.org/repos/asf/webservices/axis2/trunk/c/test/resources/wsdl/CalculatorDoc.wsdl

2. Generated server side code using the wsdl2php.php script packed with the WSO2 WSF/PHP 1.2.1.

3. Run the wsdl2php.php script from command prompt with the following arguments.

/my/home/scripts/wsdl2php.php -s CalculatorDoc.wsdl

Here '-s' means for the server side.

4. I redirected output to the service.php in my linux system, so the command was like
/my/home/scripts/wsdl2php.php -s CalculatorDoc.wsdl > service.php
In a case this this doesn't work for you copy and paste the code to the service.php file.

5. Filled the adding/ subtraction/ multiplication/ division algorithm inside method bodies. There are helping comments which guide you what are the types of parameters and return value in the function, here is my way of doing it.

// define PHP functions that maps to WSDL operations 
function add($input) {
// TODO: fill in the business logic
// NOTE: $input is of type add


$a = $input->arg_0_0;
$b = $input->arg_1_0;


$c = $a + $b;
// NOTE: should return an object of type addResponse

$res = new addResponse();
$res->addReturn = $c;


return $res;
}


function sub($input) {

// TODO: fill in the business logic
// NOTE: $input is of type sub

$a = $input->arg_0_1;
$b = $input->arg_1_1;


$c = $a - $b;
// NOTE: should return an object of type subResponse

$res = new subResponse();
$res->subReturn = $c;


return $res;
}


function mul($input) {

// TODO: fill in the business logic
// NOTE: $input is of type mul

$a = $input->arg_0_2;
$b = $input->arg_1_2;


$c = $a * $b;
// NOTE: should return an object of type mulResponse

$res = new mulResponse();
$res->mulReturn = $c;


return $res;
}


function div($input) {

// TODO: fill in the business logic
// NOTE: $input is of type div

$a = $input->arg_0_3;
$b = $input->arg_1_3;


$c = $a / $b;
// NOTE: should return an object of type divResponse

$res = new divResponse();
$res->divReturn = $c;


return $res;
}


6. Now put the service.php in to your web root directory or some sub directory inside the web root and make sure it is correctly deployed by typing the location in the browser. For an example, I put my service.php inside calculator sub directory inside the web root, so I tested in the browser by typing http://localhost/calculator/service.php

Note: that this doesn't test the algorithm of the service, but just it make sulre the service and the operation set are correctly deployed.

7. So now you can write a client to test the service operation, You can generate a client with the following command,

/my/home/scripts/wsdl2php.php CalculatorDoc.wsdl

Note that the '-s' is dropped.

8. Go through the TODO comments and fill the client logic by filling sample input to test the service,

   // create input object and set values
$input = new add();
//TODO: fill in the class fields of $input to match your business logic


$input->arg_0_0 = 3;
$input->arg_1_0 = 4;

// call the operation

$response = $proxy->add($input);
//TODO: Implement business logic to consume $response, which is of type addResponse

echo "3 + 4 = {$response->addReturn}\n";



$input = new sub();
//TODO: fill in the class fields of $input to match your business logic

$input->arg_0_1 = 3;
$input->arg_1_1 = 4;


// call the operation
$response = $proxy->sub($input);
//TODO: Implement business logic to consume $response, which is of type subResponsee


echo "3 - 4 = {$response->subReturn}\n";

$input = new mul();
//TODO: fill in the class fields of $input to match your business logic


$input->arg_0_2 = 3;
$input->arg_1_2 = 4;


// call the operation
$response = $proxy->mul($input);
//TODO: Implement business logic to consume $response, which is of type mulResponsee


echo "3 * 4 = {$response->mulReturn}\n";

$input = new div();
//TODO: fill in the class fields of $input to match your business logic


$input->arg_0_3 = 3;
$input->arg_1_3 = 4;


// call the operation
$response = $proxy->div($input);
//TODO: Implement business logic to consume $response, which is of type divResponsee


echo "3 / 4 = {$response->divReturn}\n"


9. One more thing, The wsdl is coded with it s endpoint, but for you to test you need to change the endpoint to where you have actually test the service, for an example in my case to " http://localhost/calculator/service.php"
You can do the change in the wsdl. But there is an option in the WSClient constructor where you can give the service endpoint.
So my new WSClient constructor looks like this,
   // create client in WSDL mode
$client = new WSClient(array ("wsdl" =>"CalculatorDoc.wsdl",
"to" => "http://localhost/calculator/service.php",
"classmap" => $class_map));



10. That is all I did, And Finally run the client with the following command,
php client.php

And I received the expected output as following,
3 + 4 = 7
3 - 4 = -1
3 * 4 = 12
3 / 4 = 0.75

XML Schema Parsing in WSF/PHP

Sunday, March 9, 2008

WSF/PHP is currently famous for wide range of WS-* implementations like WS-Security, WS-Reliable Messaging. Plus it is the only choice that PHP programmers have when they have to work with optimized binary attachments, secured and reliable messaging.

Anyway when it comes to WSDL-mode, for the time being it supports only very common types of XML schema constructs. Anyway currently we are on the process of improving this, and surely WSF/PHP will be able to handle most of your WSDLs by the next major release.

PHP Amazon Client Demo

Thursday, March 6, 2008

This demo shows you how to consume the Amazon e-commerce SOAP API with PHP. It is a live demo, that you can view online and if you ever wonder how to do that in PHP, the source code is also available.

There is an associated tutorial too, that you can find in the above link. The tutorial explains step-by-step what you have to do to consume the Amazon API.

Search Mashup with PHP

Tuesday, March 4, 2008

There is an online demo that shows how you can build a simple Mashup with PHP Web services, using both SOAP and REST.

It uses Amazon, Flickr and Yahoo APIs.

This sample demo is built using WSF/PHP. You can also have a look online to see how it is done in PHP. Enjoy!

REST is CRUD

Saturday, March 1, 2008

REST is more close to CRUD. In WSF/PHP, we will make it more RESTful by implementing PUT and DELETE HTTP verbs. If you are wondering about the CRUD mapping:

PUT => create

GET => read

POST => update

DELETE => delete