php5实现web services
2019独角兽企业重金招聘Python工程师标准>>>
WEBService简介:
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。
它是一种构建应用程序的普遍模型,可以在任何支持网络通信的操作系统中实施运行;它是一种新的web应用程序分支,是自包含、自描述、模块化的应 用,可以发布、定位、通过web调用。Web Service是一个应用组件,它逻辑性的为其他应用程序提供数据与服务.各应用程序通过网络协议和规定的一些标准数据格式 (Http,XML,Soap)来访问Web Service,通过Web Service内部执行得到所需结果.Web Service可以执行从简单的请求到复杂商务处理的任何功能。一旦部署以后,其他Web Service应用程序可以发现并调用它部署的服务。
关键的技术和规则:
在构建和使用Web Service时,主要用到以下几个关键的技术和规则:
1.XML:描述数据的标准方法.
2.SOAP:表示信息交换的协议.
www~phperz~com
3.WSDL:Web服务描述语言.
4.UDDI(Universal Description, Discovery and Integration):通用描述、发现与集成,它是一种独立于平台的,基于XML语言的用于在互联网上描述商务的协议。
实际上,WebService的主要目标是跨平台的可互操作性。为了达到这一目标,WebService完全基于XML(可扩展标记语言)、 XSD(XMLSchema)等独立于平台、独立于软件供应商的标准,是创建可互操作的、分布式应用程序的新平台。由此可以看出,在以下三种情况下,使用 WebService会带来极大的好处。
来段企业级应用吧,主要是讲PHP5对webservice的一些实现(以下的程序可以被JAVA,NET,C等正常调用)
国内用PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考(谢谢老农提供的WSDL和程序文件)
客户端
<?php
header ( "Content-Type: text/html; charset=utf-8" );
/*
* 指定WebService路径并初始化一个WebService客户端
*/
$ws = "http://soap/soapCspMessage.php?wsdl";
$client = new SoapClient ( $ws, array ('trace' => 1, 'uri' => 'http://www.zxsv.com/SoapDiscovery/' ) );
/*
* 获取SoapClient对象引用的服务所提供的所有方法
*/
echo ("SOAP服务器提供的开放函数:");
echo ('<pre>');
var_dump ( $client->__getFunctions () );
echo ('</pre>');
echo ("SOAP服务器提供的Type:");
echo ('<pre>');
var_dump ( $client->__getTypes () );
echo ('</pre>');
echo ("执行GetGUIDNode的结果:");
//$users = $client->GetUsers();
//var_dump($HelloWorld );
$parameters = array('uname'=>'zxsv',"upassword"=>'123');
$out = $client->HelloWorld($parameters);
$datadb = $out->HelloWorldResponse;
var_dump($out);
?>
服务端
<?php
class Member
{
public $UserId;
public $Name;
public function __construct($parmas){
$this->UserId = $parmas[0];
$this->Name = $parmas[1];
}
}
$servidorSoap = new SoapServer('testphp.xml',array('uri' => 'http://www.TestPHP.com/','encoding'=>'utf-8','soap_version' => SOAP_1_2 ));
$servidorSoap->setClass(Testphp);
$servidorSoap->handle();
class Testphp {
public function HelloWorld($uid){
return array('HelloWorldResult'=>"mystring".$uid->{'uname'}.' and '.$uid->{'upassword'});
}
public function GetMember($uid){
$s=array();
for($i=0;$i<$uid->{'uid'};$i++){
$s[] =&new Member(array($i, $uid->{'uname'}.'我测试'.$i));
}
return array('GetMemberResult'=>$s);
}
}
?> www.phperz.com
到这里应该都看的懂吧
下面是WSDL文件
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.TestPHP.com/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://www.TestPHP.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://www.TestPHP.com/">
<s:element name="HelloWorld">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="upassword" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" /> www.phperz.com
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetMember">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="uid" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetMemberResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetMemberResult" type="tns:ArrayOfMember" />
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfMember">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="Member" nillable="true" type="tns:Member" />
</s:sequence>
</s:complexType>
<s:complexType name="Member">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="UserId" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" />
</s:sequence>
</s:complexType>
</s:schema>
</wsdl:types>
<wsdl:message name="HelloWorldSoapIn">
<wsdl:part name="parameters" element="tns:HelloWorld" />
</wsdl:message>
<wsdl:message name="HelloWorldSoapOut">
<wsdl:part name="parameters" element="tns:HelloWorldResponse" /> php程序员站
</wsdl:message>
<wsdl:message name="GetMemberSoapIn">
<wsdl:part name="parameters" element="tns:GetMember" />
</wsdl:message>
<wsdl:message name="GetMemberSoapOut">
<wsdl:part name="parameters" element="tns:GetMemberResponse" />
</wsdl:message>
<wsdl:portType name="TestPHPSoap">
<wsdl:operation name="HelloWorld">
<wsdl:input message="tns:HelloWorldSoapIn" />
<wsdl:output message="tns:HelloWorldSoapOut" />
</wsdl:operation>
<wsdl:operation name="GetMember">
<wsdl:input message="tns:GetMemberSoapIn" />
<wsdl:output message="tns:GetMemberSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TestPHPSoap" type="tns:TestPHPSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap:operation soapAction="http://www.TestPHP.com/HelloWorld" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetMember">
<soap:operation soapAction="http://www.TestPHP.com/GetMember" /> phperz.com
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="TestPHPSoap12" type="tns:TestPHPSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap12:operation soapAction="http://www.TestPHP.com/HelloWorld" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetMember">
<soap12:operation soapAction="http://www.TestPHP.com/GetMember" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestPHP">
<wsdl:port name="TestPHPSoap" binding="tns:TestPHPSoap">
<soap:address location="http://soap/goodwsdl/testphp.php" />
</wsdl:port>
<wsdl:port name="TestPHPSoap12" binding="tns:TestPHPSoap12">
<soap12:address location="http://soap/goodwsdl/testphp.php" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions> php程序员之家
这里有返回的两个字段,一个是返回字符串,这个很好理解 www.phperz.com
<s:element name="HelloWorld">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="upassword" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
这一段就字符串的
那返回数组的就比较麻烦了,我和老农搞了一两周才发现是WSDL文件写错了,看下面的一段 phperz.com
<s:element name="GetMember">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="uid" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetMemberResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetMemberResult" type="tns:ArrayOfMember" />
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfMember">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="Member" nillable="true" type="tns:Member" />
</s:sequence>
</s:complexType>
<s:complexType name="Member">
<s:sequence> php程序员站
<s:element minOccurs="1" maxOccurs="1" name="UserId" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" />
</s:sequence>
</s:complexType>
第一段GetMember是输入,最重要的是GetMemberResponse这段,看type="tns:ArrayOfMember"这里, 返回一个数组,WSDL中定义了ArrayOf这个,后面的就简单了,ArrayOfMember的类型是type="tns:Member" ,从name="Member"得到要返回的数组,完工。
转载于:https://my.oschina.net/adamboy/blog/29453
php5实现web services相关推荐
- java datahandler_Java Web Services:使用DataHandler类发送文件
我是 Java Web Services的新手,所以我可能做错了. 我正在尝试使用DataHandler传输文件 – 这就是我所拥有的: 网络服务: import java.net.Malformed ...
- XFire发布Web Services
步骤 1. 把XFire依赖的jar包放入lib文件夹下. 2. 在web.xml中加入如下代码: 3. 编写程序代码 首先编写一个功能的接口类. package com.yc.web.service ...
- 调用.NET XML Web Services返回数据集合一
1. 概述 很多正在开发或者打算开发XML Web Services的程序员都问过这样的一个问题:"我的Web Service返回的结果是一个DataSet类型的对象,但如果我的客户端不是用 ...
- Jboss RestEasy构建简单的RESTful Web Services示例(1)
2019独角兽企业重金招聘Python工程师标准>>> 项目上要用到webservice,鉴于现在restful webservice比较流行,打算用restful来建立webser ...
- [论文笔记] Fast Quality Driven Selection of Composite Web Services (ECOWS, 2006)
Time: 4.0 hours Jae-Ho Jang, Dong-Hoon Shin, Kyong-Ho Lee, "Fast Quality Driven Selection of Co ...
- RESTful Web Services初探
RESTful Web Services初探 作者:杜刚 近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTf ...
- JAX-RS(Java API for RESTful Web Services)常用注解
为什么80%的码农都做不了架构师?>>> 概述 JAX-RS(Java API for RESTful Web Services)是Java 提供用于开发RESTful Web ...
- VMware vSphere开发(1)安装配置VMware vSphere Web Services SDK的运行环境
一.准备 使用VMware vSphere Web Services SDK开发Java Web-service客户端应用,需要使用到java SDK和Java Web Service开发工具,如下: ...
- Web services 安全实践: 基于 HTTP Basic Authentication 为 Web services 配置传输层安全机制...
转载:http://www.ibm.com/developerworks/cn/webservices/1106_webservicessecurity/ 简介 正如"HTTP Basic ...
最新文章
- 关于内网linux系统如果安装nodejs,npm,express,mongodb,forever等
- 【数字信号处理】线性时不变系统 LTI “ 输入 “ 与 “ 输出 “ 之间的关系 ( 线性卷积起点定理 | 左边序列概念 | 推理 )
- jstl核心标签使用
- 自由自在带你品尝一种能长出果蔬的冰淇淋
- Android 编程下 java.lang.NoClassDefFoundError: cn.jpush.android.api.JPushInterface 报错
- nginx+php+memcache高速缓存openresty)
- C#实现 UDP简单广播
- 【C++深度剖析教程35】函数模板的概念和意义
- 送书 | 人类细胞图谱计划发起人Aviv Regev博士讲单细胞基因组学
- Linux 多线程可重入函数
- 为什么要破坏JVM的双亲委派模型
- 网奇iwms插件之“我浏览过的文章”
- 大学英语四级考试大纲
- python批量tif文件转化jpg文件格式
- 网站受到流量攻击怎么办
- CTS测试中testYuvBurst[1]项
- XGBOOST从原理到实战:二分类 、多分类
- 读《把时间当作朋友:运用心智获得解放》
- EAP设备自动化系统基本功能与概念
- Swoole vs Workman