茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729063 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_292.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

PHP特區 : [轉貼]PHP and Web Services 學習筆記

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]實戰WebService III: REST篇(基於php)
實戰WebService III: REST篇(基於php)

REST是什麼

REST是英文Representational State Transfer的縮寫,中文翻譯:表述性狀態轉移。

他是由Roy Thomas Fielding博士在他的論文 《Architectural Styles and the Design of Network-based Software Architectures》中提出的一個術語。

REST本身只是為分佈式超媒體系統設計的一種架構風格,而不是標準.
Restful Web Service面向資源,不是面向動作(Action)
REST風格Web Service報文格式
什麼,你一點都不懂我說什麼?那麼先看看Rest基本概念

有效的XML格式都可以說是Rest風格的報文。如YAHOO提供的Restful Web Service的一個例子 : http://api.search.yahoo.com/WebSearchService/V1/webSearch?query=%5C%22XML%20Query%5C%22&appid=YahooDemo
下面是訪問後返回結果片斷

<?xml version="1.0" encoding="UTF-8" ?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:srch"
xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/WebSearchService/V1/WebSearchResponse.xsd"
type="web" totalResultsAvailable="575000"
totalResultsReturned="10" firstResultPosition="1"
moreSearch="/WebSearchService/V1/webSearch?query=%5C%22XML+Query%5C%22&appid=YahooDemo®ion=us">
  <Result>
    <Title>W3C XML Query (XQuery)</Title>
    <Summary>Language that allows for flexible query facilities to extract data
    from real and virtual documents on the Web.</Summary>
    <Url>http://www.w3.org/XML/Query</Url>
    <ClickUrl>http://uk.wrs.yahoo.com/_ylt=A0Je5Vx_EWpHwV0AkC7dmMwF;
    _ylu=X3oDMTB2cXVjNTM5BGNvbG8DdwRsA1dTMQRwb3MDMQRzZWMDc3IEdnRpZAM-/
    SIG=11fuf4pkj/EXP=1198220031/**http%3A//www.w3.org/XML/Query</ClickUrl>
    <DisplayUrl>www.w3.org/XML/Query</DisplayUrl>
    <ModificationDate>1198051200</ModificationDate>
    <MimeType>text/html</MimeType>
    <Cache>
      <Url>http://uk.wrs.yahoo.com/_ylt=A0Je5Vx_EWpHwV0AkS7dmMwF;
      _ylu=X3oDMTBwOHA5a2tvBGNvbG8DdwRwb3MDMQRzZWMDc3IEdnRpZAM-/
      SIG=16gufb4vc/EXP=1198220031/**http%
      3A//66.218.69.11/search/cache%3Fei=UTF-8%26query=%255C%2522XML%2BQuery%255C%2522%26
      appid=YahooDemo%26u=www.w3.org/XML/Query%26w=%2522xml%2Bquery%2522%26d=DYOEo7XiP-1e%26icp=1%26.intl=us</Url>
      <Size>58502</Size>
    </Cache>
  </Result>
  <Result>
    <Title>XQuery 1.0: An XML Query Language</Title>
    <Summary>Don Chamberlin (XML Query WG), IBM Almaden Research Center,
    via http://www. ... has been developed by the W3C XML Query Working Group,
    which is part of the ...</Summary>
    <Url>http://www.w3.org/TR/xquery/</Url>
    <ClickUrl>http://uk.wrs.yahoo.com/_ylt=A0Je5Vx_EWpHwV0Aky7dmMwF;
    _ylu=X3oDMTB2ZjQ4dDExBGNvbG8DdwRsA1dTMQRwb3MDMgRzZWMDc3IEdnRpZAM-/
    SIG=11g15fbj0/EXP=1198220031/**http%3A//www.w3.org/TR/xquery/</ClickUrl>
    <DisplayUrl>www.w3.org/TR/xquery/</DisplayUrl>
    <ModificationDate>1169193600</ModificationDate>
    <MimeType>text/html</MimeType>
    <Cache>
      <Url>http://uk.wrs.yahoo.com/_ylt=A0Je5Vx_EWpHwV0AlC7dmMwF;
      _ylu=X3oDMTBwZG5hOWwzBGNvbG8DdwRwb3MDMgRzZWMDc3IEdnRpZAM-/SIG=16hn3aelo/EXP=1198220031
      /**http%3A//66.218.69.11/search/cache%3Fei=UTF-8%26query=%255C%2522XML%2BQuery%255C%2522%26
       *      appid=YahooDemo%26u=www.w3.org/TR/xquery/%26w=%2522xml%2Bquery%2522%26d=YBKWrbXiP9TA%26icp=1%26.intl=us</Url>
      <Size>525624</Size>
    </Cache>
  </Result>
....
</ResultSet>
<!-- ws02.search.scd.yahoo.com compressed/chunked Wed Dec 19 22:53:51 PST 2007 -->


下面是我使用CakePhp(一個PHP框架)在本機構建了一個rest服務,
當訪問http://localhost/hostel/rest/countries/listing 時,得到下面的XML

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
  <countries type='array'>
    <country type='struct'>
      <id>830</id>
      <title>United States</title>
    </country>
    <country type='struct'>
      <id>831</id>
      <title>Canada</title>
    </country>
    ....
  </countries>
</rsp>

再次強調:Restful Web Service是一種風格,XML是任意的。不過最近好像出了WADL規範應用於Rest,好像WSDL規範SOAP一樣,有空可以研究一下。
訪問Restful Web Service(客戶端代碼)

再次確認,你理解了Restful?---風格,是一種風格,所以:
Restful webservice客戶端像解析一般的xml一樣,獲得Restful Web Service返回的XML流,用DOM/SAX/SimpleXML解析它均可。

這裡使用php的SimpleXML API操縱前面提到Yahoo服務。

SimpleXML 擴展適合於操縱不很複雜或者嵌套不太深並且沒有混合內容的 XML 文檔。SimpleXML 比 DOM 更容易編碼,就像名稱所暗示的那樣。如果處理的文檔結構已知,就更加直觀。libXML2 架構的互操作性大大增強了 DOM 和 SimpleXML 的靈活性,能夠隨意在 DOM 和 SimpleXML 之間交換導入格式。

下面的代碼使用 SimpleXML 擴展解析查詢結果。

<?php
  //This query does a search for any Web pages relevant to "XML Query"
  $query = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?"."query=%5C%22XML%20Query%5C%22&appid=YahooDemo";
  $xml = simplexml_load_file($query);
  // Load up the root element attributes
  foreach($xml->attributes() as $name=>$attr) {
   $res[$name]=$attr;
  }
  //Use one of those "informational" elements to display the total
  //number of results for the query.
  echo "<p>The query returns ".$res["totalResultsAvailable"]." total results The first 10 are as follows:</p>";
  //Unlike with DOM, where we loaded the entire document into the result object,
  // with SimpleXML, we get back an object in thefirst place,
  // so we can just use the number of results returned to loop through the Result members.
  for($i=0; $i<$res['totalResultsReturned']; $i++) {
   //The object represents each piece of data as a member variable
   //rather than an array element, so the syntax is a little bit
   //different from the DOM version.
   $thisResult = $xml->Result[$i];
   echo "<a href='".$thisResult->ClickUrl."'><b>".
   $thisResult->Title."</b></a>: ";
   echo $thisResult->Summary;
   echo "<br /><br />";
  }
?>

輸出結果類似如下HTML效果:

寫一個自己的Restful Web Service

別嫌我囉嗦,再一次地,前面說過:Restful Web Service是任意的有效的XML。所以使用php/jsp等服務器端技術,不需要任何框架都可以生成你想要得XML流。如此說來:這個Restful Web Service例子也沒什麼好寫的嘍

CakePHP是一個基於PHP的web框架,提供了更好的輸出Restful風格的Web Service的能力。Y有興趣請看官方Cakephp文檔


原文出處:实战WebService III: REST篇(基于php) - 百草园 - JavaEye技术网站
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|