我想从博客获得个人帖子,并使它们成为单独的类,并将其内容添加到我的网站上。我需要这样做,因为我托管网站的硬件的处理能力非常低(奔腾3),内存非常小(512MB),如果我只是在上面放一个wordpress博客,响应时间将会非常慢,即使是通过一个反向代理,如lighttpd或nginx。
因此,到目前为止,我知道我需要调用jQuery.ajax()
并将其指向博客博客的atom提要,但是之后我就迷路了。如何在获取xml数据后将其分离为单独的博客帖子/类,并可能加载将在这些博客帖子中发布的图像?
发布于 2010-10-14 23:45:36
下面是一个如何处理Atom提要的示例。在本例中,我正在获取一个本地XML提要文件。在现实世界中,您将需要一个简单的代理脚本来为您获取它,因为您无法发出跨域XML请求。简而言之,要使用jQuery处理任何XML,只需使用节点的“标记”名称循环遍历节点集合,并获取它们的内容,以后就可以根据需要重新利用这些内容……
在本例中,我正在处理一个包含标题和内容的提要tags...for摘要提要您可能需要包含一个摘要标记处理
<!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);
?>
https://stackoverflow.com/questions/3937507
复制相似问题