【问题标题】:How do I load content from a blogger blog with jquery?如何使用 jquery 从博主博客中加载内容?
【发布时间】:2010-10-14 20:56:48
【问题描述】:

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

所以,到目前为止,我知道我需要致电 jQuery.ajax() 并将其指向博主博客的 atom 提要,但在那之后我很迷茫。如何在获取 xml 数据后将其分离到单独的博客文章/类中,并可能加载将在这些博客文章中发布的图像?

【问题讨论】:

    标签: jquery ajax feed atom-feed


    【解决方案1】:

    以下是如何处理 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 出现问题。
    • 似乎运行 php 代理脚本是从不同域获取 atom 提要的唯一方法。谢谢,到时候我也会调查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多