日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

PHP教程:WebService最常用的两种方法

發(fā)布時(shí)間:2025/7/25 php 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP教程:WebService最常用的两种方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

 國內(nèi)用PHP寫WebService的真的很少,網(wǎng)上資料也沒多少,公司的項(xiàng)目開發(fā)過程中,經(jīng)歷了不少這方面的東西,寫出來以供大家參考(謝謝老農(nóng)提供的WSDL和程序文件)

  客戶端

  代碼:

01.??
  
02.header ( "Content-Type: text/html; charset=utf-8" );
  
03./*
  
04.* 指定WebService路徑并初始化一個(gè)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對(duì)象引用的服務(wù)所提供的所有方法
  
10.*/
  
11.echo ("SOAP服務(wù)器提供的開放函數(shù):");
  
12.echo ('
'
);
  13.var_dump ( $client->__getFunctions () );
  
14.echo ('
'
);
  15.echo ("SOAP服務(wù)器提供的Type:");
  
16.echo ('
'
);
  17.var_dump ( $client->__getTypes () );
  
18.echo ('
'
);
  19.echo ("執(zhí)行GetGUIDNode的結(jié)果:");
  
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.?>

  服務(wù)端

  代碼:

?

01.??
  
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[] =&new Member(array($i, $uid->{'uname'}.'我測(cè)試'.$i));
  22.}
  
23.return array('GetMemberResult'=>$s);
  24.}
  
25.}
  
26.?>

  到這里應(yīng)該都看的懂吧

  下面是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>

?

  這里有返回的兩個(gè)字段,一個(gè)是返回字符串,這個(gè)很好理解

?

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>

  這一段就字符串的

  那返回?cái)?shù)組的就比較麻煩了,我和老農(nóng)搞了一兩周才發(fā)現(xiàn)是WSDL文件寫錯(cuò)了,看下面的一段

?

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”這里,返回一 個(gè)數(shù)組,WSDL中定義了ArrayOf這個(gè),后面的就簡(jiǎn)單了,ArrayOfMember的類型是type=”tns:Member” ,從name=”Member”得到要返回的數(shù)組,完工。

?

  Ping Service,博客程序提供一種通知機(jī)制,以便在第一時(shí)間將博客的更新信息發(fā)布到提供Ping Service服務(wù)的網(wǎng)站,寫聚合的時(shí)候研究了一下

  先看標(biāo)準(zhǔn)吧

  這是一個(gè)標(biāo)準(zhǔn)的Ping Service,用XMLRPC來傳數(shù)據(jù)的,注釋寫的這么詳細(xì),代碼說明就不需要了吧,PHP5開啟XMLRPC方法

  client.php

  代碼:

?

01.??
  
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.//調(diào)用rpc_client_call函數(shù)把所有請(qǐng)求發(fā)送給XML-RPC服務(wù)器端后獲取信息
  
13.$response = rpc_client_call($host, $port, $rpc_server, $Ping);
  
14.$split='';
  15.$xml = explode($split, $response);
  
16.$xml = $split . array_pop($xml);
  
17.$response = xmlrpc_decode($xml);
  
18.//輸出從RPC服務(wù)器端獲取的信息
  
19.print_r($response);
  
20./**
  
21.* 函數(shù):提供給客戶端進(jìn)行連接XML-RPC服務(wù)器端的函數(shù)
  
22.* 參數(shù):
  
23.* $host 需要連接的主機(jī)
  
24.* $port 連接主機(jī)的端口
  
25.* $rpc_server XML-RPC服務(wù)器端文件
  
26.* $request 封裝的XML請(qǐng)求信息
  
27.* 返回:連接成功成功返回由服務(wù)器端返回的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.??
  
02./**
  
03.* 函數(shù):提供給RPC客戶端調(diào)用的函數(shù)
  
04.* 參數(shù):
  
05.* $method 客戶端需要調(diào)用的函數(shù)
  
06.* $params 客戶端需要調(diào)用的函數(shù)的參數(shù)數(shù)組
  
07.* 返回:返回指定調(diào)用結(jié)果
  
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.//產(chǎn)生一個(gè)XML-RPC的服務(wù)器端
  
25.$xmlrpc_server = xmlrpc_server_create();
  
26.//注冊(cè)一個(gè)服務(wù)器端調(diào)用的方法rpc_server,實(shí)際指向的是rpc_server_extendedping函數(shù)
  
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數(shù)據(jù)
  
30.$request = $HTTP_RAW_POST_DATA;
  
31.//print_r($request);
  
32.//執(zhí)行調(diào)用客戶端的XML請(qǐng)求后獲取執(zhí)行結(jié)果
  
33.$xmlrpc_response = xmlrpc_server_call_method($xmlrpc_server, $request, null);
  
34.//把函數(shù)處理后的結(jié)果XML進(jìn)行輸出
  
35.header('Content-Type: text/xml');
  36.echo $xmlrpc_response;
  
37.//銷毀XML-RPC服務(wù)器端資源
  
38.xmlrpc_server_destroy($xmlrpc_server);
  
39.?>

  類寫的,有BUG

  代碼:

?

 01.??
  
02.class Pings {
  
03.
  
public $xmlrpc_server;
  
04.
  
public $xmlrpc_response;
  
05.
  
public $methodName;
  
06.
  
publicfunction __construct() {
  
07.
  
//產(chǎn)生一個(gè)XML-RPC的服務(wù)器端
  
08.
  $this
->xmlrpc_server = xmlrpc_server_create ();
  
09.
  $this
->run ();
  
10.
  }
  
11.
  
12.
  
//注冊(cè)一個(gè)服務(wù)器端調(diào)用的方法rpc_server,實(shí)際指向的是ping函數(shù)
  
13.
  
publicfunction 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.
  
* 函數(shù):提供給RPC客戶端調(diào)用的函數(shù)
  
19.
  
* 參數(shù):
  
20.
  
* $method 客戶端需要調(diào)用的函數(shù)
  
21.
  
* $params 客戶端需要調(diào)用的函數(shù)的參數(shù)數(shù)組
  
22.
  
* 返回:返回指定調(diào)用結(jié)果
  
23.
  
*/
  
24.
  
publicfunction 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.
  
publicfunction update(){
  
35.
  echo
'這里放更新的一些條件';
  36.
  }
  
37.
  
38.
  
publicfunction 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.
  
//把函數(shù)處理后的結(jié)果XML進(jìn)行輸出
  
43.
  header (
'Content-Type: text/xml' );
  44.
  echo $this
->xmlrpc_response;
  
45.
  }
  
46.
  
47.
  
//銷毀XML-RPC服務(wù)器端資源
  
48.
  
publicfunction __destruct() {
  
49.
  xmlrpc_server_destroy ( $this
->xmlrpc_server );
  
50.
  }
  
51.}
  
52.$Obj =new Pings ( );
  
53.?>

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

轉(zhuǎn)載于:https://www.cnblogs.com/GmrBrian/archive/2012/10/13/2722356.html

總結(jié)

以上是生活随笔為你收集整理的PHP教程:WebService最常用的两种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。