---
title: "Java + PHP  in wso2"
author: "hellosoft"
date: "2010-06-26T11:21:30Z"
tags: []
categories: ["编程语言"]
slug: "java-php-in-wso2"
draft: false
lastmod: "2010-07-26T14:13:10Z"
id: 699762
url: "https://hellosoft.iteye.com/blog/699762"
---

Java + PHP in wso2


wso2提供了两个框架：WSFSpring, WSFPHP, 分别可以用于java和php.可以通过webservice，提供跨语言的交流。充分发挥各语言自己的优势。以下将描述，如何搭建这样的沟通渠道。

1. 服务端(Java)
需要安装JDK1.6+,  Tomcat 6.0+ , axis, 开发工具Eclipse
![](http://dl.iteye.com/upload/attachment/269152/35765b3e-68e6-3137-b7ae-65d9957824e9.jpg)
 

步骤：
1）	创建新项目：
File -> new -> Dynamic Web Project
项目名称 WSFSpringHelloWorld
2）	整理项目
a.	Copy lib下载wsf spring 的 bin 包,解压缩后，
复制 wsf-spring-1.5-bin\wsf-spring-1.5-bin\samples\webapp\wsf-spring-sample-1.5\WEB-INF\lib 目录到 WSFSpringHelloWorld 项目中的 WebContent\WEB-INF 下（如上图）
这个lib下已经包含的spring和 wsfspring了
b.	复制 axis2Config.xml
复制 wsf-spring-1.5-bin\wsf-spring-1.5-bin\samples\webapp\wsf-spring-sample-1.5\WEB-INF\ axis2Config.xml 到目录到 WSFSpringHelloWorld 项目中的 WebContent\WEB-INF 下（如上图）

c.	创建HelloWorld 类
代码如下：

```java

package com.hello;

public class HelloWorld {
	private String name;

	public String getName() {
	    return name;
	}

	public void setName(String name) {
	    this.name = name;
	}

	public String sayHello(String name) {
	    return name + " says Hello World!";
	}

}

```


d.	配置 applicationContext.xml
在 WSFSpringHelloWorld 项目中的 WebContent\WEB-INF 新建一个文件applicationContext.xml。内容如下：

```xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
	<import resource="axis2Config.xml"/>
	
	<bean id="helloworld" class="com.hello.HelloWorld">
	     
		<property name="name" value="Tharindu Mathew"></property>
	
	</bean>
	
	<bean id="services" class="org.wso2.spring.ws.WebServices">
	    <property name="services">
	    <list>
	   	 <bean id="helloService" class="org.wso2.spring.ws.SpringWebService">       
	            <property name="serviceBean" ref="helloworld"></property>
	            <property name="serviceName" value="helloWorldService"></property>
	         </bean>
	    </list>
	    </property>
	</bean>

</beans>

```


e. 在WebContent\WEB-INF\web.xml中引用
主要是这两句

```xml

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

```

web.xml全文看起来就变成了：

```xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WSFSpringHelloWorld</display-name>
	  
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
	
	<servlet>
		<servlet-name>axis2</servlet-name>
		<servlet-class>
			org.wso2.spring.ws.servlet.SpringAxis2Servlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>axis2</servlet-name>
		<url-pattern>/axis2/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>axis2</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<!--
		<welcome-file>index.jsp</welcome-file>
		-->
		<welcome-file>/axis2-web/index.jsp</welcome-file>
	</welcome-file-list>
  
</web-app>

```

f.  以上步骤就可以发布标准的axis服务了，为了更加方面了解，可以考虑将目录axis2-1.5.1-bin\axis2-1.5.1\webapp\axis2-web（请先下载axis2的bin包），复制到WSFSpringHelloWorld 项目中的 WebContent目录下。

3） 发布 项目
	可以在eclipse中发布war包(右键单击项目，选择export->war即可)， 发布的war包，可以部署到tomcat服务器上。当然也可以直接在eclipse上运行(右键单击项目，选择run as -> run on server即可)
	当发现可以访问 http://localhost:8080/WSFSpringHelloWorld/services/helloWorldService?wsdl 
则表示成功
（这里假定Tomcat发布的端口是8080）

2. 客户端(PHP)
需要安装 PHP 5.2.6+ , WSF PHP

有关安装WSF PHP 的步骤，请google相关文档。下载的包里面就有一个说明（README.INSTALL）。安装完毕后, 就可以使用WSClient这个类了。

新建一个php文件，内容如下： 

```php

<?php
$wsdl = "http://192.168.5.158:8080/WSFSpringHelloWorld/services/helloWorldService?wsdl";
$client = new WSClient(array("wsdl"=>$wsdl));

$proxy = $client->getProxy();

$input = array("name" => "hello");
print_r($input);
$re = $proxy->sayHello($input);
echo $re["return"]; 


echo "\n";

?>

```


其中，假定发布服务的WSDL的路径是： http://192.168.5.158:8080/WSFSpringHelloWorld/services/helloWorldService?wsdl

由此，你就可以看到从远程的java端发送过来的信息了。

结束语，

	这个只是一个引子。要想理解其中为什么这样，请google相关文档。当然 wso2上也有很多相关文档（例如：http://wso2.org/library/3208）。 
	如果您真的想用它到你的项目中，那么建议好好理解其中的代码和配置，这样才能更加变通的利用到你的项目中。