21,893
社区成员




<?php
//server.php
class Calculator
{
/**
* 求和
*
* @param float $x
* @param float $y
* @return float
*/
public function calculator($x, $y)
{
return $x + $y;
}
}
$server = new SoapServer('./wps.wsdl');
$server->setClass('Calculator');
$server->handle();
?>
<?php
//client.php
$soap = new SoapClient('http://localhost/soap/wps.wsdl');
echo $soap->calculator(1.2, 2.5);
?>
<?xml version='1.0' encoding='UTF-8'?>
<!-- WSDL file generated by Zend Studio. -->
<definitions name="wps" targetNamespace="urn:wps" xmlns:typens="urn:wps" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="calculator">
<part name="x" type="xsd:float"/>
<part name="y" type="xsd:float"/>
</message>
<message name="calculatorResponse">
<part name="calculatorReturn" type="xsd:float"/>
</message>
<portType name="CalculatorPortType">
<operation name="calculator">
<documentation>
求和
</documentation>
<input message="typens:calculator"/>
<output message="typens:calculatorResponse"/>
</operation>
</portType>
<binding name="CalculatorBinding" type="typens:CalculatorPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="calculator">
<soap:operation soapAction="urn:CalculatorAction"/>
<input>
<soap:body namespace="urn:wps" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body namespace="urn:wps" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="wpsService">
<port name="CalculatorPort" binding="typens:CalculatorBinding">
<soap:address location="http://localhost/soap/server.php"/>
</port>
</service>
</definitions>
#include <stdio.h>
#include <iostream.h>
#include <string>
using namespace std;
#import "msxml4.dll"
using namespace MSXML2;
#import "C:\Program Files\Common Files\MSSoap\Binaries\MSSOAP30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30;
void Hello()
{
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service.
Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = "http://localhost:8080/axis/Calculator.jws?wsdl"; //连接Axis没问题
//Connector->Property["EndPointURL"] = "http://localhost/soap/server.php"; //返回“编码错误”
//Connector->Property["EndPointURL"] = "http://localhost/soap/wps.wsdl"; //运行实时错误
Connector->Connect();
// Begin the message.
//Connector->Property["SoapAction"] = "uri:AddNumbers";
Connector->Property["SoapAction"] = ""; //保持为空字符串即可
Connector->BeginMessage();
// Create the SoapSerializer object.
Serializer.CreateInstance(__uuidof(SoapSerializer30));
// Connect the serializer object to the input stream of the connector object.
Serializer->Init((_variant_t)(IUnknown*)Connector->InputStream);
// Build the SOAP Message.
//Serializer->StartEnvelope("SOAP","","UTF-8");
Serializer->StartEnvelope("SOAP","","");
Serializer->StartBody("");
Serializer->StartElement("calculator","","",""); //方法/函数名称
Serializer->StartElement("x","","",""); //参数名称
Serializer->WriteString("1.2"); //参数值
Serializer->EndElement();
Serializer->StartElement("y","","",""); //参数名称
Serializer->WriteString("2.2"); //参数值
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
// Send the message to the XML Web service.
Connector->EndMessage();
// Read the response.
Reader.CreateInstance(__uuidof(SoapReader30));
// Connect the reader to the output stream of the connector object.
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
// Display the result.
printf("Answer: \n%s\n", (const char*)Reader->Dom->xml);
}
int main()
{
CoInitialize(NULL);
Hello();
CoUninitialize();
return 0;
}
public class Calculator
{
public float calculator(float x, float y)
{
float sum;
sum=x+y;
return sum;
}
}