VC6写的Web service客户端如何连接PHP服务器端

zla_0723 2008-04-10 12:08:56
我用PHP5给的样例,可以实现PHP客户端和服务器端的连接。另外还有一个VC写的客户端,可以连接Java写的基于Axis的服务器端。但是现在想要连接VC客户端到PHP服务器端时,老提示“错误的编码格式”。哪位高手能否帮个忙啊?

PHP服务器端
<?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客户端

<?php
//client.php
$soap = new SoapClient('http://localhost/soap/wps.wsdl');
echo $soap->calculator(1.2, 2.5);
?>


用Zend生成的WSDL描述文件

<?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>


VC客户端

#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;
}


Java服务器端

public class Calculator
{
public float calculator(float x, float y)
{
float sum;
sum=x+y;
return sum;
}
}
...全文
173 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wsb10 2008-06-09
UP
回复
flowerain 2008-05-27
个人觉得是structure的问题,我也遇到了这种情况,在php里面直接用字符串作为输入输出参数就可以,但是在vc或者.net生成的wsdl里面,参数进入和返回结果都是用特定的数据类型,在php里面表现为stdClass,你可以看看我写的两篇文章,希望能有帮助。我也因为这个东西折腾了好长事件。
http://blog.csdn.net/flowerain/archive/2008/03/30/2229563.aspx
http://blog.csdn.net/flowerain/archive/2008/05/27/2488213.aspx
回复
zla_0723 2008-05-05
最后还是没有弄出来。感觉PHP的SOAP并不通用,无法跟Java里的Soap相比。Axis开发SOAP还是很快的。
回复
meiZiNick 2008-04-30
都是很好的建议! 值得学习
回复
UltraBejing 2008-04-30
没遇到过这种情况.
回复
zla_0723 2008-04-19
顶上啊。

达人们,帮下撒,多谢了!
回复
zla_0723 2008-04-15
$server = new SoapServer('./wps.wsdl');
$soap = new SoapClient('http://localhost/soap/wps.wsdl');

上面这两个是PHP5内置的Soap类,可以直接用来创建soap服务器端和客户端,但是也正是PHP对soap这种奇怪的实现方法,让VC客户端(如一楼所示代码)无从解读,不是爆错误的编码格式就是直接运行时错误。
回复
ten789 2008-04-14
$server = new SoapServer('./wps.wsdl');

$soap = new SoapClient('http://localhost/soap/wps.wsdl');

这两个黑箱子 还真不知道是啥样的 读读代码吧

两个点之间通讯 需要个协议 例如HTTP协议 PHP应用大多是HTTP协议

PHP也支持原生的TCP/IP协议 socket扩展 不过不是很底层 一般应用足够 你可以实现大多数应用 例如游戏外挂 也就是平常说的封包

public function connectMaster()
{
if (@socket_connect($this->socket, $this->config->server->master_ip, $this->config->server->master_port) == false){
$this->log->setLog('ConnectMasterServerError', socket_strerror(socket_last_error()));
$this->log->setError();
}else {
$this->log->setLog('ConnectmasterServerOK');
}
}
public function masterLogin($user, $pass)
{
if ($this->config->server->type == 4)
{

}else {
$msg = pack('v1', 0x64) . pack('V', $this->config->server->version);
$msg .= pack('a24', $user) . pack('a24', $pass);
$msg .= pack('C*', $this->config->server->master_version);
}
$this->send($msg);
$this->log->setLog('setMasterServer', strlen($msg).'BIT</p>');
}

protected function send($msg)
{
socket_write($this->socket, $msg, strlen($msg));
}

RO脱机外挂 登录MAST
协议:2位动作编号2位游戏版本号 24位用户名24位密码其余登录服务器版本号 不足用0补齐
回复
zla_0723 2008-04-14
顶一下,CSDN人气现在怎么也这么差啊。。。
回复
sagittaquas 2008-04-10
。。。看来我可能是老土了,当我没说
回复
sagittaquas 2008-04-10
php soap 中有个register方法,用php访问时可以不用,但是不标准,其他语言访问不了
回复
相关推荐
发帖
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
帖子事件
创建了帖子
2008-04-10 12:08
社区公告
暂无公告