Log in

View Full Version : Resolved Simplepie Error handling



X-Tream
04-19-2013, 06:23 PM
Hello,

I'm in need of some sort of an error handling script...
My site fetches rss feeds from other sites (by using simplepie) and displays them like this: (http://netti-tv.net), but i'm having problems with one of the rss feeds, as some times the feed has no image -> my site crashes giving me this error code (http://netti-tv.net/katsomo):


Warning: Invalid argument supplied for foreach() in /home2/nettib22/public_html/katsomo.php on line 133

Fatal error: Call to a member function get_link() on a non-object in /home2/nettib22/public_html/katsomo.php on line 134

Lines 133-138:

foreach($item->get_enclosures() as $enclosure);
$i['img'] = str_replace("_m.jpg","_s.jpg" , $enclosure->get_link());

if(empty($i['img'])){
$i['img'] = 'img/noimage.png';
}

I'm looking for a script that checks the rss feeds images and if there is no image it replaces it with "img/noimage.png".
The above script works other vice but it has no error handling...

Sorry for my bad English. I really hope somebody could help me as it's really annoying that my site crashes...I would really appreciate it.

Edit: For now there is no error in http://netti-tv.net/katsomo but i'm still looking for the script. =/

traq
04-20-2013, 12:57 AM
# the first error is happening because foreach{} expects an array (or iterable object).
# you need to make sure that's what you give it.
# the second error is happening because of the first error.

# try the method first
$enclosures = $item->get_enclosures();

# see if it worked
# (I'm assuming this get_enclosures method returns FALSE if it doesn't return an array.
# If that is not the case, you might need to use "if( is_array( $enclosures ) )" instead -
# more correct, more accurate; but, if it works, doing "if( $enclosures )" is faster.
if( $enclosures ){

// foreach($item->get_enclosures() as $enclosure);
# ALWAYS use brackets!
# pass $enclosures to foreach{}
foreach( $enclosures as $enclosure ){ //<-- important opening bracket

# likewise, you might want to check if ::get_link() is returning a string as you expect
$i['img'] = str_replace("_m.jpg","_s.jpg" , $enclosure->get_link());

}//<-- important closing bracket

} // end "if( $enclosures )"

if(empty($i['img'])){
$i['img'] = 'img/noimage.png';
}

X-Tream
04-20-2013, 07:43 AM
Thank you so much for your help! It's working like a dream. :)

traq
04-20-2013, 02:15 PM
glad to hear it!

If your question has been answered, please mark your thread "resolved":
On your original post (post #1), click [edit], then click [go advanced]. In the "thread prefix" box, select "Resolved". Click [save changes].