欢迎各位兄弟 发布技术文章
这里的技术是共享的
webservice是一个部署在web服务器上的,向外界暴露出一个能过通过web进行调用的API。
顾名思义,就是基于WEB的服务。它使用WEB(HTTP)方式,接收和响应外部系统的某种请求;从而实现远程调用。
1
2
3
( web service descroption language
) web 服务描述语言 ;
提供能办事的文档说明
,通常由 xml
构成 ;
简单对象访问协议
提供请求的规范
wsdl和soap虽然是web service的两大标准,但是两者并没有必然的联系,都可以独立使用。
有wsdl文件的方式
在这里先介绍标准的webservice。 那么如何创建wsdl呢?对于PHP来说这确实是件很不容易的事情,有人说用zend studio创建很方便,这是一种方法。但对于那些不喜欢用zend studio的人来说,会觉得创建一个web service还要安装zend studio,太强人所难了。wsdl文件是可以 使用脚本生成的,网友写的SoapDiscovert.class.php
<?php
/**
* Copyright (c) 2005, Braulio Jos?Solano Rojas
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* Neither the name of the Solsoft de Costa Rica S.A. nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* @version $Id$
* @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$
* @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 = '',$path = './') {
$this->class_name = $class_name;
$this->service_name = $service_name;
$this->path = $path;
}
/**
* SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
*
* @return string
* */
public function getWSDL() {
header("Content-type: text/html; charset=utf-8");
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=\"http://" . $_SERVER['SERVER_NAME'] . ':' . $_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注释
$this->wsdl_name = preg_replace('/(\\\.*\\\)/','',$this->class_name);
$fso = fopen($this->path.$this->wsdl_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>";
}
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
再准备一个提供服务的Service.php类文件或者函数就可以创建wsdl了!
<?php
class Service {
public function HelloWorld() {
return "Hello";
}
public function Add($a, $b) {
return $a + $b;
}
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
下面创建wsdl文件的creat_wsdl.php
<?php
include("Service.php");
include("SoapDiscovery.class.php");
$disc = new SoapDiscovery('Service', 'soap'); //第一个参数是类名(生成的wsdl文件就是以它来命名的,例如:下面的Service.wsdl),即Service类,第二个参数是服务的名字(这个可以随便写)
$disc->getWSDL();
1
2
3
4
5
6
##说明:通过查看"SoapDiscovery.class.php"文件的源码发现,此成员函数如果开启return语句,会返回一个xml格式的字符串,如果用 $strXML = $disc->getWSDL();echo $strXML;那么在浏览器中将不能显示xml的wsdl文件。因为浏览器默认用的是"html/text"来返回stdout的内容,需要在php脚本的头部用:
header(“Content-type: text/html; charset=utf-8”); 才可以让浏览器不把输出作为html而是作为xml来解析!
用web访问的方式运行create_wsdl.php文件,此时会生成一个Service.wsdl的文件,下面将会用到这个文件!
注意:上面方法生成的wsdl文件在windows下面没问题,但是在Linux下面的wsdl文件中的"location=XXX" 段,确实没有服务器IP地址。所以要修改该代码中的: $_SERVER[‘SERVER_NAME’] 为 $_SERVER[‘SERVER_HOST’] ,因为在linux(nginx)下面,前者变量为空,这样才能生成正确的wsdl文档!
再在Service.php文件中添加一些代码
<?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)); ##此处的Service.wsdl文件是上面生成的
$server->setClass("Service"); //注册Service类的所有方法
$server->handle(); //处理请求
?>
创建**webservice客户端**程序,测试webservice是否有效,文件名是:client.php
将以下内容拷贝进去:
<?php
ini_set('soap.wsdl_cache_enabled', "0"); //关闭wsdl缓存
$soap = new SoapClient('http://localhost/Dragon/soap/Service.php?wsdl');
echo $soap->Add(28, 2);
echo $soap->__soapCall('Add',array(28,2))//或这样调用
\#echo $soap->__Call('Add',array(28,2));
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
OK!测试通过!
需要更改生成的wsdl文件中的location的值
无WSDL文件方式
服务器端
<?php
class Service
{
public function HelloWorld()
{
return "Hello";
}
public function Add($a,$b)
{
return $a+$b;
}
}
$server=new SoapServer(null,array('uri' => "abcd"));
$server->setClass("Service");
$server->handle();
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
客户端
<?php
try {
$soap = new SoapClient(null, array(
"location" => "http://localhost/Dragon/soap/Service.php",
"uri" => "abcd", //资源描述符服务器和客户端必须对应
"style" => SOAP_RPC,
"use" => SOAP_ENCODED
));
echo $soap->Add(12, 2);
} catch (Exction $e) {
echo print_r($e->getMessage(), true);
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14