程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> WebService最常用的兩種方法

WebService最常用的兩種方法

編輯:PHP綜合

企業級應用,主要是講PHP5對webservice的一些實現(以下的程序可以被Java,NET,C等正常調用)
國內用PHP寫WebService的真的很少,網上資料也沒多少,公司的項目開發過程中,經歷了不少這方面的東西,寫出來以供大家參考(謝謝老農提供的WSDL和程序文件)
客戶端

代碼:
01.<?PHP
02.header ( "Content-Type: text/Html; charset=utf-8" );
03./*
04.* 指定WebService路徑並初始化一個WebService客戶端
05.*/
06.$ws = "http://soap/soapCspMessage.PHP?wsdl";
07.$client = new SoapClIEnt ( $ws, array ('trace' => 1, 'uri' => 'http://www.zxsv.com/SoapDiscovery/' ) );
08./*
09.* 獲取SoapClIEnt對象引用的服務所提供的所有方法
10.*/
11.echo ("SOAP服務器提供的開放函數:");
12.echo ('<pre>');
13.var_dump ( $clIEnt->__getFunctions () );
14.echo ('</pre>');
15.echo ("SOAP服務器提供的Type:");
16.echo ('<pre>');
17.var_dump ( $clIEnt->__getTypes () );
18.echo ('</pre>');
19.echo ("執行GetGUIDNode的結果:");
20.//$users = $clIEnt->GetUsers();
21.//var_dump($HelloWorld );
22.$parameters = array('uname'=>'zxsv',"upassWord"=>'123');
23.$out = $clIEnt->HelloWorld($parameters);
24.$datadb = $out->HelloWorldResponse;
25.var_dump($out);
26.?>

服務端

代碼:

01.<?PHP
02.class Member
03.{
04.public $UserId;
05.public $Name;
06.public function __construct($parmas){
07.$this->UserId = $parmas[0];
08.$this->Name = $parmas[1];
09.}
10.}
11.$servidorSoap = new SoapServer('testphp.XML',array('uri' => 'http://www.TestPHP.com/','encoding'=>'utf-8','soap_version' => SOAP_1_2 ));
12.$servidorSoap->setClass(TestPHP);
13.$servidorSoap->handle();
14.class TestPHP {
15.public function HelloWorld($uid){
16.return array('HelloWorldResult'=>"mystring".$uid->{'uname'}.' and '.$uid->{'upassWord'});
17.}
18.public function GetMember($uid){
19.$s=array();
20.for($i=0;$i<$uid->{'uid'};$i++){
21.$s[] =&amp;new Member(array($i, $uid->{'uname'}.'我測試'.$i));
22.}
23.return   array('GetMemberResult'=>$s);
24.}
25.}
26.?>

到這裡應該都看的懂吧
下面是WSDL文件

代碼:

001.<?XML version="1.0" encoding="utf-8"?>
002.<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/">
003.<wsdl:types>
004.<s:schema elementFormDefault="qualifIEd" targetNamespace="http://www.TestPHP.com/">
005.<s:element name="HelloWorld">
006.<s:complexType>
007.<s:sequence>
008.<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
009.<s:element minOccurs="0" maxOccurs="1" name="upassWord" type="s:string" />
010.</s:sequence>
011.</s:complexType>
012.</s:element>
013.<s:element name="HelloWorldResponse">
014.<s:complexType>
015.<s:sequence>
016.<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
017.</s:sequence>
018.</s:complexType>
019.</s:element>
020.<s:element name="GetMember">
021.<s:complexType>
022.<s:sequence>
023.<s:element minOccurs="1" maxOccurs="1" name="uid" type="s:int" />
024.<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
025.</s:sequence>
026.</s:complexType>
027.</s:element>
028.<s:element name="GetMemberResponse">
029.<s:complexType>
030.<s:sequence>
031.<s:element minOccurs="0" maxOccurs="1" name="GetMemberResult" type="tns:ArrayOfMember" />
032.</s:sequence>
033.</s:complexType>
034.</s:element>
035.<s:complexType name="ArrayOfMember">
036.<s:sequence>
037.<s:element minOccurs="0" maxOccurs="unbounded" name="Member" nillable="true" type="tns:Member" />
038.</s:sequence>
039.</s:complexType>
040.<s:complexType name="Member">
041.<s:sequence>
042.<s:element minOccurs="1" maxOccurs="1" name="UserId" type="s:int" />
043.<s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" />
044.</s:sequence>
045.</s:complexType>
046.</s:schema>
047.</wsdl:types>
048.<wsdl:message name="HelloWorldSoapIn">
049.<wsdl:part name="parameters" element="tns:HelloWorld" />
050.</wsdl:message>
051.<wsdl:message name="HelloWorldSoapOut">
052.<wsdl:part name="parameters" element="tns:HelloWorldResponse" />
053.</wsdl:message>
054.<wsdl:message name="GetMemberSoapIn">
055.<wsdl:part name="parameters" element="tns:GetMember" />
056.</wsdl:message>
057.<wsdl:message name="GetMemberSoapOut">
058.<wsdl:part name="parameters" element="tns:GetMemberResponse" />
059.</wsdl:message>
060.<wsdl:portType name="TestPHPSoap">
061.<wsdl:Operation name="HelloWorld">
062.<wsdl:input message="tns:HelloWorldSoapIn" />
063.<wsdl:output message="tns:HelloWorldSoapOut" />
064.</wsdl:Operation>
065.<wsdl:Operation name="GetMember">
066.<wsdl:input message="tns:GetMemberSoapIn" />
067.<wsdl:output message="tns:GetMemberSoapOut" />
068.</wsdl:Operation>
069.</wsdl:portType>
070.<wsdl:binding name="TestPHPSoap" type="tns:TestPHPSoap">
071.<soap:binding transport="http://schemas.XMLsoap.org/soap/http" />
072.<wsdl:Operation name="HelloWorld">
073.<soap:Operation soapAction="http://www.TestPHP.com/HelloWorld"   />
074.<wsdl:input>
075.<soap:body use="literal" />
076.</wsdl:input>
077.<wsdl:output>
078.<soap:body use="literal" />
079.</wsdl:output>
080.</wsdl:Operation>
081.<wsdl:Operation name="GetMember">
082.<soap:Operation soapAction="http://www.TestPHP.com/GetMember"  />
083.<wsdl:input>
084.<soap:body use="literal" />
085.</wsdl:input>
086.<wsdl:output>
087.<soap:body use="literal" />
088.</wsdl:output>
089.</wsdl:Operation>
090.</wsdl:binding>
091.<wsdl:binding name="TestPHPSoap12" type="tns:TestPHPSoap">
092.<soap12:binding transport="http://schemas.XMLsoap.org/soap/http" />
093.<wsdl:Operation name="HelloWorld">
094.<soap12:Operation soapAction="http://www.TestPHP.com/HelloWorld"  />
095.<wsdl:input>
096.<soap12:body use="literal" />
097.</wsdl:input>
098.<wsdl:output>
099.<soap12:body use="literal" />
100.</wsdl:output>
101.</wsdl:Operation>
102.<wsdl:Operation name="GetMember">
103.<soap12:Operation soapAction="http://www.TestPHP.com/GetMember"  />
104.<wsdl:input>
105.<soap12:body use="literal" />
106.</wsdl:input>
107.<wsdl:output>
108.<soap12:body use="literal" />
109.</wsdl:output>
110.</wsdl:Operation>
111.</wsdl:binding>
112.<wsdl:service name="TestPHP">
113.<wsdl:port name="TestPHPSoap" binding="tns:TestPHPSoap">
114.<soap:address location="http://soap/goodwsdl/testphp.PHP" />
115.</wsdl:port>
116.<wsdl:port name="TestPHPSoap12" binding="tns:TestPHPSoap12">
117.<soap12:address location="http://soap/goodwsdl/testphp.PHP" />
118.</wsdl:port>
119.</wsdl:service>
120.</wsdl:definitions>

這裡有返回的兩個字段,一個是返回字符串,這個很好理解

01.<s:element name="HelloWorld">
02.<s:complexType>
03.<s:sequence>
04.<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
05.<s:element minOccurs="0" maxOccurs="1" name="upassWord" type="s:string" />
06.</s:sequence>
07.</s:complexType>
08.</s:element>
09.<s:element name="HelloWorldResponse">
10.<s:complexType>
11.<s:sequence>
12.<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
13.</s:sequence>
14.</s:complexType>
15.</s:element>

這一段就字符串的
那返回數組的就比較麻煩了,我和老農搞了一兩周才發現是WSDL文件寫錯了,看下面的一段

01.<s:element name="GetMember">
02.<s:complexType>
03.<s:sequence>
04.<s:element minOccurs="1" maxOccurs="1" name="uid" type="s:int" />
05.<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
06.</s:sequence>
07.</s:complexType>
08.</s:element>
09.<s:element name="GetMemberResponse">
10.<s:complexType>
11.<s:sequence>
12.<s:element minOccurs="0" maxOccurs="1" name="GetMemberResult" type="tns:ArrayOfMember" />
13.</s:sequence>
14.</s:complexType>
15.</s:element>
16.<s:complexType name="ArrayOfMember">
17.<s:sequence>
18.<s:element minOccurs="0" maxOccurs="unbounded" name="Member" nillable="true" type="tns:Member" />
19.</s:sequence>
20.</s:complexType>
21.<s:complexType name="Member">
22.<s:sequence>
23.<s:element minOccurs="1" maxOccurs="1" name="UserId" type="s:int" />
24.<s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" />
25.</s:sequence>
26.</s:complexType>

第一段GetMember是輸入,最重要的是GetMemberResponse這段,看type=”tns:ArrayOfMember”這裡,返回一 個數組,WSDL中定義了ArrayOf這個,後面的就簡單了,ArrayOfMember的類型是type=”tns:Member” ,從name=”Member”得到要返回的數組,完工。

Ping Service,博客程序提供一種通知機制,以便在第一時間將博客的更新信息發布到提供Ping Service服務的網站,寫聚合的時候研究了一下
先看標准吧
這是一個標准的Ping Service,用XMLRPC來傳數據的,注釋寫的這麼詳細,代碼說明就不需要了吧,PHP5開啟XMLRPC方法
clIEnt.PHP


代碼:
01.<?PHP
02.$host  = 'zxsv';
03.$port  = 80;
04.$rpc_server = '/test/XMLrpc_server.PHP';
05.$title = 'zxsv';
06.$server = 'http://zxsv/test/';
07.$rss = 'http://zxsv/test/rss.PHP';
08.//weblogUpdates.Ping方法
09.$Ping = XMLrpc_encode_request('weblogUpdates.Ping', array($title, $server ));
10.//weblogUpdates.extendedPing方法
11.$extendedPing = XMLrpc_encode_request('weblogUpdates.extendedPing', array($title, $server, $rss ));
12.//調用rpc_clIEnt_call函數把所有請求發送給XML-RPC服務器端後獲取信息
13.$response = rpc_clIEnt_call($host, $port, $rpc_server, $Ping);
14.$split = '<?XML version="1.0" encoding="iso-8859-1"?>';
15.$XML =  explode($split, $response);
16.$xml = $split . array_pop($XML);
17.$response = xmlrpc_decode($XML);
18.//輸出從RPC服務器端獲取的信息
19.print_r($response);
20./**
21.* 函數:提供給客戶端進行連接XML-RPC服務器端的函數
22.* 參數:
23.* $host  需要連接的主機
24.* $port  連接主機的端口
25.* $rpc_server XML-RPC服務器端文件
26.* $request  封裝的XML請求信息
27.* 返回:連接成功成功返回由服務器端返回的XML信息,失敗返回false
28.*/
29.function rpc_clIEnt_call($host, $port, $rpc_server, $request) {
30.  
$fp = fsockopen($host, $port);
31.  
$query = "POST $rpc_server HTTP/1.0\nUser_Agent: XML-RPC ClIEnt\nHost: ".$host."\nContent-Type: text/XML\nContent-Length: ".strlen($request)."\n\n".$request."\n";
32.  
if (!fputs($fp, $query, strlen($query))) {
33.      
$errstr = "Write error";
34.      
return false;
35.  
}
36.  
$contents = '';
37.  
while (!feof($fp)){
38.      
$contents .= fgets($fp);
39.  
}
40.  
fclose($fp);
41.  
return $contents;
42.}
43.?>

server.PHP


代碼:
01.<?PHP
02./**
03.* 函數:提供給RPC客戶端調用的函數
04.* 參數:
05.* $method 客戶端需要調用的函數
06.* $params 客戶端需要調用的函數的參數數組
07.* 返回:返回指定調用結果
08.*/
09.function rpc_server_extendedping($method, $params) {
10.   
$title = $params[0];
11.   
$server = $params[1];
12.   
$rss = $params[2];
13.       
//中間的判斷,成功返回$XML_RPC_String
14.   
$XML_RPC_String = array('flerror'=>false,'message'=>'Thanks for the ping.');
15. 
return $XML_RPC_String;
16.}
17.function rpc_server_ping($method, $params) {
18.   
$title = $params[0];
19.   
$server = $params[1];
20.       
//中間的判斷,成功返回$XML_RPC_String
21.   
$XML_RPC_String = array('flerror'=>false,'message'=>'Thanks for the ping.');
22. 
return $XML_RPC_String;
23.}
24.//產生一個XML-RPC的服務器端
25.$xmlrpc_server = XMLrpc_server_create();
26.//注冊一個服務器端調用的方法rpc_server,實際指向的是rpc_server_extendedping函數
27.xmlrpc_server_register_method($XMLrpc_server, "weblogUpdates.extendedPing", "rpc_server_extendedping");
28.xmlrpc_server_register_method($XMLrpc_server, "weblogUpdates.Ping", "rpc_server_ping");
29.//接受客戶端POST過來的XML數據
30.$request = $HTTP_RAW_POST_DATA;
31.//print_r($request);
32.//執行調用客戶端的XML請求後獲取執行結果
33.$xmlrpc_response = xmlrpc_server_call_method($XMLrpc_server, $request, null);
34.//把函數處理後的結果XML進行輸出
35.header('Content-Type: text/XML');
36.echo $XMLrpc_response;
37.//銷毀XML-RPC服務器端資源
38.xmlrpc_server_destroy($XMLrpc_server);
39.?>

類寫的,有BUG


代碼:

01.<?PHP
02.class Pings {
03.   
public $XMLrpc_server;
04.   
public $XMLrpc_response;
05.   
public $methodName;
06.   
public function __construct() {
07.       
//產生一個XML-RPC的服務器端
08.       
$this->xmlrpc_server = XMLrpc_server_create ();
09.       
$this->run ();
10.   
}
11.
12.   
//注冊一個服務器端調用的方法rpc_server,實際指向的是ping函數
13.   
public function rpc_server() {
14.       
$this->methodName = !$this->methodName ? 'weblogUpdates.extendedPing':'weblogUpdates.Ping';
15.       
xmlrpc_server_register_method ( $this->XMLrpc_server, $this->methodName, array (__CLASS__, "ping"));
16.   
}
17.       
/**
18.    
* 函數:提供給RPC客戶端調用的函數
19.    
* 參數:
20.    
* $method 客戶端需要調用的函數
21.    
* $params 客戶端需要調用的函數的參數數組
22.    
* 返回:返回指定調用結果
23.    
*/
24.   
public function ping($method, $params) {
25.       
$this->title = $params [0];
26.       
$this->server = $params [1];
27.       
$this->rss = $params [2];
28.       
$this->tag = $params [3];
29.       
//$a  = $this->title ? $this->update():'';
30.       
$string = array ('flerror' => false, 'message' => 'Thanks for the ping.', 'legal' => "You agree that use of the blueidea.com ping service is governed by the Terms of Use found at www.blueidea.com." );
31.       
return $string;
32.   
}
33.
34.   
public function update(){
35.       
echo '這裡放更新的一些條件';
36.   
}
37.
38.   
public function run() {
39.       
$this->rpc_server ();
40.       
$request = isset ( $GLOBALS ["HTTP_RAW_POST_DATA"] ) ? file_get_contents ( "PHP://input" ) : $GLOBALS ["HTTP_RAW_POST_DATA"];
41.       
$this->xmlrpc_response = xmlrpc_server_call_method ( $this->XMLrpc_server, $request, null );
42.       
//把函數處理後的結果XML進行輸出
43.       
header ( 'Content-Type: text/XML' );
44.       
echo $this->XMLrpc_response;
45.   
}
46.
47.   
//銷毀XML-RPC服務器端資源
48.   
public function __destruct() {
49.       
xmlrpc_server_destroy ( $this->XMLrpc_server );
50.   
}
51.}
52.$Obj = new Pings ( );
53.?>

WebService的最常用的兩種方法算是寫齊了

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved