从媒体获取URL:使用jQuery的RSS XML feed中的内容 [英] Get URL from media:content in RSS XML feed with jQuery

查看:58
本文介绍了从媒体获取URL:使用jQuery的RSS XML feed中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从media:content中为RSS feed中的每个帖子检索图像(以及其他内容): http://bits.blogs.nytimes.com/feed/

I am trying to retrieve the images (along with other content) from media:content for each post in an RSS feed: http://bits.blogs.nytimes.com/feed/

我尝试了此链接(以及许多其他链接) jQuery XML解析如何获取元素属性 但我仍然遇到错误.

I tried this link (along with many others) jQuery XML parsing how to get element attribute but I am still getting errors.

也有一个PHP文件(请参阅底部),但我不认为这是问题所在.

There is a PHP file as well (see at bottom) but I don't think that it is the issue.

我尝试了几种变体,最后得到了<img src="undefined" alt="image">

I have tried several variations and I am ending up with <img src="undefined" alt="image">

这就是日志所说的TypeError: 'undefined' is not an object (evaluating '$item.find('media\\:content').attr('url').text')

这是RSS提要中XML的示例(我正在尝试获取url ="this value"中的内容) <media:content url="http://graphics8.nytimes.com/images/2011/11/30/technology/bits-daily-report/bits-daily-report-thumbStandard.jpg" medium="image" width="75" height="75"></media:content>

Here is an example of the XML from the RSS feed (I am trying to grab what is in url="this value") <media:content url="http://graphics8.nytimes.com/images/2011/11/30/technology/bits-daily-report/bits-daily-report-thumbStandard.jpg" medium="image" width="75" height="75"></media:content>

这是我的带jQuery嵌入的HTML.感谢您提供的所有解决方案,指示和建议.我现在遇到障碍了.

Here is my HTML with jQuery Embeded. Thank you for all solutions, pointers, and Suggestions. I am hitting a roadblock right now.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Your Site Title</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){

function get_rss_feed() {

// RSS Feed Var
var rssUrl = 'http://bits.blogs.nytimes.com/feed/';

//clear the content in the div for the next feed.
$("#feedContent").empty();

//use the JQuery get to grab the URL from the selected item, put the results in to an argument for parsing in the inline function called when the feed retrieval is complete
$.get('rss.php?url='+rssUrl, function(d) {

    //find each 'item' in the file and parse it
    $(d).find('item').each(function() {

        //name the current found item this for this particular loop run
        var $item = $(this);
        // grab the post title
        var title = $item.find('title').text();
        // grab the post's URL
        var link = $item.find('link').text();
        // next, the description
        var description = $item.find('description').text();
        //don't forget the pubdate
        var pubDate = $item.find('pubDate').text();
        var media = $item.find('media\\:content').attr('url').text();

            // now create a var 'html' to store the markup we're using to output the feed to the browser window
        var html = "<div class=\"entry\"><h2 class=\"postTitle\">" + title + "<\/h2>";
        html += "<em class=\"date\">" + pubDate + "</em>";
        html += "<p class=\"description\">" + description + "</p>";
        html += "<img src=\"" + media + "\" alt=\"image\"/>";
        html += "<a href=\"" + link + "\" target=\"_blank\">Read More >><\/a><\/div>";

        //Send Results to a div
        $('#rss').append($(html));  
    });
});

};
get_rss_feed();

  });
</script>
</head>

<body>
<h1>RSS TEST</h1>
<!-- Your RSS DIV -->
<div id="rss"></div>

</body>
</html>

这是我的PHP

<?php

  ob_start();

  $curl = curl_init($_GET['url']);

  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HEADER, false);

  $xml = curl_exec($curl);  
  header("Content-Type: text/xml");
  echo $xml; // Results

  $output = ob_get_clean();

  curl_close($curl);
  echo $output;

  ob_end_flush();


?>

推荐答案

此:

var media = $item.find('media\\:content').attr('url').text();

应该是这样:

 var media = $item.find('media\\:content, content').attr('url'):

编辑(进行了其他更改):

EDIT (addition of other changes made):

我更改了:

$(d).find('item').each(function() {

收件人:

$(d).find('item').each(function(index) {

我添加了(限制为三个供稿项):

I added (to limit to three feed items):

if (index >= 3) { return false }

然后我添加了(如果没有将图像定义为空白图像):

And I added (if there was no image defined set to blank image):

if (typeof mediatest === 'undefined') { var mediatest = 'blank.png' };

一切都很好,至少到目前为止:)

Everything works great...at least so far :)

这篇关于从媒体获取URL:使用jQuery的RSS XML feed中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆