如何使用jquery从博客博客加载内容?

时间:2021-12-29 08:33:41

I want to get individual posts from a blogger blog, and make them into individual classes and add their content to my website. I need to do this because the hardware that I'm hosting my website on has very little processing power (pentium 3), and very little ram (512 mb), and if I were to just put a wordpress blog on it, response time would be extremely slow, even through a reverse proxy such as lighttpd or nginx.

我想从博客博客中获取个人帖子,并将它们分成单独的类并将其内容添加到我的网站。我需要这样做,因为我托管我的网站的硬件具有非常小的处理能力(奔腾3),并且非常小的内存(512 mb),如果我只是放一个wordpress博客,响应时间即使通过反向代理,例如lighttpd或nginx,也会非常慢。

So, so far I know that I need to call jQuery.ajax() and point that to the atom feed of the blogger blog, but I'm pretty lost after that. How do I separate the xml data after getting it, into individual blog posts/classes, and possibly load images that would be posted in these blog posts?

所以,到目前为止,我知道我需要调用jQuery.ajax()并将其指向博客博客的原子提要,但之后我很失落。如何将获取后的xml数据分成单个博客帖子/类,并可能加载将在这些博客帖子中发布的图像?

1 个解决方案

#1


0  

Here is an example of how to process an Atom feed. In this example I am fetching a local XML feed file. In real world you will need a simple proxy script to fetch it for you as you cannon make cross domain XML requests. In a nutshell to process any XML using jQuery you just loop through a collection of nodes using their "tag" names and grab their content which you can later re-purpose as you see fit...

以下是如何处理Atom订阅源的示例。在此示例中,我将获取本地XML Feed文件。在现实世界中,您将需要一个简单的代理脚本来为您获取它,因为您可以创建跨域XML请求。简而言之,使用jQuery处理任何XML,您只需使用其“标记”名称循环遍历节点集合,并抓取其内容,以后您可以根据需要重新使用...

In this case I am processing a feed which contains title and content tags...for summary feeds you might need to include a summary tag processing

在这种情况下,我正在处理包含标题和内容标记的Feed ...对于摘要Feed,您可能需要包含摘要标记处理

<!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>

If you are using PHP on your server this is a simple proxy script that you will need

如果您在服务器上使用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);

?>

#1


0  

Here is an example of how to process an Atom feed. In this example I am fetching a local XML feed file. In real world you will need a simple proxy script to fetch it for you as you cannon make cross domain XML requests. In a nutshell to process any XML using jQuery you just loop through a collection of nodes using their "tag" names and grab their content which you can later re-purpose as you see fit...

以下是如何处理Atom订阅源的示例。在此示例中,我将获取本地XML Feed文件。在现实世界中,您将需要一个简单的代理脚本来为您获取它,因为您可以创建跨域XML请求。简而言之,使用jQuery处理任何XML,您只需使用其“标记”名称循环遍历节点集合,并抓取其内容,以后您可以根据需要重新使用...

In this case I am processing a feed which contains title and content tags...for summary feeds you might need to include a summary tag processing

在这种情况下,我正在处理包含标题和内容标记的Feed ...对于摘要Feed,您可能需要包含摘要标记处理

<!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>

If you are using PHP on your server this is a simple proxy script that you will need

如果您在服务器上使用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);

?>