lys
2018年02月28日
php curl soap java webservice xml 实例
首先我们编写一个java的webservice服务类
package lys.TheService;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
* 通过WebService将类标记为实现WebService
@WebService
public class App
@WebResult(name="return",targetNamespace="http://TheService.lys/")
@WebMethod
/** 供客户端调用方法 该方法是非静态的,会被发布
* @param name 传入参数
* @return String 返回结果
public String getValue(String[] args){
System.out.println(args[0]);
for(String x:args){
System.out.println(x);
return "Hello:"+args[0];
public static void main(String[] args) {
Endpoint.publish("http://localhost:9091/Service/ServiceHello", new App());
System.out.println("Service success!");
}
接着我们使用php编写请求
<?php
* Created by PhpStorm.
* User: Administrator
* Date: 2018/2/28
* Time: 15:25
* 请求第三方 返回报文需进行命名空间解析
* @param $xml string xml数据
* @return array 请求第三方返回的数据
function curl_posts($url,$xml)
//XML soap协议处理
$xml_post_string = <<<xml
<?xml version="1.0" encoding="GBK"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://TheService.lys/">
<soap:Header/>
<soap:Body>
<ser:getValue >
<!--Optional:-->
<arg0>321321</arg0>
<arg0>321321213123123</arg0>
</ser:getValue>
</soap:Body>
</soap:Envelope>
var_dump($xml_post_string);
// $header = ["Content-type: application/soap+xml;charset=GBK;", "Content-Length:" . strlen($xml_post_string)];
$header = ["Content-type: text/xml;charset=GBK;", "Content-Length:" . strlen($xml_post_string)];
//设置传输的数据为xml格式与长度 SOAP POST必须两个参数
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);//设置超时时间
$result = curl_exec($ch);
$curl_code = curl_errno($ch);//curl错误码
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);//http状态码
$message = curl_error($ch);//错误消息
return array('result' => $result, 'curl_code' => $curl_code, 'http_code' => $http_code, 'msg' => $message);
$zhi = curl_posts('http://localhost:9091/Service/ServiceHello?wsdl','sadasdsa');
header('Content-Type:text/html;charset=gbk');
var_dump($zhi);
当然也是可以使用php的另外一种写法
<?php
* Created by PhpStorm.
* User: Administrator
* Date: 2018/2/25
* Time: 15:05
libxml_disable_entity_loader(false); //adding this worked for me
$client = new SoapClient("http://localhost:9091/Service/ServiceHello?wsdl" , array('trace'=>true));
var_dump($client->__getTypes());
try {
$response = $client->getValue(['arg0'=>['dasdsad',23213213]]);
var_dump($response);
}catch (SoapFault $sf){
var_dump($sf);
print ($client->__getLastRequest());
print ($client->__getLastResponse());
}
更多参考 http://www.runoob.com/soap/soap-tutorial.html
原创文章,转载请注明出处