`

刚在项目中应用到了java与php通过webservice进行数据交互。觉得挺有意思,贴出来,跟大家分享。

阅读更多

   刚在项目中应用到了java与php通过webservice进行数据交互。觉得挺有意思,贴出来,跟大家分享。

一.java编写webservice服务端,php作为客户端调用.

1.首先我们写一个简单的java类并发布webservice.

package com.php;

import java.util.Map;

/**
 * @author yangjuqi
 * @createdate 2009-5-18 下午04:43:09
 *
 */

public class WebServiceImpl {

 public String sendTransact(Map map) throws Exception {
  System.out.println("::: Call testModel1 :::");
  
  if(map!=null){
   String bugmanifestid = StringUtil.getValue(map.get("bugmanifestid"));
   String editedby = StringUtil.getValue(map.get("editedby"));
   String dditeddate = StringUtil.getValue(map.get("dditeddate"));
   String fullinfo = StringUtil.getValue(map.get("fullinfo"));
   String action = StringUtil.getValue(map.get("action"));
   System.out.println("bugmanifestid ->" +bugmanifestid);
   System.out.println("editedby      ->" +editedby);
   System.out.println("dditeddate    ->" +dditeddate);
   System.out.println("fullinfo      ->" +fullinfo);
   System.out.println("action        ->" +action);
  }
  return "success";
 }
}

 2.配置server-config.wsdd

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
 xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

 <handler name="URLMapper"
  type="java:org.apache.axis.handlers.http.URLMapper" />
 <handler name="auth"
  type="java:com.php.AuthenticationHandler" />
 <handler name="URLLogging"
  type="java:com.php.LogHandler">
  <parameter name="filename" value="c:\\MyService.log" />
 </handler>

 <service name="IWebService" provider="java:RPC">
  <parameter name="className"
   value="com.php.WebServiceImpl" />
  <parameter name="allowedMethods" value="*" />
  <namespace>http://localhost:8088/testphpweb</namespace>    
 </service>

 <transport name="http">
  <requestFlow>
   <handler type="URLMapper" />
   <handler type="URLLogging" />
  </requestFlow>
 </transport>

</deployment>

3.发布到jboss后,访问http://localhost:8088/testphpweb/services/IWebService?wsdl能看到xml文件就说明webservice发布好了。

4.写testphpweb.php文件

 <?php
/*
 * @author      juqi yang <yangjuqi@126.com>
 * @create date 2009-05-18
 */
header("Content-Type: text/html; charset=GB2312");
echo " ::: PHP CALL JAVA-WEBSERVICE ::: <br>";
require_once("nusoap/lib/nusoap.php");

// 要访问的webservice路径
$NusoapWSDL="http://localhost:8088/testphpweb/services/IWebService?wsdl";

// 生成客户端对象
$client = new soapclient($NusoapWSDL, true);

// 设置参数(注意:PHP只能以'数组集'方式传递参数,如果服务端是java,用Map接收)
$param = array( 'bugmanifestid' => 'E090500001',
    'editedby'      => '张三',
    'dditeddate'    => '2009-05-19',
    'fullinfo'      => '已联系刘德华,筹备今晚吃饭的事,等待回复',
    'action'        => '0');

echo "begin remote 。。。<br>";
// 调用远程方法
$result = $client->call('sendTransact', array($param));
echo "end remote 。。。<br>";

// 显示执行结果
if (!$err=$client->getError()){  
 echo '结果 : '.$result;    
}else{  
 echo '错误 : '.$err;  
}  
?>

 5.启动apache,访问http://localhost/service/testphpweb.php

php页面显示:

 ::: PHP CALL JAVA-WEBSERVICE :::
begin remote 。。。
end remote 。。。
结果 : success

jboss后台监视结果:

17:12:20,781 INFO  [STDOUT] ::: Call testModel1 :::
17:12:20,781 INFO  [STDOUT] bugmanifestid ->E090500001
17:12:20,781 INFO  [STDOUT] editedby      ->张三
17:12:20,781 INFO  [STDOUT] dditeddate    ->2009-05-19
17:12:20,781 INFO  [STDOUT] fullinfo      ->已联系刘德华,筹备今晚吃饭的事,等待回复
17:12:20,796 INFO  [STDOUT] action        ->0

到此,php作为客户端调用java写的webservice服务端完成.

二,php编写webservice服务端,java作为客户端调用.

1.编写php webservice    

<?php
/*
 * @author      juqi yang <yangjuqi@126.com>
 * @create date 2009-05-18
 */
header("Content-Type: text/html; charset=GB2312");
require_once("nusoap/lib/nusoap.php");

function sendManifest($param)
{

  //把接收到的数据显示出来
    return "hello ".$param["projectid"]."<=>".$param["projectname"]."<=>".$param["moduleid"];
}

$server = new nusoap_server();

//配置WSDL namespace
$server->configureWSDL('myservice',                                    //服务名称
     'http://localhost/service/web_call_center.php',                   //tns指定的namespace,一般填写自己的URI
     true,                                                             //endpoint url or false
     'rpc',                                                            //服务样式
     'http://schemas.xmlsoap.org/soap/http',                           //传输协议,一直是这个。
     'http://localhost/service/web_call_center.php'                    //wsdl 'types'元素targetNamespace
);

// 注册web服务
$server->register('sendManifest',                                      // 服务
    array(
 'projectid'     => 'xsd:string',
 'projectname'   => 'xsd:string',
 'moduleid'      => 'xsd:string',
 'modulepath'    => 'xsd:string',
 'bugtitle'      => 'xsd:string',
 'bugtype'       => 'xsd:string',
 'openedby'      => 'xsd:string',
 'openeddate'    => 'xsd:string',
 'assignedto'    => 'xsd:string',
 'assigneddate'  => 'xsd:string',
 'fixedtime'     => 'xsd:string',
 'fullinfo'      => 'xsd:string',
 'bugmanifestid' => 'xsd:string'),                                  // 输入参数;数组,指定类型
    array('resultCode' => 'xsd:string'),                               // 输出;数组,指定类型
    'http://localhost/service/web_call_center.php',                    // namespace of method
    '',                                                                // soapaction
    'rpc',                                                             // style
    'encoded',                                                         // use
    'serviceConsumeNotify'                                             // documentation
);

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>

2.启动apache后,访问  http://localhost/service/phpserver.php,如果页面如下图所示,表示webservice发布好了。


 

 3.编写java客户端CallPhpServer .java 并调用php webservice

package com.php;

import java.util.HashMap;
import java.util.Map;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

/**
 * @author yangjuqi
 * @createdate 2009-5-18 下午05:31:06
 *
 */

public class CallPhpServer {

 /**
  * 测试方法
  * @return
  * @throws Exception
  */
 public static String callManifest() throws Exception {
  System.out.println("0");
  Service service = new Service();

  Call call = (Call) service.createCall();
  System.out.println("1");
  call.setTargetEndpointAddress(new java.net.URL("http://localhost/service/phpserver.php")); 
  call.setOperationName("sendManifest");
  System.out.println("2");
  Map map=new HashMap();
  map.put("projectid", "109");
  map.put("projectname", new String("新MM国际物流平台".getBytes(),"iso-8859-1"));
  map.put("moduleid", "11");
  map.put("modulepath", new String("财务管理".getBytes(),"iso-8859-1"));
  map.put("bugtitle", new String("关于总账报表数据的问题".getBytes(),"iso-8859-1"));
  map.put("bugtype", "TrackThings");
  map.put("openedby", "zhangsan");
  map.put("openeddate", "2009-05-31");
  map.put("assignedto", "liumang");
  map.put("assigneddate", "2009-05-31");
  map.put("fixedtime", "2009-06-03");
  map.put("fullinfo", new String("现在总账报表页面下的合计数据不对,烦请抓紧事件核实确认更正,谢谢!".getBytes(),"iso-8859-1"));
  map.put("bugmanifestid", "E090500001");
  call.addParameter("param", org.apache.axis.Constants.SOAP_ARRAY,javax.xml.rpc.ParameterMode.IN);
  call.setReturnType(org.apache.axis.Constants.XSD_STRING);
  System.out.println("3");
  Object obj=call.invoke(new Object[]{map});
  return obj.toString();
 }
 public static void main(String[] args) throws Exception {
  System.out.println("::: call php webservice :::");
  String str = callManifest();

  String result=new String(str.getBytes("iso-8859-1"),"GBK");
  System.out.println(result);
 }
}
控制台显示结果:

::: call php webservice :::
0
log4j:WARN No appenders could be found for logger (org.apache.axis.i18n.ProjectResourceBundle).
log4j:WARN Please initialize the log4j system properly.
1
2
3
hello 109<=>新MM国际物流平台<=>11
到此,java作为客户端调用php的webservice服务端完成.

http://blog.163.com/yangjuqi@126/blog/static/284522852009524435214/

 

分享到:
评论

相关推荐

    java定时进行webservice数据上报

    java定时进行webservice数据上报java定时进行webservice数据上报java定时进行webservice数据上报

    WebService数据交互事例

    Flex+WebService数据交互的事例

    基于webservice的Android与服务器端数据交互

    基于webservice的Android与服务器端数据交互。

    Java调用php的webservice

    Java调用php的webservice: header("Content-Type:text/html;charset=UTF-8"); // require('lib/nusoap.php'); require_once("lib/nusoap.php"); $server = new soap_server(); $server-&gt;configureWSDL('...

    Java-WebService应用实例

    java-WebService应用实例中包括所用的jar包,说明文档,还收集的Spring集成XFire开发WebService的应用

    SAP&Java的Webservice实例

    本文将通过 SAP 系统和外部系统的交互为例,讲述从 SAP 到 SAP,SAP 到 Java,Java 到 Java,Java 到 SAP 的 Webservice 实例。 一、SAP 系统间的 Webservice 调用 在 SAP 中,可以使用 SE37 创建函数,类型设置为...

    java调用json参数的webservice

    java调用json参数的webservice 涉及技术: JAVA JSON WEBSERVICE

    纯flex网站前台(httpservice、webservice进行数据交互)

    纯flex网站前台,通过httpservice、webservice进行数据交互

    C#调用JavaWebService

    调用WebService,最简单的办法当然是直接添加WEB引用,然后自动产生代理类,但是在调用JAVA的WebService时并没有这么简单,特别是对于SoapHeader的处理,通过C#添加Web引用方式访问JavaWebService的方法,除了string...

    Java访问c#开发的webservice

    在大型项目开发中,避免不了分布式的应用,分布式应用的业务逻辑一般分别部署到若干个服务器上,供各个客户端程序调用。而部署到服务器上的业务逻辑一般用webservice实现。开发webservice可以使用java、也可以使用c#...

    java发布webservice接口

    java开发过程中,很多地方都会遇到数据传递,远程获取数据问题,我这个简单的webservice接口发布可以在java开发过程中,很多地方都会遇到数据传递,远程获取数据问题,我这个简单的webservice接口发布可以在

    Java使用SOAP获取webservice实例解析

    在这个过程中,Java 程序需要使用到 SOAP 协议来发送请求和接收响应,实现对 Webservice 的调用。 首先,需要了解 Webservice 的基本概念。Webservice 是一种基于网络的软件系统,可以提供各种服务,如天气预报、...

    利用Java编写简单的WebService实例

    在本文中,我们详细介绍了如何使用 Java 编写简单的 WebService 实例,并使用 Apache Axis 将其发布到 Tomcat 下的 Web 应用中。通过本文,我们可以了解到 WebService 的基本概念和实现方式,以及如何使用 Java 和 ...

    android 客户端与webservice之间的交互

    android 客户端与webservice之间的交互,服务器端采用java web编写,值得参考

    php调用java写的webservice

    里面是php调用java写的webservice的源码和说明,希望对大家有所帮助

    VB.NET/JAVA 采用SOAP 与Webservice发送/接收数据

    VB.NET(2010)/JAVA 采用SOAP 与Webservice发送/接收数据

    java android 调用webservice

    java android 调用webservice,java android 调用webservice

    java使用XFire调用webService接口

    在本文中,我们将学习如何使用 XFire 框架在 Java 中调用 webService 接口。XFIRE 是一个基于 Java 的开源框架,用于简化 Web 服务的开发和集成。下面,我们将通过一个简单的例子,展示如何使用 XFire 调用 ...

    Flex4 通过HttpService与Java进行交互项目

    这是本人做的Flex4 通过HttpService与Java进行交互项目,本人测试通过。本人在此声明运行环境为:MyEclipse 8.5,JDK 6,Tomcate 6,Flex 4. 本人已经将Flex4整合到Myeclipse 8.5中去了,所以这个项目是在MyEclipse ...

Global site tag (gtag.js) - Google Analytics