首页 > 后端开发 > php教程 > JAVA调用PHP SOAP服务方法

JAVA调用PHP SOAP服务方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-20 13:00:56
原创
1110 人浏览过

JAVA调用PHP SOAP服务

WebService即是一种跨平台的远程调用方法。一方提供服务,另一方使用服务。传输协议是HTTP,而传输的数据则是XML格式的数据。

它有两种方式,WSDL和SOAP。目前仅提及SOAP。

1.PHP提供SOAP服务

定义一个类,它里面有加、减和除三种操作

 

1

2

3

4

5

6

7

8

9

10

11

12

<?php class Operator{

    public function add($x,$y){

        return $x+$y;

    }

    public function substract($x,$y){

        return $x-$y;

    }

    public function divide($x,$y){

        return $x/$y;

    }

}

?>

登录后复制


使用PHP SOAP API,提供SOAP服务。(需要开启SOAP EXTENSION)

 

1

2

3

4

5

<?php require("Operator.php");

$server = new SoapServer(null,array('uri' => "http://localhost:8089"));

$server->setClass("Operator");

$server->handle();

?>

登录后复制


假设提供服务的php为index.php

2.JAVA使用PHP提供的SOAP服务

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import javax.xml.soap.MessageFactory;

import javax.xml.soap.Node;

import javax.xml.soap.SOAPBody;

import javax.xml.soap.SOAPConnection;

import javax.xml.soap.SOAPConnectionFactory;

import javax.xml.soap.SOAPElement;

import javax.xml.soap.SOAPEnvelope;

import javax.xml.soap.SOAPException;

import javax.xml.soap.SOAPMessage;

import javax.xml.soap.SOAPPart;

public class Main {

    public void testSOAP(String opename,int x,int y){

        String url = "http://localhost:8089/index.php";

        SOAPConnectionFactory soapConnectionFactory = null;

        SOAPConnection soapConnection = null;

        MessageFactory messageFactory = null;

        SOAPMessage soapMessage = null;

        SOAPPart soapPart = null;

        SOAPEnvelope soapEnvelope = null;

        SOAPBody body = null;

        try{

            //建立连接

            soapConnectionFactory = SOAPConnectionFactory.newInstance();

            soapConnection = soapConnectionFactory.createConnection();

            messageFactory = MessageFactory.newInstance();

            soapMessage = messageFactory.createMessage();

            soapPart = soapMessage.getSOAPPart();

            soapEnvelope = soapPart.getEnvelope();

         

            //设置调用的webservice方法,及传参

            body = soapEnvelope.getBody();

            SOAPElement element = body.addChildElement(soapEnvelope.createName(opename));

            element.addChildElement("in0").addTextNode(String.valueOf(x));

            element.addChildElement("in1").addTextNode(String.valueOf(y));

            soapMessage.saveChanges();

             

            //获取返回值

            SOAPMessage reply = soapConnection.call(soapMessage,url);

            soapPart = reply.getSOAPPart();

            soapEnvelope = soapPart.getEnvelope();

            body = soapEnvelope.getBody();

            Node returnvalue = (Node) body.getChildElements().next();

         

            if (returnvalue != null) {

                  if (returnvalue.getChildNodes().item(0).getNodeName().equals("return")) {

                      List<hashmap>> ReturnArray = new ArrayList<hashmap>>();

                      for (int i=0;i<returnvalue.getchildnodes string key="returnvalue.getChildNodes().item(0).getChildNodes().item(i).getNodeName();" value="returnvalue.getChildNodes().item(0).getChildNodes().item(i).getNodeValue();" system.out.println else returned ex ex.printstacktrace try soapconnection.close catch e todo auto-generated block e.printstacktrace public static void main args main.testsoap>

 

<p><br>

运行结果如下:</p>

 

<p>3+4:<br>

#text:7<br>

9-7:<br>

#text:2<br>

20/4:<br>

#text:5</p>

<p class="item-note"><br></p></returnvalue.getchildnodes></hashmap></hashmap>

登录后复制
相关标签:
php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
怎么学好php
来自于 1970-01-01 08:00:00
0
0
0
PHP扩展intl
来自于 1970-01-01 08:00:00
0
0
0
php数据获取?
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板