DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 結合社會媒體 API 和基於 XML 的數據格式
結合社會媒體 API 和基於 XML 的數據格式
編輯:XML詳解     

本文面向致力於理解社會媒體環境和如何跨各種 API 應用基於 XML 的數據格式的開發人員。很多開發人員對這一點感興趣,其原因是基於 XML 的數據遍布各種社會媒體 API。基於 XML 的數據格式有助於標准化和簡化跨各種異構站點和服務的 HTTP 編程模型。

  社會媒體簡介

  術語社會媒體 指的是允許用戶共享媒介、內容、數據等的站點和服務。有些社會媒體站點和服務眾所周知,比如 LinkedIn、Facebook、Twitter 和 MySpace,Google 還提供了大量類似的站點和服務。新聞媒體也正在快速轉變為社會媒體提供模型。

  常用縮略詞

  API:應用程序編程接口

  Html:超文本標記語言

  HTTP:超文本傳輸協議

  URI:統一資源標識符

  URL:統一資源定位符

  XML:可擴展標記語言

  社會媒體提供工具包括在線論壇、網頁博客、wiki、圖像共享、視頻共享、在線書簽和社交活動流。

  XML 在社會媒體社區中的數據交換中扮演重要角色。社會媒體供應商在它們提供的用於促進對它們的服務的訪問的 API 中廣泛使用基於 XML 的方言。

  基於 XML 的數據格式簡介

  XML 是一種基於文本的標記語言,用於組織具有多種不同方言的數據的結構。XML 方言通過可以被嵌入到一個關聯 XML 文檔的模式定義。多數常用編程語言提供對創建、更新和解析 XML 文檔的支持。

  基於 XML 的方言應用於廣泛的 Web 服務中。Web 服務中更常用的一些方言包括 POX、RSS、Atom 和 SOAP。其他新出現的方言也廣受歡迎。表 1 列示並描述了 Web 服務中一些更常用的、基於 XML 的方言。


表 1. Web 服務中常用的、基於 XML 的方言

方言 說明 SOAP   SOAP 最初代表 Simple Object Access Protocol,但它現在不再是一個首字母縮略詞。SOAP 是一個基於 XML 的協議規范,用於在 Web 服務上下文中交換結構化數據。它最初設計用於 Remote Procedure Call (RPC) 和面向文檔的消息交換。隨著用於定義其格式、交換模式、傳輸級別綁定等特性的其他規范的引入,SOA 的復雜程度也逐漸增加。這種復雜性阻礙了服務供應商和社會媒體供應商對它的廣泛采用。 RSS RSS 代表 Really Simple Syndication 或 Rich Site Summary,它是一種基於 XML 的方言,用於組織各種 Web 提要(如博客、新聞和其他在線數據源)提供的數據的結構。RSS 文檔允許發布者以一種標准和迅速的方式聚合內容。RSS 客戶使用計算機桌面上、浏覽器中或來自一個移動設備的應用程序,以特定的間隔從 RSS 站點自動檢索更新。RSS 客戶通過在給定的 RSS 閱讀器應用程序中輸入提要的地址來訂閱 RSS 提要。 Atom Atom Syndication Format 是用於 Web 提要的一種基於 XML 的方言,其使用方式與 RSS 類似。Atom 提要比 RSS 更綜合一些,原因是每個 Atom 元素可以包含圖像、指向內容的外部鏈接、元數據等。 FBML FBML 代表 Facebook Markup Language,它是 Facebook 提供的一個 Html 子集,用於允許應用程序開發人員構建在 Facebook 服務器上運行的 Html 站點。 OST 和 OSML OpenSocial Template (OST) 和 OpenSocial Markup Language (OSML) 是由 Google 等 OpenSocial 網絡成員定義的基於 XML 的方言,用於支持開發人員構建遵循行業推薦標准並在 OpenSocial 兼容的容器中運行的社會媒體應用程序和站點。OST 和 OSML 鼓勵數據驅動的應用程序和服務開發,同時推動標記和編程邏輯之間的明確分離。

  將 XML 數據格式應用到社會媒體 API

  社會媒體 API 是由社會媒體供應商提供的請求/響應交互框架,用於支持第三方站點構建與社會媒體供應商的功能交互的站點、頁面和服務等。eBay、Amazon、 Google、Facebook、Twitter 和 OpenSocial 是幾個為構建社交媒體應用程序和服務提供 API 的供應商。下面展示這些基於 XML 的 API 的一些簡單示例。

  eBay

  eBay 提供的 API 允許開發人員提交項目並在 eBay 上列示,檢索 eBay 類別的當前列表,查看關於在 eBay 上列示的項目的信息,檢索項目的高競價人信息,檢索一個給定用戶正在出售的項目的列表,檢索一個給定用戶已經出價的項目的列表,顯示其他站點上的 eBay 列表,以及留下關於賣家的反饋。

  檢索作為 XML 文檔的基本用戶信息

  清單 1 是一個 HTTP 請求示例,該請求檢索一個 eBay 用戶的作為 XML 文檔的默認/基本信息。


清單 1. eBay GetUserProfile
http://open.api.ebay.com/shopping?callname=GetUserProfile 
  &responseencoding=XML 
  &appid=johndoesAppID 
  &siteid=0 
  &version=525 
  &UserID=johndoesUserID 

  清單 2 展示可以使用 HTTP POST 傳輸的 XML 格式的相同請求。


清單 2. 作為 XML 的 eBay GetUserProfile
<?XML version="1.0" encoding="utf-8"?> 
<GetUserProfileRequest XMLns="urn:ebay:apis:eBLBaseComponents"> 
 <UserID>johndoesUserID</UserID> 
</GetUserProfileRequest> 

  檢索 XML 文檔形式的擴展用戶信息

  為便於檢索關於給定用戶的其他信息,eBay API 提供了 IncludeSelector 參數。

  清單 3 是檢索給定用戶的詳細信息的 HTTP GET 請求的 URL 格式示例。


清單 3. 帶有細節的 eBay GetUserProfile
http://open.api.ebay.com/shopping?callname=GetUserProfile 
  &responseencoding=XML 
  &appid=johndoesAppID 
  &siteid=0 
  &version=525 
  &UserID=johndoesUserID 
  &IncludeSelector=Details 

  清單 4 是可以使用 HTTP POST 傳輸的 XML 格式的相同的詳細用戶信息請求。


清單 4. 帶有細節的 XML 格式的 eBay GetUserProfile
<?XML version="1.0" encoding="utf-8"?> 
<GetUserProfileRequest XMLns="urn:ebay:apis:eBLBaseComponents"> 
 <UserID>hypothetical-user</UserID> 
 <IncludeSelector>Details</IncludeSelector> 
</GetUserProfileRequest> 

  清單 5 展示了使用 清單 3 和 清單 4 中展示的請求檢索時返回的一個典型響應示例。這個響應包含關於該用戶的詳細信息。


清單 5. eBay GetUserProfile 響應
<?XML version="1.0" encoding="utf-8"?> 
<GetUserProfileResponse XMLns="urn:ebay:apis:eBLBaseComponents"> 
 <Timestamp>2007-11-09T21:05:05.781Z</Timestamp> 
 <Ack>Success</Ack> 
 <Build>e525_core_APILW_5028523_R1</Build> 
 <Version>525</Version> 
 <User> 
  <FeedbackScore>1054</FeedbackScore> 
  <FeedbackPrivate>false</FeedbackPrivate> 
  <FeedbackRatingStar>Red</FeedbackRatingStar> 
  <NewUser>false</NewUser> 
  <RegistrationDate>2005-11-01T20:50:00.000Z</RegistrationDate> 
  <RegistrationSite>US</RegistrationSite> 
  <Status>Confirmed</Status> 
  <SellerBusinessType>Undefined</SellerBusinessType> 
  <UserID>johndoesUserID</UserID> 
  <StoreURL>http://stores.ebay.com/johndoesStore</StoreURL> 
  <StoreName>John Doe's Store</StoreName> 
  <SellerItemsURL> 
   http://search.ebay.com/_W0LMsarsrjohndoesUserIDQQhtZ-1 
  </SellerItemsURL> 
  <AboutMeURL>http://members.ebay.com/aboutme/johndoesUserID</AboutMeURL> 
  <MyWorldURL>http://myworld.ebay.com/johndoesUserID</MyWorldURL> 
  <MyWorldSmallImage> 
   http://i2.ebayimg.ebay.com/01/u/000/00/23/animage.JPG?set_id=34 
  </MyWorldSmallImage> 
  <MyWorldLargeImage> 
   http://i2.ebayimg.ebay.com/01/u/000/77/23/anotherimage.JPG?set_id=34 
  </MyWorldLargeImage> 
  <RevIEwsAndGuideURL> 
   http://search.revIEws.us.ebay.com/members/johndoesUserID 
  </RevIEwsAndGuideURL> 
  <FeedbackDetailsURL> 
   http://feedback.ebay.com?VIEwFeedback&userid=johndoesUserID&... 
  </FeedbackDetailsURL> 
 </User> 
</GetUserProfileResponse> 

  檢索作為 SOAP 信封的基本用戶信息

  清單 6 展示了檢索作為一個 SOAP 請求提供的基本用戶信息的 eBay API HTTP 請求示例。


清單 6. 請求 SOAP 響應的 eBay GetUserProfile
http://open.api.ebay.com/shopping?callname=GetUserProfile 
  &responseencoding=SOAP 
  &appid=johndoesAppID 
  &siteid=0 
  &version=525 
  &UserID=johndoesUserID 
  &IncludeSelector=Details 

  清單 7 展示了對 SOAP 信封形式的詳細用戶信息的相同 eBay API 請求。


清單 7. SOAP 信封形式的 eBay GetUserProfile 請求
<?XML version="1.0" encoding="utf-8"?> 
<soapenv:Envelope 
 xmlns:soapenv="http://schemas.XMLsoap.org/soap/envelope/" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
 <soapenv:Body> 
  <GetUserProfileRequest XMLns="urn:ebay:apis:eBLBaseComponents"> 
   <UserID>johndoesUserID</UserID> 
   <IncludeSelector>Details</IncludeSelector> 
  </GetUserProfileRequest> 
 </soapenv:Body> 
</soapenv:Envelope> 

  清單 8 展示了作為 SOAP 信封返回的詳細用戶信息響應。


清單 8. eBay GetUserProfile SOAP 響應
<?XML version="1.0" encoding="utf-8"?> 
<soapenv:Envelope 
 XMLns:soapenv="/School/UploadFiles_7810/201103/20110327163515739.JPG?set_id=23 
    </MyWorldLargeImage> 
    <RevIEwsAndGuideURL> 
     http://search.revIEws.us.ebay.com/members/johndoesUserID 
    </RevIEwsAndGuideURL> 
    <FeedbackDetailsURL> 
     http://feedback.ebay.com/ws?VIEwFeedback&userid=johndoesUserID&... 
    </FeedbackDetailsURL> 
   </User> 
  </GetUserProfileResponse> 
 </soapenv:Body> 
</soapenv:Envelope> 

  Amazon

  Amazon 提供幾個 Web 服務 API — 為方便起見稱為 Amazon Web Services — 來交付其功能,以便訪問 Amazon 的服務基礎設施。這些服務包括(但不僅限於)在 表 2 中列示和描述的服務。


表 2. 一些 Amazon Web 服務

服務 說明 Elastic Compute Cloud (EC2) 在雲中提供可調整大小的計算能力的 Web 服務 Simple Storage Service (S3) 可用於在任何時間、從 Web 上的任意位置存儲並檢索大量數據的簡單 Web 服務接口 CloudFront 用於內容交付的 Web 服務 SimpleDB 用於在雲中的結構化數據上運行查詢的 Web 服務 Simple Queue Service (SQS) 用於在分布式應用程序之間移動消息的雲托管的隊列 E-Commerce Service (ECS) 提供創建集成 ECS 和其他 Amazon 服務的 Web 站點和應用程序的設施,以便檢索產品數據、來自客戶的內容、賣家信息、產品列表、購物車等信息

  檢索 XML 文檔形式的公共用戶信息

  以下示例使用 Amazon ECS 服務來檢索關於一個給定客戶的公共信息。這個 API 要求每個請求使用一個請求簽名驗證。這個請求簽名通過使用一個 Amazon Web Service 秘密密匙對請求的類型、域、URI 和該請求中的每個參數的一個排序字符串進行基本的 Base64 編碼而創建。

  清單 9 一個 Amazon ECS API 請求的典型簽名請求示例。


清單 9. Amazon CustomerContentSearch 請求
http://webservices.amazon.com/onca/XML? 
Service=AWSECommerceService 
&AWSAccessKeyId=johndoesAccessKeyID 
&AssociateTag=johndoesAssociateID 
&Operation=CustomerContentSearch 
&Name=Jane%20Doe 

  您可以通過以下步驟創建清單 9 中的簽名請求:

  將時間戳附加到這個 URL:http://ecs.amazonaws.com/onca/XML? 
Service=AWSECommerceService 
&AWSAccessKeyId=johndoesAccessKeyID 
&AssociateTag=johnDoesAssociateID 
&Operation=CustomerContentSearch 
&Name=Jane%20Doe

  對請求進行 URL 編碼。

  對一個請求進行 URL 編碼意味著通過將非 URL 兼容字符轉換為 URL 兼容字符來將請求字符串轉換為有效的 URL 格式。多數編程語言擁有完成這個任務的標准庫和/或函數。

  提取查詢參數並刪除與字符(&):

Service=AWSECommerceService 
AWSAccessKeyId=johndoesAccessKeyID 
AssociateTag=johnDoesAssociateID 
Operation=CustomerContentSearch 
Name=Jane%20Doe

  按照 ASCII 字節值對查詢參數排序:AssociateTag=johnDoesAssociateID 
AWSAccessKeyId=johndoesAccessKeyID 
Name=Jane%20Doe 
Operation=CustomerContentSearch 
Service=AWSECommerceService

  用與字符(&)重新連接排序後的查詢參數列表。結果是一個規范字符串:AssociateTag=johnDoesAssociateID 
&AWSAccessKeyId=johndoesAccessKeyID 
&Name=Jane%20Doe 
&Operation=CustomerContentSearch 
&Service=AWSECommerceService

  使用分行符將請求類型頭部附加到這個規范字符串的前面:GET 
ecs.amazonaws.com 
/onca/XML 

  將要執行簽名的字符串看起來類似於 清單 10。


清單 10. Amazon CustomerContentSearch 簽名字符串
GET 
ecs.amazonaws.com 
/onca/XML 
AssociateTag=johnDoesAssociateID 
&AWSAccessKeyId=johndoesAccessKeyID 
&Name=Jane%20Doe 
&Operation=CustomerContentSearch 
&Service=AWSECommerceService 

HMac 和 SHA-256

  Hash-based Message Authentication Code (HMac) 是一種聯合使用散列函數和秘密密匙的消息驗證加密機制。

  Secure Hash Algorithm 256 (SHA-256) 是一個 32 位加密散列函數,用於在消息驗證中驗證數據塊的真實性。

  現在,使用 SHA-256 和您的 Amazon Secret Access Key 從 清單 10 中的字符串創建一個 Request for Comments (RFC) 2104-compliant HMac 代碼。簽名後的字符串類似於 清單 11。


清單 11. Amazon CustomerContentSearch 簽名字符串
Nace+U3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg= 

  現在,URL 對簽名編碼,如 清單 12 所示。


清單 12. Amazon CustomerContentSearch 已簽名和已編碼字符串
Nace%2BU3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg%3D 

  然後,這個經過 URL 編碼的簽名被附加到 API 請求的 URL 後面,如 清單 13 所示。


清單 13. 帶有簽名的 Amazon CustomerContentSearch 請求
http://ecs.amazonaws.com/onca/XML?Service=AWSECommerceService 
  &AWSAccessKeyId=johndoesAccessKeyID 
  &AssociateTag=johnDoesAssociateID 
  &Name=Jane%20Doe 
  &Operation=CustomerContentSearch 

  由 CustomerContentSearch 檢索的信息量通過請求中指定的響應組和客戶提供的數據決定。清單 14 是包含一個完整 XML 客戶數據的典型響應。


清單 14. Amazon CustomerContentSearch XML 響應
<CustomerContentSearchResponse 
 XMLns="http://webservices.amazon.com/AWSECommerceService/2005-10-05"> 
 <OperationRequest> 
  <HTTPHeaders> 
   <Header Name="UserAgent" Value="Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) 
AppleWebKit/532.0 (KHtml, like Gecko) Chrome/3.0.195.27 Safari/532.0"></Header> 
  </HTTPHeaders> 
  <RequestId>0RXAHS524460FBBFDMGJ</RequestId> 
  <Arguments> 
   <Argument Name="Service" Value="AWSECommerceService"></Argument> 
   <Argument Name="Name" Value="Jane Doe"></Argument> 
   <Argument Name="Operation" Value="CustomerContentSearch"></Argument> 
   <Argument Name="AssociateTag" Value="johnDoesAssociateID"></Argument> 
   <Argument Name="Signature" 
Value="SOWM2hnR8zF5b9tyXQEsO0oG0crM7rbSwy1QL7UDfT0="></Argument> 
   <Argument Name="AWSAccessKeyId" Value="johndoesAccessKeyID"></Argument> 
  </Arguments> 
  <RequestProcessingTime>0.104732990264893</RequestProcessingTime> 
 </OperationRequest> 
 <Customers> 
  <Request> 
   <IsValid>True</IsValid> 
   <CustomerContentSearchRequest> 
    <Name>Jane Doe</Name> 
   </CustomerContentSearchRequest> 
  </Request> 
  <TotalResults>1</TotalResults> 
  <TotalPages>1</TotalPages> 
  <Customer> 
   <CustomerId>janedoesCustomerID</CustomerId> 
  </Customer> 
 </Customers> 
</CustomerContentSearchResponse> 

  檢索作為 SOAP 信封的公共用戶信息

  Amazon SOAP 請求根據地區和請求是否在一個安全的連接上進行來指定端點。表 3 中列示的 SOAP 端點可用於 Amazon Product Advertising API SOAP 請求。


表 3. Amazon Web Services SOAP 端點

地區 端點 CA http://ecs.amazonaws.ca/onca/soap
https://aws.amazonaws.ca/onca/soap DE http://ecs.amazonaws.de/onca/soap
https://aws.amazonaws.de/onca/soap FR http://ecs.amazonaws.fr/onca/soap
https://aws.amazonaws.fr/onca/soap JP http://ecs.amazonaws.jp/onca/soap
https://aws.amazonaws.jp/onca/soap UK http://ecs.amazonaws.co.uk/onca/soap
https://aws.amazonaws.co.uk/onca/soap US http://ecs.amazonaws.com/onca/soap
https://aws.amazonaws.com/onca/soap

  清單 15 展示了傳遞到對 Amazon ItemSearch 操作的調用的 SAOP 信封示例。


清單 15. 作為 SOAP 的 Amazon CustomerContentSearch 響應
<?XML version="1.0" encoding="UTF-8" ?> 
<soapenv:Envelope 
   xmlns:soapenv="http://schemas.XMLsoap.org/soap/envelope/" 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
 <soapenv:Body> 
  <ItemSearch XMLns="http://ecs.amazonaws.com/AWSECSCommerce/onca/soap"> 
   <AWSAccessKeyId>johndoesAccessKeyID</AWSAccessKeyId> 
   <Request> 
    <SearchIndex>Books</SearchIndex> 
    <Keywords>Lord%20of%20the%20Rings</KeyWords> 
   </Request> 
  </ItemSearch> 
 </soapenv:Body> 
</soapenv:Envelope> 

  Google

  Google 提供了廣泛的通過 Web 服務 API 啟用的在線服務。多數這些 API 使用一種基於 XML 的請求/響應格式支持。Google APPS 就是一個在 Google 基礎設施范圍內支持基於 XML 的交互的 Google 服務示例。

  驗證 API 請求

  許多 Google 服務在交換信息之前要求對訪問進行授權。在 API 中使用的一種機制稱為 ClientLogin API。經過 ClIEntLogin API 授權的訪問涉及到一個 Google 簽發的、可以在所有後續請求中使用的令牌。

  清單 16 展示了向 Google 請求一個 ClIEntLogin 令牌的請求。


清單 16. Google ClIEntLogin 請求
https://www.google.com/accounts/ClIEntLogin? 
accountType=HOSTED_OR_GOOGLE 
&[email protected] 
&Passwd=foobar 
&service=aPPS 
&source=mytestapp 

  清單 16 中的請求的成功響應類似於 清單 17。


清單 17. Google ClIEntLogin 響應
SID=ABCDE... 
LSID= ABCDE... 
Auth= ABCDE... 

  針對本文的目的,您可以忽略清單 17 中展示的兩個代碼:SID 和 LSID。但是,Auth 值是您可以在後續 Google API 請求中使用的驗證令牌。

  安全的 API 請求要求 Authorization 頭部使用來自一個成功的 ClIEntLogin 響應的令牌設置,如 清單 18 所示。


清單 18. 安全的 Google 請求的 Google 授權頭部
Authorization: GoogleLogin auth=ABCDE... 

  檢索作為 Atom 提要的用戶個人信息

  cURL

  cURL 是一個使用 URL 語法執行在線請求的命令行工具,它支持 FTP、FTPS、HTTP、HTTPS、SCP、SFTP、TFTP、TELNET、DICT、LDAP、LDAPS 等協議。cURL 還支持 SSL 證書、HTTP POST、HTTP PUT、FTP 上傳、基於 HTTP 格式的上傳、代理、cookIE 以及用戶+密碼驗證等。

  Google Apps 在一個 profile 提要中提供用戶概況信息。您可以使用一個 profile 提要來查看並修改與 Google APPS 帳戶關聯的用戶個人信息。profile 提要使用 Profiles Data API 檢索。

  清單 19 展示了一個用於檢索 Google APPS 用戶 [email protected] 的個人信息的標准 HTTP GET 請求的示例。該請求作為一個 cURL 命令行傳輸,並傳送一個此前檢索到的驗證令牌。


清單 19. Google 檢索用戶 profile 提要請求
curl -k --header "Authorization: GoogleLogin auth=ABCDE..." \ 
https://aPPS-apis.google.com/a/feeds/example.com/user/2.0/johndoe 

  這個前導的 cURL 命令包括從一個此前的成功 ClIEntLogin 請求返回的 Auth 令牌。當這個命令成功執行後,應該返回一個類似於 清單 20 中的示例的 Atom 條目。


清單 20. Google 檢索用戶 profile Atom 提要響應
<?XML version='1.0' encoding='UTF-8'?> 
<entry XMLns='http://www.w3.org/2005/Atom' 
    XMLns:apps='http://schemas.google.com/aPPS/2006' 
    XMLns:gd='http://schemas.google.com/g/2005'> 
 <id>https://aPPS-apis.google.com/a/feeds/example.com/user/2.0/johndoe</id> 
 <updated>1970-01-01T00:00:00.000Z</updated> 
 <category 
  scheme='http://schemas.google.com/g/2005#kind' 
  term='http://schemas.google.com/aPPS/2006#user'/> 
 <title type='text'>John Doe</title> 
 <link 
  rel='self' 
  type='application/atom+XML' 
  href='https://aPPS-apis.google.com/a/feeds/example.com/user/2.0/johndoe'/> 
 <link 
  rel='edit' 
  type='application/atom+XML' 
  href='https://aPPS-apis.google.com/a/feeds/example.com/user/2.0/johndoe'/> 
 <aPPS:login 
  userName='johndoe' 
  suspended='false' 
  ipWhitelisted='false' 
  admin='true' 
  changePassWordAtNextLogin='false' 
  agreedToTerms='true'/> 
 <aPPS:quota limit='7168'/> 
 <aPPS:name familyName='Doe' givenName='John'/> 
 <gd:feedLink 
  rel='http://schemas.google.com/aPPS/2006#user.nicknames' 
  href='https://aPPS- 
apis.google.com/a/feeds/example.com/nickname/2.0?username=johndoe'/> 
 <gd:feedLink 
  rel='http://schemas.google.com/aPPS/2006#user.emailLists' 
  href='https://aPPS- 
apis.google.com/a/feeds/example.com/emailList/2.0?recipIEnt=johndoe%example.com'/> 
</entry> 

  檢索 RSS 提要形式的用戶個人信息

  您可以參數化許多 Google API 以返回 RSS 格式的結果,這通常需要在輸入請求上設置一個參數。

  要使用 cURL 檢索 RSS 形式的相同的 [email protected] 用戶個人信息,執行 清單 21 中的命令行。


清單 21. Google 檢索用戶 profile RSS 提要請求
curl -k --header "Authorization: GoogleLogin auth=ABCDE..." \ 
https://aPPS-apis.google.com/a/feeds/example.com/user/2.0/johndoe?alt=rss 

  在上述 cURL 命令成功執行後,一個類似於 清單 22 的 RSS 條目將作為響應返回。


清單 22. Google 檢索用戶 profile RSS 提要響應
<?XML version='1.0' encoding='UTF-8'?> 
<item XMLns:atom='http://www.w3.org/2005/Atom' 
   XMLns:apps='http://schemas.google.com/aPPS/2006' 
   XMLns:gd='http://schemas.google.com/g/2005'> 
 <guid isPermaLink='false'> 
  https://aPPS-apis.google.com/a/feeds/example.com/user/2.0/johndoe 
 </guid> 
 <atom:updated>1970-01-01T00:00:00.000Z</atom:updated> 
 <category domain='http://schemas.google.com/g/2005#kind'> 
  http://schemas.google.com/aPPS/2006#user 
 </category> 
 <title>John Doe</title> 
 <aPPS:login 
  userName='johndoe' 
  suspended='false' 
  ipWhitelisted='false' 
  admin='true' 
  changePassWordAtNextLogin='false' 
  agreedToTerms='true'/> 
 <aPPS:quota limit='7168'/> 
 <aPPS:name familyName='Doe' givenName='John'/> 
 <gd:feedLink 
  rel='http://schemas.google.com/aPPS/2006#user.nicknames' 
  href='https://aPPS- 
apis.google.com/a/feeds/example.com/nickname/2.0?username=johndoe'/> 
 <gd:feedLink 
  rel='http://schemas.google.com/aPPS/2006#user.emailLists' 
  href='https://aPPS- 
apis.google.com/a/feeds/example.com/emailList/2.0?recipIEnt=johndoe%40example.com'/> 
</item> 

  Facebook

  Facebook 發布了 Facebook Platform 來支持開發人員創建允許用戶共享信息並保持連接的應用程序。在 Facebook 上運行的應用程序可以使用此前在 表 1 中描述的 FBML 標記語言來構造。

  使用 FBML 的登錄按鈕頁面

  Facebook 提供 FBML 作為一種構建與 Facebook 服務和數據進行交互的頁面的機制。清單 23 中的啟用了 FBML 的頁面構造了一個登錄按鈕,用於對要登錄到 Facebook 平台的用戶進行驗證。您可以將這個按鈕嵌入到任何 Html 頁面中。


清單 23. 啟用 FBML 的頁面
<Html> 
<head> 
<title>test</title> 
</head> 
<body> 
<script 
src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.JS.PHP/en_US" 
type="text/Javascript"> 
</script> 
<script type="text/Javascript"> 
 FB.init("ff60291faf5de5c350fab4d4cc84b9cb"); 
</script> 
 
 <fb:login-button length="short" size="medium"></fb:login-button> 
 <br/> 
 <fb:profile-pic uid="loggedinuser" size="square" facebook-logo="true"> 
 </fb:profile-pic> 
 <br/> 
 <fb:name uid="loggedinuser" useyou="false" linked="true"></fb:profile-pic> 
 <br/> 
 <div id="profile_pics"></div> 
<script type="text/Javascript"> 
var widget_div = document.getElementById("profile_pics"); 
FB.ensureInit(function () { 
 FB.Facebook.get_sessionState().waitUntilReady(function() { 
 FB.Facebook.apiClient.frIEnds_get(null, function(result) { 
  var markup = ""; 
  var num_frIEnds = result ? Math.min(5, result.length) : 0; 
  if (num_frIEnds > 0) { 
   for (var i=0; i<num_frIEnds; i++) { 
    markup += 
     '<fb:profile-pic 
       size="square" 
       uid="'+result[i]+'" 
       facebook-logo="true"></fb:profile-pic>'; 
   } 
  } 
  widget_div.innerHtml = markup; 
  FB.XFBML.Host.parseDomElement(widget_div); 
 }); 
 }); 
}); 
</script> 
</body> 
</Html> 

  用於解析清單 23 中的 FBML 表單的響應在 圖 1 中展示。


圖 1. 用 FBML 呈現的響應
結合社會媒體 API 和基於 XML 的數據格式

  使用 XFBML 的登錄按鈕頁面

  Facebook 提供了 XFBML 作為 FBML 的一個子集,用於允許將基於 FBML 的頁面整合到一個 Html 頁面中,以便使用 Facebook Connect 或在一個內嵌框架(iframe)中使用 FBML。

  清單 24 構建了一個用於對要登錄到 Facebook Connect 平台的用戶進行驗證的登錄按鈕。您可以將這個按鈕嵌入到任何 Html 頁面中。


清單 24. 支持 XFBML 的頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHtml 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xHtml1-strict.dtd"> 
<html XMLns="http://www.w3.org/1999/xHtml" 
XMLns:fb="http://www.facebook.com/2008/fbml"> 
<head> 
<title>test</title> 
</head> 
<body> 
 <script 
src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.JS.PHP/en_US" 
type="text/Javascript"></script> 
 <fb:login-button></fb:login-button> 
 <script type="text/Javascript"> FB.init("ff60291faf5de5c350fab4d4cc84b9cb", 
"xd_receiver.htm"); </script> 
</body> 
</Html> 

  用於解析清單 24 中的 XFBML 表單的響應在 圖 2 中展示。


圖 2. 用 XFBML 呈現的響應
結合社會媒體 API 和基於 XML 的數據格式

  Twitter

  REST

  Representational State Transfer (REST) 是一個用於分布式系統的軟件架構和會話模型。術語 Representational State Transfer(具象狀態傳輸)是 Roy FIElding(Hypertext Transfer Protocol 1.0 版和 1.1 版的作者之一)在他的博士論文中首次定義並引入的。

  Twitter 正在 Web 上流行,這在很大程度上歸功於 Twitter 可以通過其 API 輕松向開發人員提供其服務。Twitter 的 API 基於 REST,可以返回 XML、JavaScript Serialized Object Notation (JSON)、RSS 和 Atom 提要格式的結果。

  檢索 RSS 提要形式的 Twitter 公共時間表

  Twitter 允許您檢索多種格式的 API 響應,其中包括 RSS。清單 25 展示了一個 Twitter API 請求,該請求使用 cURL 命令行從 Twitter 檢索 RSS 提要形式的公共時間表(timeline)。


清單 25. Twitter 公共時間表請求
curl http://twitter.com/statuses/public_timeline.rss 

  在上述 cURL 命令成功執行後,一個類似於 清單 26 中的示例的 RSS 提要將作為響應返回。


清單 26. Twitter 公共時間表響應
<?XML version="1.0" encoding="UTF-8"?> 
<rss XMLns:atom="http://www.w3.org/2005/Atom" version="2.0"> 
 <channel> 
  <title>Twitter public timeline</title> 
  <link>http://twitter.com/public_timeline</link> 
  <atom:link type="application/rss+XML" 
href="http://twitter.com/statuses/public_timeline.rss" rel="self"/> 
  <description>Twitter updates from everyone!</description> 
  <language>en-us</language> 
  <ttl>40</ttl> 
 <item> 
  <title>Here is a tweet title</title> 
  <description>Here is a tweet description</description> 
  <pubDate>Tue, 10 Nov 2009 18:28:16 +0000</pubDate> 
  <guid>http://twitter.com/astrogerly/statuses/1234567890</guid> 
  <link>http://twitter.com/astrogerly/statuses/1234567890</link> 
 </item> 
 <item> 
  <title>Here is another tweet title</title> 
  <description>Here is another tweet description</description> 
  <pubDate>Tue, 9 Nov 2009 14:32:11 +0000</pubDate> 
  <guid>http://twitter.com/astrogerly/statuses/6789012345</guid> 
  <link>http://twitter.com/astrogerly/statuses/6789012345</link> 
 </item> 
 ... 
 </channel> 
</rss> 

  檢索 XML 形式的好友時間表

  如果需要,Twitter API 可以返回格式為 plain old XML 的結果。可以通過在請求 URL 的末尾添加 XML 來實現這一點。

  某些 API 在可以被訪問之前要求驗證,驗證方法是在請求中包含一個簡單的 “用戶名/密碼” 對。

  要使用 cURL 檢索一個給定用戶的 Twitter 好友的 XML 形式的時間表,執行 清單 27 中的命令。


清單 27. Twitter 好友的時間表請求
curl -u johndoe:johndoespassWord http://twitter.com/statuses/frIEnds_timeline.XML 

  注意 清單 27 的命令行中用於該 API 的 username:passWord 對。

  在上述 cURL 命令成功執行後,一個類似於 清單 28 中的示例的 XML 文檔將作為響應返回。


清單 28. Twitter 好友的時間表響應
<?XML version="1.0" encoding="UTF-8"?> 
<statuses type="array"> 
 <status> 
  <created_at>Tue Nov 10 16:50:40 +0000 2009</created_at> 
  <id>1234567890</id> 
  <text>Tweet shown here</text> 
  <source>web</source> 
  <user> 
   <id>7654321</id> 
   <name>atwitterusername</name> 
   <screen_name>atwitterscreenname</screen_name> 
   <location>Worldwide...</location> 
   <description>User description</description> 
   ... 
  </user> 
 </status> 
 <status> 
  <created_at>Tue Nov 9 14:33:22 +0000 2009</created_at> 
  <id>1234567890</id> 
  <text>Another tweet shown here</text> 
  <source>web</source> 
  <user> 
   <id>1234567</id> 
   <name>anothertwitterusername</name> 
   <screen_name>anothertwitterscreenname</screen_name> 
   <location>Worldwide...</location> 
   <description>Another user description</description> 
   ... 
  </user> 
 </status> 
 ... 
</statuses> 

  檢索作為 Atom 提要的 Twitter 公共時間表

  Twitter API 還可以返回 Atom 格式的結果。同樣,您可以通過在請求 URL 的末尾添加 “atom” 來實現這一點,如 清單 29 的 cURL 命令行所示。


清單 29. 作為 Atom 提要的 Twitter 公共時間表請求
curl http://twitter.com/statuses/public_timeline.atom 

  在上述 cURL 命令成功執行後,一個類似於 清單 30 中的示例的 Atom 提要將作為響應返回。


清單 30. 作為 Atom 提要返回的 Twitter 公共時間表響應
<?XML version="1.0" encoding="UTF-8"?> 
<feed xml:lang="en-US" XMLns="http://www.w3.org/2005/Atom"> 
 <title>Twitter public timeline</title> 
 <id>tag:twitter.com,2007:Status</id> 
 <link type="text/Html" 
    href="http://twitter.com/public_timeline" 
    rel="alternate"/> 
 <link type="application/atom+XML" 
    href="http://twitter.com/statuses/public_timeline.atom" 
    rel="self"/> 
 <updated>2009-11-10T18:48:25+00:00</updated> 
 <subtitle>Twitter updates from everyone!</subtitle> 
  <entry> 
   <title>username: @Here is a tweet</title> 
   <content type="Html">username: @Here is a tweet</content> 
   <id> 
    tag:twitter.com,2007:http://twitter.com/username/statuses/1234567890 
   </id> 
   <published>2009-11-10T18:48:25+00:00</published> 
   <updated>2009-11-10T18:48:25+00:00</updated> 
   <link type="text/Html" 
      href="http://twitter.com/username/statuses/1234567890" 
      rel="alternate"/> 
   <link type="image/jpeg" 
      href="http://a3.twimg.com/profile_images/1234567890/userimage.JPG" 
      rel="image"/> 
   <author> 
    <name>User Name</name> 
   </author> 
  </entry> 
  <entry> 
   <title>anotherusername: @Here is another tweet</title> 
   <content type="Html">anotherusername: @Here is another tweet</content> 
   <id> 
    tag:twitter.com,2007:http://twitter.com/anotherusername/statuses/6789012345 
   </id> 
   <published>2009-11-09T16:42:15+00:00</published> 
   <updated>2009-11-09T16:42:15+00:00</updated> 
   <link type="text/Html" 
      href="http://twitter.com/anotherusername/statuses/6789012345" 
      rel="alternate"/> 
   <link type="image/jpeg" 
      href="http://a3.twimg.com/profile_images/6789012345/userimage.JPG" 
      rel="image"/> 
   <author> 
    <name>Anotheruser Name</name> 
   </author> 
  </entry> 
</feed> 

  OpenSocial

  OpenSocial 是一個組織聯盟(包括 Google、MySpace 等),致力於為構建社會媒體應用程序和服務定義公共 API 和標記語言。OpenSocial 聯盟定義的主要標記語言是 OpenSocial Markup Language (OSML) 和 OpenSocial Template (OST)。

  使用 OST 進行聲明性標記

  OST 是一種針對小工具開發人員設計的聲明性標記語言,用於支持他們創建用於構建數據驅動的應用程序和站點的可重用模板。OST 實現在抵達客戶機的浏覽器之前解析並執行 OST 代碼,這個過程允許數據在浏覽器中呈現之前對其進行檢索、存儲和注入等操作。

  在 OST 中使用 OSML

  OSML 定義一組標記,每個 OpenSocial 兼容的容器都能解析並呈現該標記。OST 頁面中的 OSML 標記用於檢索諸如用戶個人信息、好友列表之類的數據。

  使用 OST/OSML 檢索用戶的好友

  清單 31 中的 OST/OSML 小工具示例展示了如何使用 OSML PeopleRequest 標記來檢索一個用戶的好友。userId 屬性 userId 和 groupId 指定檢索哪個好友列表。然後,OST 容器在頁面執行時呈現該好友列表。


清單 31. OpenSocial OST/OSML 小工具
<?XML version="1.0" encoding="UTF-8"?> 
<Module> 
 <ModulePrefs title="Server-side Template"> 
  <Require feature="opensocial-data" /> 
  <Require feature="opensocial-templates"> 
  </Require> 
 </ModulePrefs> 
 <Content type="Html"> 
  <![CDATA[    
   <script XMLns:os="http://ns.opensocial.org/2008/markup" type="text/os-data"> 
    <os:PeopleRequest key="friends" userId="@viewer" groupId="@frIEnds"/> 
   </script> 
 
   <script type="text/os-template"> 
    <ul> 
     <li repeat="${frIEnds}"> 
      <span id="id${Context.Index}">${Cur.name.givenName}</span> 
     </li> 
    </ul> 
   </script> 
  ]]> 
 </Content> 
</Module> 

  結束語

  社會媒體正迅速成為允許用戶共享內容和媒體等資源的新一波科技、技術和 API 浪潮,您肯定不會對這一新浪潮無動於衷。社會媒體正在 LinkedIn、Facebook、Twitter 以及 Google 提供的眾多站點和服務上流行起來。

  在本文中,我們探討了與使用各種 API 和基於 XML 的數據格式(如 RSS、Atom、FBML、OSML、SOAP 和 simple XML)的社會媒體站點進行交互相關的概念、設計和實現。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved