以下是如何處理Atom提要的示例。在這個例子中,我獲取一個本地XML提要文件。在現實世界中,您需要一個簡單的代理腳本來爲您提取,以便您可以製作跨域XML請求。簡而言之,使用jQuery處理任何XML,您只需使用其「標記」名稱循環訪問節點集合,並獲取其內容,然後您可以根據需要重新設定它們的內容......
在這種情況下,我處理包含標題和內容標籤飼料...簡易供稿您可能需要包括摘要標籤處理
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript">
</script>
<script>
//This example shows getting a local ATOM file. I am assuming that you will be using a proxy to fetch the feed as you
//are getting it from a remote source
//get the feed
$.get("feed.xml", function(data){
//if XML loaded successfully find all blog entries
html = "";
$(data).find("entry").each(function(){
//get text for title and the content
title = $(this).find("title").text();
content = $(this).find("content").text()
//create your own html
html += "<h1>" + title + "</h1>";
html += "<div class='blogEntry'>" + content + "</div>"
})
//append html to the container of yor choice
$(".blogClone").append(html)
})
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<div class="blogClone">
</div>
</body>
</html>
如果您正在使用服務器上的PHP,這是一個簡單的代理腳本,您將需要
<?php
// PHP Proxy
// Responds to both HTTP GET and POST requests
//
// Author: Abdul Qabiz
// March 31st, 2006
//
// Get the url of to be proxied
// Is it a POST or a GET?
$url = ($_POST['url']) ? $_POST['url'] : $_GET['url'];
$headers = ($_POST['headers']) ? $_POST['headers'] : $_GET['headers'];
$mimeType = ($_POST['mimeType']) ? $_POST['mimeType'] : $_GET['mimeType'];
//Start the Curl session
$session = curl_init($url);
// If it's a POST, put the POST data in the body
if ($_POST['url']) {
$postvars = '';
while ($element = current($_POST)) {
$postvars .= key($_POST).'='.$element.'&';
next($_POST);
}
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $postvars);
}
// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, ($headers == "true") ? true : false);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Make the call
$response = curl_exec($session);
if ($mimeType != "") {
// The web service returns XML. Set the Content-Type appropriately
header("Content-Type: ".$mimeType);
}
echo $response;
curl_close($session);
?>
如果我要使用你描述的代理,我會添加一個URL檢查以防止黑客使用您的服務器攻擊其他網站,避免您的ISP出現問題。 – PleaseStand 2010-10-14 23:55:11
這將是明智的 – Michal 2010-10-15 01:52:37
這似乎是運行一個PHP代理腳本是唯一的方式去獲取從另一個域的原子飼料。謝謝,我也會研究這個。 – ertemplin 2010-10-15 02:47:54