craigslist rss feed

爷,独闯天下 提交于 2019-11-29 08:48:33

First: You can't fetch data from another domain because the crossdomain policy. I don't know about jfeed but in my projects i came up with this Solution. With this simple function you can save some bandwidth and code overhead.

Working Example

http://intervisual.de/stackoverflow/fetchxml/index.html

proxy.php (src: http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html)

<?php
// Set your return content type
header('Content-type: application/xml');

// Website url to open
$daurl = 'http://www.craigslist.org/about/best/all/index.rss';

// Get that website's content
$handle = fopen($daurl, "r");

// If there is something, read and return
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}
?>

jQuery

$.ajax({
    type: "GET",
    url: "proxy.php",
    dataType: "xml",
    success: parseXml
 });

function parseXml(xml) {
    console.log(xml);
    $(xml).find("item").each(function() {
        var content = $(this).find("title").text()
        $("#news_list").append('<li>' + content +'</li>');
    });
}

HTML

<div id="news_list"></div>

Alternatively you could use some other service to read the RSS feed and convert it to JSON, which is extremely useful if you don't have access to any server side environment.

To do this, I usually use YQL, but there are definitely other services out there.

Here is a working example using craigslist with the source: http://jsfiddle.net/soparrissays/NFSaq/2/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!