Java Calls PHP Soap service
WebService is a cross-platform, remote invocation method. One party provides services and the other uses the services. The transport protocol is HTTP, and the transmitted data is data in XML format.
It has two ways, WSDL and soap. Only soap is currently mentioned.
1.PHP SOAP Service available
Defines a class that contains addition, subtraction, and three operations
Use the PHP soap API to provide SOAP services. (need to turn on soap EXTENSION)
"http://localhost:8089"); $server->setclass ("Operator"); $server->handle ();? >
Assuming PHP for service is index.php
2.JAVA SOAP service provided with PHP
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{//Establish connection soapconnectionfactory = Soapconnectionfactory.newinstance (); soapconnection = Soapconnectionfactory.createconnection (); messagefactory = Messagefactory.newinstance (); soapMessage = Messagefactory.createmessage (); Soappart =Soapmessage.getsoappart (); SoapEnvelope = Soappart.getenvelope ();//Set the WebService method of the call, and the reference 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 ();//Gets the return value 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.getchildn Odes (). Item (0). Getnodename (). Equals ("return")) {List
> returnarray = new ArrayList
> (); for (int i=0;i
operation are as follows:
3+4:
#text: 7
9-7:
#text: 2
20/4:
#text: 5