2010-10-14 85 views
0

我想從博客博客中獲取單個帖子,並將其製作成單獨的課程並將其內容添加到我的網站中。我需要這樣做,因爲我在我的網站上託管的硬件具有很小的處理能力(pentium 3),而且很少有內存(512 MB),如果我只是在其上放置一個wordpress博客,響應時間即使通過反向代理(如lighttpd或nginx)也會非常緩慢。如何使用jquery從blogger博客加載內容?

因此,到目前爲止,我知道我需要撥打jQuery.ajax(),並指出博主博客的原子提要,但之後我相當迷茫。如何在獲取後將xml數據分離到各個博客文章/類中,並且可能會加載將發佈在這些博客文章中的圖像?

回答

0

以下是如何處理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); 

?> 
+0

如果我要使用你描述的代理,我會添加一個URL檢查以防止黑客使用您的服務器攻擊其他網站,避免您的ISP出現問題。 – PleaseStand 2010-10-14 23:55:11

+0

這將是明智的 – Michal 2010-10-15 01:52:37

+0

這似乎是運行一個PHP代理腳本是唯一的方式去獲取從另一個域的原子飼料。謝謝,我也會研究這個。 – ertemplin 2010-10-15 02:47:54