`
vtrtbb
  • 浏览: 358375 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

php 生成web services 类

    博客分类:
  • 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 = '') {
		$this->class_name = $class_name;
		$this->service_name = $service_name;
	}
	
	/**
	 * 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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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注释
		//$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>";
	}
}

?> 
分享到:
评论

相关推荐

    php 利用 nusoap 生成 webservices

    以下将详细讲解如何利用nusoap生成Webservices,以及服务器端和客户端的具体实现。 一、什么是Webservices? Webservices是一种基于互联网的、能够互相交换数据的服务。它们使用开放标准(如XML、SOAP、WSDL和UDDI...

    php实现webservices接口

    【PHP实现Web Services接口】 Web Services是一种基于网络的、分布式的模块化组件,它提供了一种标准的方法,使得运行在不同系统上的应用程序可以互相通信。PHP作为一种流行的服务器端脚本语言,广泛应用于Web开发...

    RESTful PHP Web Services

    ### RESTful PHP Web Services #### 一、RESTful Web Services 背景介绍 REST (Representational State Transfer) 是一种软件架构风格,主要用于构建网络应用。RESTful Web Services 是基于 REST 架构风格的一种...

    C#winform调用PHP_Webservices例子

    这里,我们创建了一个MyWebServiceSoapClient对象,它通过WSDL地址自动生成了与PHP Web服务相匹配的客户端代理。然后,我们调用sayHello方法并传入参数,最后将返回的结果显示在消息框中。 在实际开发中,可能还...

    Building XML Web Services with PHP NuSOAP

    在《Building XML Web Services with PHP NuSOAP》教程中,作者通过两个实例展示了NuSOAP的应用。第一个实例是使用NuSOAP在PHP中创建一个Web服务,然后使用Visual Basic 6.0调用该服务。第二个实例则展示了如何使用...

    webServices

    2. **生成代理类**:使用工具(如Java的wsimport或.NET的svcutil)根据WSDL生成客户端所需的代理类,这个类提供了与服务交互的方法。 3. **调用服务**:通过实例化代理类并调用其方法,客户端可以向服务发送请求并...

    Pro.PHP.XML.and.Web.Services.2006

    《Pro PHP XML and Web Services 2006》是一本专注于PHP、XML以及Web服务的专著,旨在帮助读者深入理解和应用这些技术。这本书详细介绍了如何利用PHP这一强大的服务器端脚本语言,结合XML(可扩展标记语言)的灵活性...

    利用Zend Studio for Eclipse生成wsdl文件,php调用web service

    WSDL(Web Services Description Language)文件是XML格式,用于描述Web Service接口、操作和消息结构。 2. **Zend Studio for Eclipse**:这是一款强大的PHP集成开发环境,支持Web Service的创建、调试和管理。在...

    PHP自动生成WSDL的Class

    WSDL(Web Services Description Language)是一种XML格式,用于定义Web服务的接口,描述了服务的位置、可用操作以及如何调用这些操作。SOAP(Simple Object Access Protocol)是用于交换结构化信息的协议,常与WSDL...

    清华WebServices内部讲义

    ### 清华WebServices内部讲义关键知识点梳理 #### 一、引言 本文档为清华大学内部关于Web Services的教学资料,旨在介绍Web Services的核心概念、技术背景及其在现代互联网架构中的应用。通过深入剖析JSP(Java...

    wsdl2php自动生成

    `wsdl2php`是这样一个工具,它的主要功能是自动生成PHP客户端类,这些类可以直接用于调用由WSDL文件定义的Web服务。通过这个工具,开发者可以避免手动编写繁琐的SOAP请求和解析响应的过程,从而提高开发效率和代码的...

    JSON FOR PHP4.0 PHP JSON操作 Services_JSON

    在PHP中,JSON常用于与服务器进行数据交互,特别是在Web服务和AJAX应用中。PHP 4.0版本时,官方并未内置对JSON的支持,因此需要依赖第三方库来实现JSON的操作。 "Services_JSON"是针对PHP 4.0的一个扩展库,它提供...

    C#][Web.Service][Professional.C#.Web.Services].zip

    例如,通过使用`svcutil.exe`工具,我们可以生成与Web服务对应的客户端代理类。这些代理类封装了与服务交互的所有细节,使得开发者可以像调用本地方法一样调用Web服务。 五、Web服务的安全性 在C# Web服务中,安全...

    用xfire的Eclipse_Plugin生成web服务的客户端.

    1. **自动生成Web服务客户端代码**:这是插件的核心功能之一,通过解析WSDL(Web Services Description Language)文件,插件能够自动为指定的Web服务生成客户端存根代码,极大地简化了客户端开发过程。 2. **简化...

    Building.RESTful.Web.Services.with.PHP.7.2017.9

    【标题】"Building.RESTful.Web.Services.with.PHP.7.2017.9" 指的是一本关于使用PHP 7构建RESTful Web服务的书籍,更新至2017年9月版。这本书旨在教授读者如何利用PHP这一流行的服务器端脚本语言来设计和实现符合...

    Axis生成wsds工具类.zip

    在IT行业中,Web服务是应用程序之间进行通信的一种标准方法,而WSDL(Web Services Description Language)则是一种XML格式,用于定义Web服务的具体接口和消息交换模式。Axis是Apache软件基金会开发的一个开源Java库...

    axis2(web service接口文件生成工具)

    在本文中,我们将深入探讨Axis2的核心功能、工作原理以及如何使用它来生成Web服务接口文件。 1. Axis2概述 Axis2是基于AXIOM(Abstract XML Information Model)和Axis1.x的升级版,提供了一种更高效、灵活和模块化...

    projeto_webservices_uninorte:使用SOA和WebServices模块的Web服务进行项目开发-Post-Uninorte

    project_webservices_uninorte 使用SOA和WebServices模块的Web服务进行项目开发-Post-Uninorte脚步服务器-Java桌面更改服务器URL时,必须重新编译服务器和客户端。生成服务器类wsgen -cp bin -s src -wsdl br....

Global site tag (gtag.js) - Google Analytics