PHP调用java的webservice(SOAP)

现在都是用微服务了,早期有个概念和微服务差不多的东西,叫SOA。
最近有个项目就需要调用java那边的webservice获取订单状态,由于之前不了解走了些弯路,此次记录一下。

soap client 使用

PHP要开启soap

调用步骤
  1. 连接soap
  2. 找出对外的function
  3. 找出支持的参数types
  4. 传参调用function,获取结果。
连接soap
$wsdl = 'http://172.16.88.125/ap/getOrderInfo?wsdl';
$soap=new \SoapClient($wsdl);
获取function

连接之后,先看下该wsdl开放的function

echo $soap->__getFunctions();

可以看到相关暴露出来的方法:

getOrderInfoWithDelivery getOrderInfo(getOrderInfo $parameters)"

之后调用时要传相关的function

获取types

找到对应方法的参数和参数类型,之后按要求传递

echo $soap->__getTypes();

可以看到相关的参数和类型:

"struct getOrderInfo {  tactOrderVehicleInfo parmInput; }",
"struct tactOrderVehicleInfo {  string phone;  string start;  string end; }",
"struct baseInputInfo {  string fromCode;  string toCode;  string messagId;  string chgSourceSys; }",
"struct baseOutputInfo {  int returnFlag;  string errorMessage; }"

这里注意,PHP中没有struct,用array代替即可,不需要专门去new一个对象或转xml格式。
此次要调用getOrderInfo方法,传的参数parmInput对应的结构体是tactOrderVehicleInfo,它包含三个参数:

phone, // string 字符串
startdate, // string 字符串
enddate  // string 字符串

对应PHP中传参如下:

$paramData = array('phone'=> $phone,
            'startdate' => '2020-01-10 00:00:00',
            'enddate' => '2020-05-10 00:00:00');
$param = array('parmInput' => $paramData);
调用soap的方法

准备好了参数后,直接调用即可

$result = $soap->getOrderInfo($param);

返回的结果是个对象,可直接使用,不需要再去转xml什么的。

echo gettype($result);

soap client 示例

$wsdl = 'http://172.16.88.125/ap/getOrderInfo?wsdl';
// 连接soap
$soap=new \SoapClient($wsdl);

// echo $soap->__getFunctions(); // 查看方法
// echo $soap->__getTypes(); // 查看要传的数据类型

// 根据function和types,配置好参数
$paramData = array('phone'=> $phone,
            'startdate' => '2020-01-10 00:00:00',
            'enddate' => '2020-05-10 00:00:00');
$param = array('parmInput' => $paramData);

// 调用方法,获取结果
$result = $soap->getOrderInfo($param);

参考

https://www.cnblogs.com/hujun1992/p/wsdl.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 axios 发送 POST 请求来调用 Java WebService SOAP 接口。首先需要了解 SOAP 协议及其请求格式,然后可以按照以下步骤进行调用。 1. 安装 axios 可以使用 npm 安装 axios: ``` npm install axios --save ``` 2. 构造 SOAP 请求 构造 SOAP 请求的格式如下: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example.com"> <soapenv:Header/> <soapenv:Body> <ser:methodName> <!--Optional:--> <arg0>?</arg0> <!--Optional:--> <arg1>?</arg1> </ser:methodName> </soapenv:Body> </soapenv:Envelope> ``` 其中,`ser:methodName` 为要调用Java WebService 方法名,`arg0`、`arg1` 等为方法参数,可以根据实际情况进行修改。 3. 发送 SOAP 请求 使用 axios 发送 SOAP 请求的代码如下: ```javascript import axios from 'axios' const url = 'http://example.com/soap/service' // WebService 地址 const action = 'http://service.example.com/methodName' // SOAPAction const xml = ` <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example.com"> <soapenv:Header/> <soapenv:Body> <ser:methodName> <!--Optional:--> <arg0>?</arg0> <!--Optional:--> <arg1>?</arg1> </ser:methodName> </soapenv:Body> </soapenv:Envelope> ` // SOAP 请求体 axios.post(url, xml, { headers: { 'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': action } }).then(response => { console.log(response.data) }).catch(error => { console.log(error) }) ``` 其中,`url` 为 WebService 地址,`action` 为 SOAPAction,需要根据实际情况进行修改。`xml` 为 SOAP 请求体,需要根据实际情况进行修改。在发送请求时,需要指定 `Content-Type` 为 `text/xml;charset=UTF-8`,并在请求头中加入 `SOAPAction`。 4. 解析响应 Java WebService 返回的响应也是 SOAP 格式的,需要进行解析。可以使用第三方库 `xml2js` 将响应转换为 JavaScript 对象,代码如下: ```javascript import axios from 'axios' import { parseString } from 'xml2js' const url = 'http://example.com/soap/service' // WebService 地址 const action = 'http://service.example.com/methodName' // SOAPAction const xml = ` <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example.com"> <soapenv:Header/> <soapenv:Body> <ser:methodName> <!--Optional:--> <arg0>?</arg0> <!--Optional:--> <arg1>?</arg1> </ser:methodName> </soapenv:Body> </soapenv:Envelope> ` // SOAP 请求体 axios.post(url, xml, { headers: { 'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': action } }).then(response => { parseString(response.data, (error, result) => { if (error) { console.log(error) } else { console.log(result) } }) }).catch(error => { console.log(error) }) ``` 在响应中,可以通过 `result.Body` 获取到 Java WebService 返回的实际内容,需要根据实际情况进行解析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值