跳至正文
i@laoer.com

技术、生活、感悟 -- Laoer的博客

i@laoer.com

技术、生活、感悟 -- Laoer的博客

  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
关

搜索

  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
i@laoer.com

技术、生活、感悟 -- Laoer的博客

i@laoer.com

技术、生活、感悟 -- Laoer的博客

  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
关

搜索

  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
PHP

PHP SOAP实践

作者 Laoer
2009年3月27日 2 分钟阅读
0

我现在公司里的系统都是SOA/面向服务架构的,用Java编写的Web Service,理论上Web Service支持异构语言的,我想测试一下用PHP来调用Java的Web Service是否方便。

首先用Java建立一个Web Service服务端(用CXF,JAX-WS实现),下面是公布出来的WSDL

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
<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="SampleWebService" targetNamespace="http://www.abc.net/WS/2008" xmlns:ns1="http://webservice.passport.abc.net/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.abc.net/WS/2008" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:import location="http://192.168.19.42:8080/sample/service/SampleWebService?wsdl=SampleWebService.wsdl" namespace="http://webservice.passport.abc.net/">
    </wsdl:import>
  <wsdl:message name="helloUser">
    <wsdl:part element="ns1:helloUser" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="helloUserResponse">
    <wsdl:part element="ns1:helloUserResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:binding name="SampleWebServiceSoapBinding" type="ns1:SampleWebService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="helloUser">
      <soap:operation soapAction="" style="document" />
      <wsdl:input name="helloUser">
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output name="helloUserResponse">
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="SampleWebService">
    <wsdl:port binding="tns:SampleWebServiceSoapBinding" name="SampleWebServicePort">
      <soap:address location="http://192.168.19.42:8080/sample/service/SampleWebService" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="SampleWebService" targetNamespace="http://www.abc.net/WS/2008" xmlns:ns1="http://webservice.passport.abc.net/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.abc.net/WS/2008" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:import location="http://192.168.19.42:8080/sample/service/SampleWebService?wsdl=SampleWebService.wsdl" namespace="http://webservice.passport.abc.net/"> </wsdl:import> <wsdl:message name="helloUser"> <wsdl:part element="ns1:helloUser" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="helloUserResponse"> <wsdl:part element="ns1:helloUserResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:binding name="SampleWebServiceSoapBinding" type="ns1:SampleWebService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="helloUser"> <soap:operation soapAction="" style="document" /> <wsdl:input name="helloUser"> <soap:body use="literal" /> </wsdl:input> <wsdl:output name="helloUserResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="SampleWebService"> <wsdl:port binding="tns:SampleWebServiceSoapBinding" name="SampleWebServicePort"> <soap:address location="http://192.168.19.42:8080/sample/service/SampleWebService" /> </wsdl:port> </wsdl:service> </wsdl:definitions>

这个Web Service有个“helloUser”方法,传入一个参数,它返回一段文本“Hello 参数!”,

PHP5之后已经加入了SOAP的支持,我最开始用PHP5的SOAP函数来调用,下面是代码

1
2
$client = new SoapClient('http://192.168.19.42:8080/sample/service/SampleWebService?wsdl');
var_dump($client->__getFunctions());

$client = new SoapClient('http://192.168.19.42:8080/sample/service/SampleWebService?wsdl'); var_dump($client->__getFunctions());

但报错Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: <message> ‘helloUser’ already defined in …,我搜了PHP网站上的相关文档也没找到答案,没办法,我只能找其他SOAP的组件,找到了NuSOAP,它最近发布的版本是0.7.3,是在2007年发布的,现在已经2009了,用用试试吧,参考nusoap的例子,写好自己代码

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
require_once('nusoap.php');
$client = new nusoap_client('http://192.168.19.42:8080/sample/service/SampleWebService?wsdl', 'wsdl');
$err = $client->getError();
if ($err) {
	echo '<h2>Constructor error</h2><p>' . $err . '</p>';
}
$param = array('arg0'=>'laoer');
$result = $client->call('helloUser', array('parameters' => $param),"http://webservice.passport.abc.net/",'', false, false,"document","literal");
 
if ($client->fault) {
	echo '<h2>Fault</h2><p>';
	print_r($result);
	echo '</p>';
} else {
	// Check for errors
	$err = $client->getError();
	if ($err) {
		// Display the error
		echo '<h2>Error</h2><p>' . $err . '</p>';
	} else {
		// Display the result
		echo '<h2>Result</h2><p>';
		//print_r($result);
		echo $result["return"];
		echo '</p>';
	}
}
 
echo '<h2>Request</h2><p>' . htmlspecialchars($client->request, ENT_QUOTES) . '</p>';
echo '<h2>Response</h2><p>' . htmlspecialchars($client->response, ENT_QUOTES) . '</p>';

require_once('nusoap.php'); $client = new nusoap_client('http://192.168.19.42:8080/sample/service/SampleWebService?wsdl', 'wsdl'); $err = $client->getError(); if ($err) { echo '<h2>Constructor error</h2><p>' . $err . '</p>'; } $param = array('arg0'=>'laoer'); $result = $client->call('helloUser', array('parameters' => $param),"http://webservice.passport.abc.net/",'', false, false,"document","literal"); if ($client->fault) { echo '<h2>Fault</h2><p>'; print_r($result); echo '</p>'; } else { // Check for errors $err = $client->getError(); if ($err) { // Display the error echo '<h2>Error</h2><p>' . $err . '</p>'; } else { // Display the result echo '<h2>Result</h2><p>'; //print_r($result); echo $result["return"]; echo '</p>'; } } echo '<h2>Request</h2><p>' . htmlspecialchars($client->request, ENT_QUOTES) . '</p>'; echo '<h2>Response</h2><p>' . htmlspecialchars($client->response, ENT_QUOTES) . '</p>';

执行结果

Result
Hello laoer!

Request
POST /sample/service/SampleWebService HTTP/1.0
Host: 192.168.19.42:8080
User-Agent: NuSOAP/0.7.3 (1.114)
Content-Type: text/xml; charset=UTF-8
SOAPAction: “”
Content-Length: 436

我们可以看到Request的SOAP文本

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://webservice.passport.abc.net/">
	<SOAP-ENV:Body>
		<helloUser xmlns="">
			<arg0 xmlns="">laoer</arg0>
		</helloUser>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://webservice.passport.abc.net/"> <SOAP-ENV:Body> <helloUser xmlns=""> <arg0 xmlns="">laoer</arg0> </helloUser> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

Response
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
SOAPAction: “”
Content-Type: text/xml;charset=UTF-8
Content-Length: 237
Date: Fri, 27 Mar 2009 04:27:11 GMT
Connection: close

Response的SOAP文本

1
2
3
4
5
6
7
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<soap:Body>
		<ns2:helloUserResponse xmlns:ns2="http://webservice.passport.abc.net/">
			<return>Hello laoer!</return>
		</ns2:helloUserResponse>
	</soap:Body>
</soap:Envelope>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:helloUserResponse xmlns:ns2="http://webservice.passport.abc.net/"> <return>Hello laoer!</return> </ns2:helloUserResponse> </soap:Body> </soap:Envelope>

碰到一个小问题就是PHP给方法传参数,看是我不知道怎么填,后来我用soapUI,做了一个SOAP Request的例子,发现参数的Name是arg0,如果是多个参数,则arg0、arg1、arg2…以此类推,所以我们定义的参数是$param = array(‘arg0’=>’laoer’),还有就是nusoap里对返回信息的编码有点问题,如果你Web Service放回的编码是UTF-8,则nusoap多转换了一次,在nusoap.php文件nusoap_client类里把$decode_utf8置为false就可以了。

标签:

PHPSOAPWeb Service
作者

Laoer

1999年开始做互联网,做过开发、技术管理,也创过几次业。 现在依然在一线做产品、写代码、折腾AI。 关注AI如何重新改变软件、公司和人的工作方式。 公众号:Laoer杂谈。

关注我
其他文章
上一个

Java RSA研究

下一个

生日在愚人节

暂无评论!成为第一个。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

微信订阅

欢迎订阅微信公众号--“Laoer杂谈”,搜索微信号:laoertalk,“Laoer杂谈”是关于技术、创业、生活的杂谈与随笔,希望与大家多多交流。

用药有数·DoseLoop

用药有数·DoseLoop 一款不会响完就消失的用药提醒。每次计划用药持续追踪,直到有明确结果。支持慢病、老人、ADHD 等各种用药场景。

标签

AI AI Agent AI Coding Anthropic ChatGPT Claude Claude Code Codex DeepMind DeepSeek DeepSeek Harness Gemini Google GPT iOS iPhone Java JavaScript jQuery Kimi Kohana Linux LLM Mac Memcached Meta Mysql Nginx OpenAI Oracle PHP RSA Sam Altman SNS Struts Tomcat Web2.0 互联网 人工智能 创业 大模型 工作 旅游 生活 管理

日历

2026 年 11 月
日 一 二 三 四 五 六
1234567
891011121314
15161718192021
22232425262728
2930  
« 10 月    

近期评论

  • AI Coding 文档管理:写代码越来越快,文档却更重要了 发表在《AI写代码已经不稀奇了,真正的变化是很多东西突然值得做了》
  • 大模型商业化:中国AI模型越来越强,为什么反而更难赚钱? 发表在《DeepSeek V4涨价后,真正的问题不是谁在用,而是谁在赚钱》
  • Laoer 发表在《AI 有没有意识还没定论,Anthropic 为什么先限制虐待它?》
  • 爱生活 发表在《AI 有没有意识还没定论,Anthropic 为什么先限制虐待它?》
  • 爱生活 发表在《AI 有没有意识还没定论,Anthropic 为什么先限制虐待它?》
  • AI数据分析如何改变BI工作流?15分钟完成一份运营报告 发表在《我把DeepSeek Harness接进生产环境后,发现AI运维真正改变的不是查日志》
  • 99%的代码都由AI写了,那程序员真正值钱的是什么? – i@laoer.com 发表在《我把DeepSeek Harness接进生产环境后,发现AI运维真正改变的不是查日志》
  • 爱生活 发表在《你的一分钟语音,可能是别人的十分钟》
  • 爱生活 发表在《Bill Gates为什么突然不相信“AI最终会创造更多工作”了?》
  • DeepSeek Harness来了,国产AI Coding终于开始补最短的那块板 – i@laoer.com 发表在《这次真不慌了,Claude随时可以取消订阅》

分类

  • iOS
  • Java
  • JavaScript
  • Linux
  • Mac
  • PHP
  • Web2.0
  • 互联网
  • 人工智能
  • 创业
  • 博客文章
  • 商业观察
  • 工作
  • 数据库
  • 构架设计
  • 生活

归档

  • 2026 年 11 月
  • 2026 年 10 月
  • 2026 年 9 月
  • 2026 年 8 月
  • 2026 年 7 月
  • 2026 年 6 月
  • 2026 年 1 月
  • 2024 年 12 月
  • 2024 年 10 月
  • 2024 年 8 月
  • 2024 年 7 月
  • 2023 年 12 月
  • 2023 年 1 月
  • 2022 年 8 月
  • 2021 年 12 月
  • 2021 年 10 月
  • 2020 年 12 月
  • 2020 年 1 月
  • 2018 年 12 月
  • 2018 年 8 月
  • 2018 年 4 月
  • 2017 年 12 月
  • 2016 年 12 月
  • 2016 年 8 月
  • 2016 年 2 月
  • 2015 年 12 月
  • 2015 年 1 月
  • 2013 年 6 月
  • 2013 年 5 月
  • 2012 年 11 月
  • 2012 年 7 月
  • 2012 年 6 月
  • 2012 年 5 月
  • 2012 年 3 月
  • 2012 年 1 月
  • 2011 年 12 月
  • 2011 年 11 月
  • 2011 年 10 月
  • 2011 年 9 月
  • 2011 年 8 月
  • 2010 年 11 月
  • 2010 年 2 月
  • 2010 年 1 月
  • 2009 年 12 月
  • 2009 年 11 月
  • 2009 年 9 月
  • 2009 年 7 月
  • 2009 年 6 月
  • 2009 年 5 月
  • 2009 年 4 月
  • 2009 年 3 月
  • 2009 年 2 月

其他操作

  • 登录
  • 条目 feed
  • 评论 feed
  • WordPress.org
Copyright 2026 — i@laoer.com. All rights reserved. Blogsy WordPress Theme