本文主要以代码的形式展示以下具体如何调用wsdl接口(关于wsdl接口,本文不多做介绍了,可自行Google),本文的用例为camds相关接口的调用,出于安全,可能不会暴露一些关键信息。
一、首先以java调用为例:
1、maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.axis2.wso2/axis2 -->
<dependency>
<groupId>org.apache.axis2.wso2</groupId>
<artifactId>axis2</artifactId>
<version>1.6.1.wso2v10</version>
</dependency>
2、插件,用于生成本地java类文件
```
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>com.maksim.ws</generatePackage>
<generateDirectory>${basedir}/src/main/java</generateDirectory>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/resources/schemas</directory>
<includes>
<include>*.wsdl</include>
</includes>
</fileset>
</schema>
</schemas>
</configuration>
</plugin>
3、具体调用
package com.maksim;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
/**
* Created by maksim on 2017/5/16.
*/
@Configuration
public class WSConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.maksim.ws");
return marshaller;
}
@Bean
public WsClient wsClient(Jaxb2Marshaller marshaller) {
WsClient client = new WsClient();
client.setDefaultUri("http://****/CamdsWebService?wsdl");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
```
二、PHP调用比较简单,只需要安装soapClient拓展即可,附上PHP调用例子,代码如下:
处理类
<?php
/**
-
获取WSDL接口数据
-
@author huwen
/
class camdsWsdl
{
/*- wsdl接口地址
- @var [string]
*/
public $apiurl;
/**
- 接口方法参数
- @var [array]
*/
private $parameter;
/**
- 需要调用的接口
- @var [string]
*/
private $func;
/**
- 接口返回的结果
- @var [type]
*/
private $result;
/**
- soap实例
- @var [SoapClient]
*/
private static $soapClientHandler;
/**
- 构造函数
- @param [type] $apiurl [description]
- @param [type] $parameter [description]
- @param [type] $func [description]
*/
public function __construct($apiurl){
$this->apiurl=$apiurl;
}
public function getResult($parameter,$func){
try { $result=$this->getSoapClientHandler()->$func($parameter); if (!$result instanceof stdClass) { throw new Exception("出现异常:" . json_encode($result)); } return $result; } catch (SoapFault $soapFault) { throw new Exception($soapFault->getMessage() . $this->getSoapClientHandler()->__getLastResponse()); }
}
/**
- 调试方法
- @param [type] $var [description]
- @return [type] [description]
*/
public function p($var){
if (is_bool($var)) {
var_dump($var);
}else if (is_null($var)) {
var_dump(NULL);
}else{
echo "<pre style='position:relative;z-index:1000;padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;' >" . print_r($var,true) ."</pre>";
}
}
/**
- 获得soapClient单例
- @return [type] [description]
*/
public function getSoapClientHandler()
{
if (!self::$soapClientHandler) {
ini_set('default_socket_timeout', 10000);
self::$soapClientHandler = new SoapClient($this->getApiUrl());
}
return self::$soapClientHandler;
}
/**
- 返回接口地址
- @return [type] [description]
*/
public function getApiUrl()
{
return $this->apiurl;
}
/**
-
查看api信息
-
@return [type] [description]
*/
public function getApiInfo()
{$this->p("提供的方法\n");
$this->p($this->getSoapClientHandler()->__getFunctions());
$this->p("相关的数据结构\n");
$this->p($this->getSoapClientHandler()->__getTypes());
}
}
网友评论