WEBService簡介:php
Web Services是由企業發佈的完成其特定商務需求的在線應用服務,其餘公司或應用軟件可以經過Internet來訪問並使用這項在線服務。 html
它是一種構建應用程序的廣泛模型,能夠在任何支持網絡通訊的操做系統中實施運行;它是一種新的web應用程序分支,是自包含、自描述、模塊化的應 用,能夠發佈、定位、經過web調用。Web Service是一個應用組件,它邏輯性的爲其餘應用程序提供數據與服務.各應用程序經過網絡協議和規定的一些標準數據格式 (Http,XML,Soap)來訪問Web Service,經過Web Service內部執行獲得所需結果.Web Service能夠執行從簡單的請求到複雜商務處理的任何功能。一旦部署之後,其餘Web Service應用程序能夠發現並調用它部署的服務。 程序員
關鍵的技術和規則: web
在構建和使用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;
服務端
<?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++){
到這裏應該都看的懂吧
下面是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>
這裏有返回的兩個字段,一個是返回字符串,這個很好理解 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>
第一段GetMember是輸入,最重要的是GetMemberResponse這段,看type="tns:ArrayOfMember"這裏, 返回一個數組,WSDL中定義了ArrayOf這個,後面的就簡單了,ArrayOfMember的類型是type="tns:Member" ,從name="Member"獲得要返回的數組,完工。