建立接口实现类的代码
<?php
include "SoapDiscovery.php";
ini_set('soap.wsdl_cache_enabled','0');//关闭缓存
//注意在开始编程的时候一点要关闭缓存这样的话,更新方法后,client端不会报错
class Service
{
public function Hello()
{
return 'hello good';
}
public function Add($a,$b)
{
return (int)$a+$b;
}
}
$wsdl=new SoapDiscovery('Service','soap');
//if(isset($_GET['wsdl'])){
// header("Content-Type:text/xml; charset=utf-8");
// echo file_get_contents("Service.wsdl");
// die; //也可以这么写。 也可以直接在URL中这样webservice.php?wsdl
//}
//$wsdl->getWSDL(); 调用或wsdl生成字符串
$ss = new SoapServer('http://localhost/Service.wsdl');
$ss->setClass('Service');
$ss->handle();
然后SoapDoscpvery.php这个wsdl生成类的代码
<?php
/**
* Copyright (c) 2005, Braulio Jos?Solano Rojas
* All rights reserved
*
* @version $Id: SoapDiscovery.class.php 66 2013-04-10 07:12:21Z ideaa $
* @copyright 2005
*/
/**
* SoapDiscovery Class that provides Web Service Definition Language (WSDL).
*
* @package SoapDiscovery
* @author Braulio Jos?Solano Rojas
* @copyright Copyright (c) 2005 Braulio Jos?Solano Rojas
* @version $Id: SoapDiscovery.class.php 66 2013-04-10 07:12:21Z ideaa $
* @access public
* */
class SoapDiscovery
{
private $class_name = '';
private $service_name = '';
/**
* SoapDiscovery::__construct() SoapDiscovery class Constructor.
*
* @param string $class_name
* @param string $service_name
* */
public function __construct($class_name = '', $service_name = '')
{
$this->class_name = $class_name;
$this->service_name = $service_name;
}
/**
* SoapDiscovery::getWSDL($protocol) Returns the WSDL of a class if the class is instantiable.
* @return string
* @throws ReflectionException
*/
public function getWSDL()
{
if (empty($this->service_name)) {
throw new Exception('No service name.');
}
$headerWSDL = "<?xml version=\"1.0\" ?>\n";
$headerWSDL .= "<definitions name=\"$this->service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n";
$headerWSDL .= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n";
if (empty($this->class_name)) {
throw new Exception('No class name.');
}
$class = new ReflectionClass($this->class_name);
if (!$class->isInstantiable()) {
throw new Exception('Class is not instantiable.');
}
$methods = $class->getMethods();
$portTypeWSDL = '<portType name="' . $this->service_name . 'Port">';
$bindingWSDL = '<binding name="' . $this->service_name . 'Binding" type="tns:' . $this->service_name . "Port\">\n<soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n";
$serviceWSDL = '<service name="' . $this->service_name . "\">\n<documentation />\n<port name=\"" . $this->service_name . 'Port" binding="tns:' . $this->service_name . "Binding\"><soap:address location=\"https://" . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] . "\" />\n</port>\n</service>\n";
$messageWSDL = '';
foreach ($methods as $method) {
if ($method->isPublic() && !$method->isConstructor()) {
$portTypeWSDL .= '<operation name="' . $method->getName() . "\">\n" . '<input message="tns:' . $method->getName() . "Request\" />\n<output message=\"tns:" . $method->getName() . "Response\" />\n</operation>\n";
$bindingWSDL .= '<operation name="' . $method->getName() . "\">\n" . '<soap:operation soapAction="urn:' . $this->service_name . '#' . $this->class_name . '#' . $method->getName() . "\" />\n<input><soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</input>\n<output>\n<soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</output>\n</operation>\n";
$messageWSDL .= '<message name="' . $method->getName() . "Request\">\n";
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
$messageWSDL .= '<part name="' . $parameter->getName() . "\" type=\"xsd:string\" />\n";
}
$messageWSDL .= "</message>\n";
$messageWSDL .= '<message name="' . $method->getName() . "Response\">\n";
$messageWSDL .= '<part name="' . $method->getName() . "\" type=\"xsd:string\" />\n";
$messageWSDL .= "</message>\n";
}
}
$portTypeWSDL .= "</portType>\n";
$bindingWSDL .= "</binding>\n";
// return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
//生成wsdl文件,将上面的return注释
$fso = fopen($this->class_name . ".wsdl", "w");
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
}
/**
* SoapDiscovery::getDiscovery() Returns discovery of WSDL.
*
* @return string
* */
public function getDiscovery()
{
return "<?xml version=\"1.0\" ?>\n<disco:discovery xmlns:disco=\"http://schemas.xmlsoap.org/disco/\" xmlns:scl=\"http://schemas.xmlsoap.org/disco/scl/\">\n<scl:contractRef ref=\"http://" . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] . "?wsdl\" />\n</disco:discovery>";
}
}
apache,和nginx中$_SERVER变量信息不太一样, apache 和nignx的主机变量前者$_SERVER['HTTP_HOST']后者$_SERVER['SERVER_HOST'];
soap:address location=\"https://" . $_SERVER['HTTP_HOST']
。中的协议原来是http 让我改成了https。因为网站是https协议。生成的wsdl的文件是http://www.wkcto.com:443/webservice.php。调用方法会出现地址性的错误下面是更改后的截图
声明一个连接的代码。当然连接直向webservice.php?wsdl最好。
推荐使用类库:composer require php2wsdl/php2wsdl
<?php
ini_set('soap.wsdl_cache_enabled','0');//关闭缓存
$soap=new SoapClient('http://localhost/wsdl.php');
//$soap=new SoapClient('http://localhost/webservice.php?wsdl');
$a=$soap->add(1,1);
//echo $soap->_soapCall('Add',array(1,2))//或者这样调用也可以
var_dump($a);
也可以在声明wsdl.php来引入wsdl进行解析,本质上说wsdl就是xml代码
<?php
header("Content-Type:text/xml; charset=utf-8;");
echo file_get_contents("Service.wsdl");
关于Java测试的代码
package testWeb;
import java.rmi.RemoteException;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class Test {
public String invokeRemoteFuc() {
String endpoint = "http://test.solo365.cn/webservice.php?wsdl";
String result = "no result!";
Service service = new Service();
Call call;
Object[] object = new Object[1];
object[0] = "Dear I miss you";//Object是用来存储方法的参数
try {
call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);// 远程调用路径
call.setOperationName("Hello");// 调用的方法名
// 设置参数名:
call.addParameter("str1", // 参数名
XMLType.XSD_STRING,// 参数类型:String
ParameterMode.IN);// 参数模式:'IN' or 'OUT'
// 设置返回值类型:
call.setReturnType(XMLType.XSD_STRING);// 返回值类型:String
result = (String) call.invoke(object);// 远程调用
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
Test t = new Test();
String result = t.invokeRemoteFuc();
System.out.println(result);
}
}
php的soap无故出错的真凶
wsdl缓存
最近在开发上用到了soap,同时还碰到了一个怪问题。
当我修改了服务端的调用方法后,更新了wsdl,客户端的参数老是丢失(不是全丢失,而是部分),让人很郁闷。
第二天我索性增加了一个服务端的新方法,更新了wsdl,但是客户端请求却报错说没有定义该方法。
最后发现是因为php对soap的wsdl进行了缓存。
解决方法:
**修改php.ini中的soap.wsdl_cache_enabled=1(改为0),soap.wsdl_cache_ttl=86400(改为0)
两项。
特别注意的是,客户端和服务端的机器都要改。**
在Java工具wsimport错误
素包含 use ="encoded"
无法解析 WSDL!
原因:本地的jdk 版本是大于1.6 ,jdk1.7以后不再支持这种
解决方法
可以用eclipse工具类操作
操作步骤:
- 新建java project
- 选中刚才的java project,然后在new web service client ,将wsdl 的路径粘贴上去 。直接finish就可以看到java project 里面有生成的java 代码了。
关于use = "encoded" 几种类型的资料阅读
https://www.cnblogs.com/kaqike/archive/2011/11/06/2238261.html
关于stackoverflow.com
网站上说 是因为这个use ="encoded"
这个类型太老了 ,不支持。
在WebXml网站中 用的是 <soap:body use="literal"/>
也就是说jdk版本高不支持。