Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

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


Avatar of hernst42
hernst42
Flag of Germany image

see http://de.php.net/manual/de/domelement.getelementsbytagnamens.php
and try:
$channel_thumbnail= $channel->getElementsByTagNameNS('media', 'thumbnail')->item(0)->childNodes->item(0)->nodeValue;
Avatar of error77
error77

ASKER

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
Avatar of error77

ASKER

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

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

Avatar of error77

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of error77

ASKER

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

Avatar of error77

ASKER

Never mind. Done it. Perfect, thanks a million :o)
Excellent - glad you're on the right track, and thanks for the points, ~Ray
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
Avatar of error77

ASKER

Great thanks :)