Thursday, January 10, 2008

Detecting the OS using PHP

Sometimes it is necessary to detect the operating system on which your php code is running. PHP has number of pre defined variables and PHP_OS is one such variable which is an string containing the operating system name.
So following code checks whether the system is running on windows or not.

if (stristr(PHP_OS, 'WIN')) {
echo "Win32";
}

Sending a custom soap header to a Web Service

Sometimes Web Services require sending information through soap headers.It is quite simple and easy to send a custom soap header using WSF/PHP.

Consider we want to send a soap header "Transaction" with like in the following soap envelope.

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header>
<t:Transaction soapenv:mustUnderstand="1" xmlns:t="Some-URI">5</t:Transaction>
</soapenv:Header>
<soapenv:Body>
<ns1:Greet xmlns:ns1="http://php.axis2.org/samples">
<text>Hello</text>
</ns1:Greet>
</soapenv:Body>
</soapenv:Envelope>

First we will create a WSHeader object to produce this header.

$header = WSHeader("ns"=>"some-URI",
"prefix"=>"t",
"name"=>"Transaction",
"data"=>"5",
"mustUnderstand"=>"true");

Now lets create a WSMessage object and set the header to the inputHeaders array.

$msg = new WSMessage($requestPayloadString,
array("inputHeaders" => array($header)));

Note that $requestPayloadString holds the payload string that will be sent to the service.


Following is the complete source code.

$requestPayloadString = <<<XML
<ns1:Greet xmlns:ns1="http://php.axis2.org/samples">
<text>Hello</text>
</ns1:Greet>
XML;

try {

$client = new WSClient(array ("to"=>"http://localhost/samples/greet_service.php"));


$header = WSHeader("ns"=>"some-URI",
"prefix"=>"t",
"name"=>"Transaction",
"data"=>"5");

$msg = new WSMessage($requestPayloadString ,
array("inputHeaders" => array($header)));

$client->request($msg);

echo "\n\n Received message \n";
echo htmlspecialchars($recvMsg);

} catch (Exception $e) {
if ($e instanceof WSFault) {
printf("Soap Fault: %s\n", $e->Reason);
} else {
printf("Message = %s\n",$e->getMessage());
}
}

Wednesday, January 9, 2008

Deploying PHP Web Services using XAMPP for windows

XAMPP is one of the most popular Apache destribution containing Apache, MySql , PHP and PERL. Now it is possible to deploy web services on XAMPP using WSF/PHP extension.

All you need to do is to download WSF/PHP 1.2.0 Win32 Binary release and do the following configuration steps.

1. Unzip the binary package. You will get the directory C:\wso2-wsf-php-bin-1.2.0-win32.
Add C:\wso2-wsf-php-bin-1.2.0-win32\lib directory to PATH environment variable.
( assuming that you unpacked the binary zip to C drive )

2. Copy wsf.dll located in wso2-wsf-php-bin-1.2.0-win32 directory to "C:\xampp\php\ext".
(assuming that you have xampp installed on C drive).

3. Edit the php.ini file located in C:\xampp\apache\bin directory and add following php.ini entries.

extension=wsf.dll

wsf.home=C:\wso2-wsf-php-bin-1.2.0-win32\wsf_c
wsf.log_path=C:\wso2-wsf-php-bin-1.2.0-win32\wsf_c\logs
wsf.log_level=3
wsf.rm_db_dir=C:\wso2-wsf-php-bin-1.2.0-win32\wsf_c

Append C:\wso2-wsf-php-bin-1.2.0-win32\scripts to include_path in php.ini as follows.

include_path = ".;\xampp\php\pear\;C:\wso2-wsf-php-bin-1.2.0-win32\scripts\"

Thats it. Now you are ready to try out the samples. So copy the samples directory (C:\wso2-wsf-php-bin-1.2.0-win32\samples) to C:\xampp\htdocs directory.

Now you should be able to run the samples.

Now you can write your own web services and clients using WSF/PHP.

Tuesday, January 8, 2008

Using Pecl command to install WSF/PHP

With WSO2 WSF/PHP 1.2.0 release, Now you are able to use pecl tool for installing WSF/PHP extension. We built this package for linux platform. I am sure that all those php developers on linux platfrom interested in using WSF/PHP will welcome this.

Following are the steps needed to install WSF/PHP on linux.

1. Download WSF/C Framework from here.
http://dist.wso2.org/products/wsf/c/wso2-wsf-c-src-1.2.0.tar.gz
2. Installing wso2-wsf-c is very simple.

tar xf wso2-wsf-c-src-1.2.0.tar.gz
cd wso2-wsf-c-src-1.2.0
./configure
./make
/make install

This will install WSO2 WSF/C Web Services Framework will be installed to /opt/wso2
directory. You will need super user previlages to install it to /opt directory.

3. Now use pecl tool to install the WSF/PHP.
pecl install http://dist.wso2.org/products/wsf/php/wso2_wsf_php-1.2.0.tgz

Now pecl tool will download and install WSF/PHP extension. It will copy the docs folder to /usr/local/lib/php/doc/wso2_wsf_php. samples and scripts folders will be installed to /usr/local/lib/php directory.

Copy the samples folder to your apache2 webserver's document root.

4. Add the following entries to php.ini file.

extension=wsf.so
wsf.home = /opt/wso2/wsf_c
log_level = 3

Uncomment the include_path entry and edit it to point to the location for scripts folder as include_path = ".:/usr/local/lib/php/wso2_wsf_php/scripts"

Please note that you need to have php_xsl extension installed on your system to run the wsdl mode samples.

Also you can check the the project home page.

Monday, January 7, 2008

WSO2 Web Services Framework for PHP 1.2.0 Released

Almost after 3 months from the previous release, we are shiping the latest WSF/PHP release with version numbers 1.2.0. It carries substantial improvement over the previous release of WSF/PHP. The WSDL support has been improved and it now support WS-Security and WS-Addressing. So if you are to write a web service client or service in PHP with the intension of consuming or providing a web service that uses WS-Security, WS-Addressing, then your life will become significantly easy with this new release.In addition, a lot of imporvements and bug fixes have been done.
This release includes binary packages for zend core as well. As usuall packages were released for fedora, ubuntu, and debian in addition to windows. So what ever the platform you are on, you have a chance of trying out this release with very few steps.


Following are the key features in this new 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

All of the people there are most welcomed to checkout this release.
You can download the release from http://wso2.org/downloads/wsf/php

Of course if you run in to any problems, you are most welcome to contact us
at WSF/PHP forum. http://wso2.org/forum/188

Monday, October 15, 2007

Building WSF/PHP From source

WSF/PHP is an extension for PHP that provide WS* web Services support for PHP. It is build on top of WSF/C Framework. Since there was number of questions on the forum on how to build WSF/C from source on Win32 Platfrom I thought of writing the steps down.

First you need to download the dependency libraries needed. Here is a list of them.

1. Libxml2 (http://www.zlatkovic.com/pub/libxml/)

2. Iconv (http://www.zlatkovic.com/pub/libxml/)

3. zlib (http://www.zlatkovic.com/pub/libxml/)

4. PHP source and binary ( www.php.net )

5. PHP build utilities (http://www.php.net/extra/win32build.zip)

6. WSO2-WSF/PHP extension source. (http://dist.wso2.org/products/wsf/php/wso2-wsf-php-src-1.1.0.zip)

Now you are ready to build WSO2 WSF/PHP extension from source.

Unzip the wso2-wsf-php-src-1.1.0.zip file to your prefered location. I use E:\wso2-wsf-php-src-1.1.0 as an example. In this folder you will find a configure.in file which is used to specify the configuration parameters to build WSF/PHP.

In the configure.in file you will find entries for specifing the locations of the
WSF/PHP dependencies. Here is the configure.in file shiped with WSF/PHP.

###################################
### General Build Configuration Parameters

# To build with debug symbols set DEBUG = 1
DEBUG = 1

###################################
### Apache Axis2/C Build Configuration Parameters

# Path to libxml2 installation
LIBXML2_BIN_DIR = E:\libxml2-2.6.27.win32

# Path to iconv installation
ICONV_BIN_DIR = E:\iconv-1.9.2.win32

# Path to zlib installation
ZLIB_BIN_DIR=E:\zlib-1.2.3.win32

# Path to Apache2 installation
APACHE_BIN_DIR = "E:\Apache22"
# Apache httpd version, to use apache 2.0.x set APACHE_VERSION_2_0_X = 1
APACHE_VERSION_2_0_X = 0

# Enable/disable LibCurl client transport, to enable set ENABLE_LIBCURL = 1
ENABLE_LIBCURL = 0
# Path to LibCurl installation, required if LibCurl transport is enabled
#LIBCURL_BIN_DIR = E:\libcurl-7.15.1-msvc-win32-ssl-0.9.8a-zlib-1.2.3

# Enable/disable SSL client transport, to enable set ENABLE_SSL = 1
ENABLE_SSL = 1
# Path to OpenSSL installation, required if SSL transport is enabled
#OPENSSL_BIN_DIR = D:\OpenSSL

# Enable/disable Guththila parser, to enable set ENABLE_GUTHTHILA = 1
ENABLE_GUTHTHILA = 0


############################
### Apache Rampart/C Build Configuration Parameters

# Path to OpenSSL installation
OPENSSL_BIN_DIR = E:\OpenSSL


############################
### Apache Sandesha2/C Build Configuration Parameters

# Build with SQLite support for permanent storage, to disable set WITH_SQLITE = 0
WITH_SQLITE = 1
# Path to SQLite installation
SQLITE_BIN_DIR = "E:\sqlite-source-3_3_8"
# Path to SQLite source
SQLITE_SRC_DIR = "E:\sqlite-source-3_3_8"

# Build with MySQL support for permanent storage, to enable set WITH_MYSQL = 1
WITH_MYSQL = 0
# Path to MySQL installation
MYSQL_BIN_DIR = "C:\Program Files\MySQL\MySQL Server 5.0"

############################
# Configuration for building wsf/php extension using its own build
PHP_SRC_DIR = E:\php\php-5.2.4
PHP_BIN_DIR = E:\php\php-5.2.4-Win32
WIN32BUILD_DIR = E:\php\win32buil

As you can see , specifing the Dependencies is very simple.
Next step is to run the build.bat file located in the wso2-wsf-php-src-1.1.0 directory. It will build the binary distribution to a folder named wso2-wsf-php-bin-1.1.0-win32 directory.

Thats it.

Thursday, August 2, 2007

WSO2 WSF/PHP v1.0.0 Released

After almost one year of development, We are finally releasing the 1.0.0 version of WSO2 WSF/PHP.
WSO2 1.0.0 version has following features.

1. Client API to consume Web Services.
* WSMessage class to handle message levell 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. Attachment support [ MTOM / Base64 ]
* Binary Optiomized
* 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 requests

8. REST Support
* Expose a single service script both as SOAP and REST service

9. WSDL mode support for both client and server side
* Write services and client based on a given WSDL

10. Backward compatibility with PHP5 SOAP extension
* Experimental


You can download the realse from
http://dist.wso2.org/products/wsf/php/1.0.0/

Project home page:
http://wso2.org/projects/wsf/php