Forum

Using RSS lib, url enclosure

2008-06-23 08:31:19

user300

Hello I have been trying to get the 'enclosure URL' on a RSS feed. I have been trying code like this:
$tnl = $item->getElementsByTagName("enclosure");
$tnl = $tnl->item(0);
$enclosure = $tnl->firstChild->data;

$attrs = $enclosure->item->attributes;
return array2(

                    'url' => $attrs->getNamedItem('url')->value,
                    'length' => $attrs->getNamedItem('length')->value,
                    'type' => $attrs->getNamedItem('type')->value);
Can you help me? Thanks for any help
2008-06-23 08:51:26

scriptol

Enclosure is an optional tag that is a child of <item> in RSS 2.0. The first part of your code seems to be perfect, but once you get the data, you should not be able to get attributes of them. An exemple of your feed would be welcomed, as enclosure usually does not have data but an attribute "url" instead.
2008-06-23 15:25:56

user300

The forum will not let me post example RSS XML code with hyperlinks or hyperlinks on their own :( This is an example RSS feed with the enclosure URL www.autoexpress.co.uk/rss/news/ It would be great to get it working with rsslib.php :D
2008-06-25 11:01:20

scriptol

You can modify rsslib to handle additional tags. You code may be modified in this way (I have not tested it):
$tnl = $item->getElementsByTagName("enclosure");
$enclosure = $tnl->item(0);

$url = $enclosure->getAttribute("url");
$length = $enclosure->getAttribute("length");
$type = $enclosure->getAttribute("type");
and then
$x = array($url, $length, $type);
getAttribute is a method of DOMElement.
2009-05-18 19:00:28

kherron

Will this code work for retrieving the "Image" tags and information? Or has anyone altered the rsslib code to work with images? Any help will be greatly appreciated. Thanks
2009-05-19 13:07:57

scriptol

Image is a sub-element of channel. Getting the link to an image as as easy than to other data In RSS_Tag, add these lines:
$tnl = $item->getElementsByTagName("image");
$image = $tnl->item(0);

$iurl = $image->getElementsByTagName("url");
$iurl = $iurl->item(0)->firstChild->data;
and so on for title, width, height
2010-07-22 10:27:01

SCSU10

Hello. I tried adding the above lines but I'm getting an error: Fatal error: Call to a member function getElementsByTagName() on a non-object in C:\path\rsslib\rsslib.php on line 46 I'm trying to separate out the image from the rest of the description. Right now, the image is showing up with no spacing around it, so it doesn't look great. Does "description" automatically include text and images? What would I need to use if I wanted to format them individually on my page? Thanks.
2010-07-22 11:08:19

scriptol

Hello, "image" is a tag in channel, not in item, so you can get it through getElementByTagName. If an image in present in the description, with a <img /> tag inside, you can extract it by a regular expression. But if you want only add a margin to the image, you can make that with a CSS style sheet.
<div id="rss">  // the layer for the feed
and the style:
img#rss
{
   margin:8px;
}
2010-07-22 11:46:06

SCSU10

I do want to just add a margin to the image, but I don't understand where to put that div (Sorry, I'm new to this). In my front-end page, I simply have:
<?php
require_once("rsslib.php");
$url = "xml file here";
echo RSS_Display($url, 15, true);
?>
Do I need to add something in the rsslib.php file itself? Thanks for your help.
2010-07-22 12:47:49

scriptol

The div encapsulates the whole feed reader:
<?php
require_once("rsslib.php");
$url = "xml file here";
echo "<div id='rss'>";
echo RSS_Display($url, 15, true);
echo "</div>";
?>
If you want to put the image at left or at right, you can use the float:left or float:right property.
img#rss
{
  margin:8px;
}
li#rss
{
 float:left;
}
This could require some tests.
2010-07-22 14:10:47

SCSU10

Wrapping it around the whole thing worked. Thanks for your help!
2010-11-21 09:41:33

bourbour

Hi there! Is it possible to have a exemple of the SCSU10 solution for his fatal error problem? I got the same, and i don't understand how he solved it. Separate description and image?? I also got a link problem : on my computer with apache in local, no prob, but once I put files at my OVH server, the rsslib.php doesn't find the xml. Strange because, I try to retrieve it by the way. And more funny the link above to suscribe at my rss flux works perfectly and its looking for the same xml. the rssLib.php, my html page and the xml file are in the same file. Thanks in advance :)
2010-11-22 02:36:51

scriptol

Hello This may comes from the configuration of PHP. Ensure you can load a file from the server. The allow_url_fopen option must be true in php.ini. You can also try to use curl instead. See: Using cURL in PHP.
2010-11-23 11:20:56

bourbour

I changed for enclosure, as I certainly need to put videos in. I erased the last post ^^ but i got an error with this on line 3 :/
$tnl = $item->getElementsByTagName("enclosure");
$enclosure = $tnl->item(0);

$url = $enclosure->getAttribute("url");
path seems to be good to retrieve an attribute, where is the problem?