Why isnt this code working?

hi all,

I have this code that that works:

$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;

and I've added another line to get the thumbnail:

$channel_thumbnail= $channel->getElementsByTagName('media:thumbnail')->item(0)->childNodes->item(0)->nodeValue;

But this line above does not work ..

I'm guessing its the media:thumbnail ... if so, what is the way to get the media:thumbnail ?

thanks


error77Asked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:

<?php // RAY_temp_error77.php
error_reporting(E_ALL);
echo "<pre>";


// READ XML AND SHOW IMAGES


$url = 'http://feeds.bbci.co.uk/news/rss.xml';
$xml = file_get_contents($url);
$new = str_replace('<media:', '<media_', $xml);
$obj = SimpleXML_Load_String($new);

// PAGE HEADER
echo "<h3>";
echo $obj->channel->copyright;
echo "</h3>";

// CLICKABLE IMAGES THAT PROVIDE LINKS TO STORIES
foreach ($obj->channel->item as $item)
{
    $t = $item->title;
    $l = $item->link;
    $i = $item->media_thumbnail[0]; // USE THE SMALL THUMBNAIL
    $i = $item->media_thumbnail[1]; // OR USE THE LARGER THUMBNAIL
    $w = $i["width"];
    $h = $i["height"];
    $u = $i["url"];

// USE HEREDOC NOTATION FOR IMAGE LINKS
$img = <<<IMG
<a target="_blank" href="$l" title="$t"><img alt="ImageLink" title="$t" src="$u" width="$w" height="$h" /></a>
IMG;

    // SHOW THE WORK PRODUCT
    echo PHP_EOL;
    echo '<p>';
    echo $img;
    echo $t;
    echo '</p>';
}

Open in new window

0
 
hernst42Commented:
see http://de.php.net/manual/de/domelement.getelementsbytagnamens.php
and try:
$channel_thumbnail= $channel->getElementsByTagNameNS('media', 'thumbnail')->item(0)->childNodes->item(0)->nodeValue;
0
 
error77Author Commented:
This is sooo strange...

I'm testing your code and when I add it I get a blank page...no errors or nothing, the page just turns blank.

Any ideas why please?

Thanks again
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
error77Author Commented:
OK Update:

I've looking in the source code and it says:

<b>Fatal error</b>:  Call to a member function item() on a non-object in <b>

Hope this helps

0
 
Ray PaseurCommented:
This is just a variant on a warning.  In OOP notation PHP is a little stronger.  The code snippet demonstrates the difference.
http://www.laprbass.com/RAY_expected_error.php

Set error_reporting(E_ALL); and make sure PHP is configured to display the errors with ini_set('display_errors', TRUE);

In my experience with things like the ATOM feed where you have tags like media:thumbnail I have found that you can use SimpleXML if you simply mung the tags with code like this.  It's not very scientific but it gets results.

$xml = str_replace('media:', 'media_', $xml);

But I would like to ask the larger question.  Where is the test data?  Why are you using the DOM to parse something that is perfectly designed for SimpleXML?  This work should really be going a lot easier for you, and if you want to post the inputs and the desired outputs you will find that EE is a great place to get suggestions about how to bridge the gap.  From this and other related questions it looks to me like you are using the wrong tools.  Sure, you can drive nails with a frozen pork chop, but most carpenters would choose a hammer instead.  You might want to consider recasting the questions more in the nature of, "What would the professionals use to achieve this desired result?"

Best regards, ~Ray
<?php // RAY_expected_error.php
error_reporting(E_ALL);

// CREATE A NON-OBJECT (THIS IS A STRING)
$foo = 'bar';

// WARNING IF WE TRY TO USE WITH AN ITERATOR
foreach ($foo as $lump)
{
   /* STUFF */
}
// FATAL ERROR IF WE TRY TO USE A NON-OBJECT WITH OOP NOTATION
$thing = $foo->baz();

Open in new window

0
 
error77Author Commented:
Hi Ray,

Reference your questions:

1. The Test data is here: http://feeds.bbci.co.uk/news/rss.xml
2. I'm using this system because apart from the thumbnail everything working fine for me.
If there was a similar orking SimpleXML sample I would probably use that as the base, but right now this one works fine ...I just need to get the thumbnail to show up and I'm done.
Again, if you can point me to a sampple or provided some code in simpleXML that will do this (including the thumb), I'll be happy to switch, but up to now this is all iv'e found.

Thanks

0
 
error77Author Commented:
Hi Ray...testing it out to see if I can get the other information in too.

I've tried adding:

$item_desc = $i["description"];


and then

echo $item_desc;

but it's not showing up and I don't see why :o/

How would you add it please?

Thanks

0
 
error77Author Commented:
Never mind. Done it. Perfect, thanks a million :o)
0
 
Ray PaseurCommented:
Excellent - glad you're on the right track, and thanks for the points, ~Ray
0
 
Ray PaseurCommented:
BTW, you can use var_dump() to print the contents of a SimpleXML object.  If you echo "<pre>" before, it makes an easy-to-read display of the detailed information inside the object.

Over and out, ~Ray
0
 
error77Author Commented:
Great thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.