在网上找了很多版本,踩了很多的坑
问题主要有两个
1.生成 wsdl文件
2.开启php.ini的配置
生成wsdl需要SoapDiscovery.class.php
我在网上找了一个错误的版本,导致文件有问题
class_name = $class_name;
$this->service_name = $service_name;
if(empty($service_url))
{
$this->service_url = "http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF'];
}else{
$this->service_url = $service_url;
}
}
/**
* SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
*
* @return string
**/
public function getWSDL() {
if (empty($this->service_name)) {
throw new Exception('No service name.');
}
$headerWSDL = "\n";
$headerWSDL.= "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.= " \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 = '';
$bindingWSDL = '\n \n";
$serviceWSDL = '\n \nservice_name.'Port" binding="tns:'.$this->service_name."Binding\">service_url."\" />\n \n \n";
$messageWSDL = '';
foreach ($methods as $method) {
if ($method->isPublic() && !$method->isConstructor()) {
$portTypeWSDL.= '\n".'\n \n";
$bindingWSDL.= '\n".' \nservice_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n\n \n";
$messageWSDL.= '\n";
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
$messageWSDL.= ' \n";
}
$messageWSDL.= " \n";
$messageWSDL.= '\n";
$messageWSDL.= ' \n";
$messageWSDL.= " \n";
}
}
$portTypeWSDL.= " \n";
$bindingWSDL.= "\n";
// return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' ');
$fso = fopen($this->class_name . ".wsdl" , "w");
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ''));
}
/**
* SoapDiscovery::getDiscovery() Returns discovery of WSDL.
*
* @return string
**/
public function getDiscovery() {
return "\n\nservice_url."?wsdl\" />\n ";
}
}
?>
class service
{
public function HelloWorld()
{
return "Hello";
}
public function Add($a,$b)
{
return $a+$b;
}
}
再去访问
creat_wsdl.php
include_once('Service.php');
include_once('SoapDiscovery.class.php');
$wsdl=new SoapDiscovery('Service','soap');//第一参数为类名,也是生成wsdl的文件名Service.wsdl,第二个参数是服务的名字可以随便写
$wsdl->getWSDL();
这个时候就会生成
Service.wsdl
里面有我们写好的方法
再去sevice.php里面加上三行数据
class service
{
public function HelloWorld()
{
return "Hello";
}
public function Add($a,$b)
{
return $a+$b;
}
}
$server=new SoapServer('Service.wsdl',array('soap_version' => SOAP_1_2));
$server->setClass("service");
$server->handle();
最后就是client文件了
header('Content-Type: text/html; charset=UTF-8');
ini_set('soap.wsdl_cache_enabled','0');//关闭缓存
$soap=new SoapClient('http://127.0.0.1/webservices/service.php?wsdl');
echo $soap->Add(1,2);
最后设置php.ini
always_populate_raw_post_data=-1